01_akka入門(java版)__akka

來源:互聯網
上載者:User

文檔版本:2.4.9 RC1 1.環境

akka需要java8以上版本. 2.入門指導和模板項目

學習akka最好的途徑是下載 Lightbend Activator 並實驗一個akka模板項目. 3.模組

akka非常的模組化,是由包含不同特性的jar檔案組成. akka-actor – Classic Actors, Typed Actors, IO Actor etc. akka-agent – Agents, integrated with Scala STM akka-camel – Apache 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 systems akka-remote – Remote Actors akka-slf4j – SLF4J Logger (event bus listener) akka-testkit – Toolkit for testing Actor systems 4.使用akka(maven)

通過maven構建akka項目最簡單的方法就是下載 Lightbend Activator的叫做 Akka Main in Java 的教程.

從2.1-M2版本開始akka被發布到maven中央倉庫,下面是akka-actor依賴執行個體:

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

如果需要使用snapshot版本,還需要添加snapshot庫:

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

下載 Lightbend Activator的叫做 Akka Main in Java 的教程.
helloworld項目pom.xml檔案:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         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    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 is done, stop 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是應用的main class,當它終結時程式被關閉.主商務邏輯發生在preStart方法,Greeter actor被建立,並且等待HelloWorld發起一個問候(GREET).當HelloWorld一個問候(GREET)通過發送後端訊息發送給Greeter,Greeter會通過onReceive方法接收,列印”Hello World”後,再返回一個訊息(DONE)告訴HelloWorld問候一句收到,HelloWorld通過onReceive接收到這訊息(DONE)後關閉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實際上只是一個小封裝的通用類akka.Main,它只需要一個參數:應用程式主actor的類名這主方法將建立運行actor所需的基礎設施,啟動給定主要actor和安排整個應用程式關閉。因此你可以使用下面的命令運行應用程式:
java -classpath akka.Main sample.hello.HelloWorld

如果你需要比akka.Main更多的控制啟動代碼,你可以參考Main2.java編寫自己的主類.
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(getContext().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 terminated, shutting down system", ref.path());        getContext().system().terminate();      } else {        unhandled(msg);      }    }  }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.