01_akka Getting Started (Java edition) __akka

Source: Internet
Author: User
Tags maven central

Document version: 2.4.9 RC1 1. Environment

Akka need to java8 the above version. 2. Introductory guidance and Template projects

The best way to learn Akka is to download Lightbend Activator and experiment with a Akka template project. 3. Module

Akka is very modular and consists of jar files that contain different features. Akka-actor–classic actors, Typed actors, IO actor etc. akka-agent–agents, integrated with Scala STM Akka-camel–apach e Camel Integration Akka-cluster–cluster Membership management, elastic routers. Akka-osgi–utilities for using Akka in OSGi containers akka-osgi-aries–aries Blueprint for provisioning actor A Kka-remote–remote Actors akka-slf4j–slf4j Logger (Event Bus listener) Akka-testkit–toolkit for testing Actor 4. Use Akka (maven)

The easiest way to build a Akka project through Maven is to download the lightbend activator tutorial called Akka Main in Java.

Starting with the 2.1-m2 version, Akka is released to the MAVEN central repository, and the following is a akka-actor-dependent instance:

<dependency>
  <groupId>com.typesafe.akka</groupId>
  <artifactid>akka-actor_2.11 </artifactId>
  <version>2.4.9-RC1</version>
</dependency>

If you need to use the snapshot version, you will also need to add the snapshot library:

<repositories>
  <repository>
    <id>akka-snapshots</id>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    <url>http://repo.akka.io/snapshots/</ url>
  </repository>
</repositories>
5.hello World

Download Lightbend Activator's tutorial called Akka Main in Java.
HelloWorld Project Pom.xml File:

<?xml version= "1.0" encoding= "UTF-8"?> <project "xmlns="
         xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http ://maven.apache.org/xsd/maven-4.0.0.xsd ">

  <modelVersion>4.0.0</modelVersion>
  < Artifactid>akka-sample-main-java</artifactid>
  <groupid>com.typesafe.akka.samples</ Groupid>
  <name>akka Main in java</name>
  <version>1.0</version>

  < properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </ properties>

  <dependencies>
    <dependency>
      <groupid>com.typesafe.akka</ groupid>
      <artifactId>akka-actor_2.11</artifactId>
      <version>2.4.4</version >
    </dependency>
  </dependencies>

</project>

Helloworld.java

Package Sample.hello;

Import Akka.actor.Props;
Import Akka.actor.UntypedActor;
Import Akka.actor.ActorRef;

public class HelloWorld extends Untypedactor {

  @Override public
  void Prestart () {
    //create the greeter actor< c7/>final actorref greeter = GetContext (). Actorof (Props.create (Greeter.class), "greeter");
    Tell it to perform the greeting
    Greeter.tell (Greeter.Msg.GREET, getself ());

  @Override public
  void OnReceive (Object msg) {
    if (msg = Greeter.Msg.DONE) {
      //When the Greeter are done, s Top this actor and with it the application
      getcontext (). Stop (Getself ());
    } else
      unhandled (msg);
}

Greeter.java

Package Sample.hello;

Import Akka.actor.UntypedActor;

public class Greeter extends Untypedactor {public

  static enum MSG {
    GREET, done;
  }

  @Override public
  void OnReceive (Object msg) {
    if (msg = msg.greet) {
      System.out.println ("Hello world!");
      Getsender (). Tell (Msg.done, getself ());
    else
      unhandled (msg);
  }

}

HelloWorld actor is the main class of the application, and when it is terminated, the program is closed. The main business logic occurs in the Prestart method, Greeter actor is created, and waits for HelloWorld to initiate a greeting (GREET). When HelloWorld a greeting (GREET) sent to Greeter,greeter by sending a backend message, it is received by the OnReceive method, and then a message (done) is returned after printing "Hello world". Tell HelloWorld to greet a sentence received, HelloWorld through OnReceive received this message (done) after the closure of HelloWorld actor.

Main.java

Package Sample.hello;

public class Main {public

  static void Main (string[] args) {
    Akka. Main.main (new string[] {HelloWorld.class.getName ()});
  }

Main.java is actually just a generic class akka.main for a small wrapper, which requires only one parameter: the class name of the application's main actor The Main method will create the infrastructure needed to run the actor, start the given main actor, and schedule the entire application shutdown. So you can run the application using the following command:
Java-classpath Akka. Main Sample.hello.HelloWorld

If you need more than Akka. Main more control startup code, you can refer to Main2.java to write your own main class.
Main2.java

Package Sample.hello;
Import Akka.actor.ActorRef;
Import Akka.actor.ActorSystem;
Import Akka.actor.Props;
Import akka.actor.Terminated;
Import Akka.actor.UntypedActor;
Import akka.event.Logging;

Import Akka.event.LoggingAdapter;
    public class Main2 {public static void main (string[] args) {Actorsystem system = actorsystem.create ("Hello");
    Actorref a = System.actorof (Props.create (Helloworld.class), "HelloWorld");
  System.actorof (Props.create (Terminator.class, a), "Terminator"); public static class Terminator extends Untypedactor {private final Loggingadapter log = Logging.getlogger (Getco
    ntext (). System (), this);

    Private final actorref ref;
      Public Terminator (Actorref ref) {this.ref = ref;
    GetContext (). watch (ref); @Override public void OnReceive (Object msg) {if (msg instanceof terminated) {Log.info ("{} has T
        erminated, shutting down system ", Ref.path ());
      GetContext (). System (). Terminate (); } else{unhandled (msg);
 }
    }

  }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.