通過Typesafe Activator建立akka java sample
通過Typesafe Activator建立akka java sample
What is Typesafe Activator?
Typesafe Activator gets you started with the Typesafe Reactive Platform, including Akka, Play Framework and Scala. It is a hub for developers that want to build reactive applications, providing an in browser environment for creating new applications.
首先安裝Typesafe Activator,然後啟動它,如下所示,找到該模板,建立專案,
根據這個例子,為了簡單,做了一些修改。
實現HelloWorld的actor,這個actor的作用就是發出問候訊息,並且接收MSG.DONE的訊息,處理之,如下,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package sample.hello; import akka.actor.Props; import akka.actor.UntypedActor; import akka.actor.ActorRef; public class HelloWorld extends UntypedActor { /** * Is called when an Actor is started. */ @Override public void preStart() { // create the greeter actor final ActorRef greeter = getContext().actorOf(Props.create(Greeter. class ), "greeter" ); // 把Greeter.Msg.GREET這個訊息傳送給給名字為greeter的actor,傳送者是self //getSelf() 是一個sender 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的actor接收問候訊息,然後列印,然後向其訊息的傳送者傳送DONE的訊息,如下,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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) { //得到Msg.GREET訊息,列印 System.out.println( "Hello World!" ); // 得到當前訊息的傳送者,並告訴這個傳送者訊息應經處理完成, // 此時Msg.DONE的傳送者是self getSender().tell(Msg.DONE, getSelf()); } else unhandled(msg); } } |
這是兩個actor的互動行為,在引入第三個actor,負責監視HelloWorld的actor,當其發出Terminated的訊息時,這個負責監視的actor會處理之,如下,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package sample.hello; import akka.actor.ActorRef; import akka.actor.Terminated; import akka.actor.UntypedActor; import akka.event.Logging; import akka.event.LoggingAdapter; public class Terminator extends UntypedActor { private final LoggingAdapter log = Logging.getLogger(getContext().system(), this ); private final ActorRef ref; /** * 此時的Terminator actor要觀察另一個actor ref * * @param ref */ public Terminator(ActorRef ref) { this .ref = ref; //Registers this actor as a Monitor-監視 for the provided ActorRef. getContext().watch(ref); } @Override public void onReceive(Object msg) { if (msg instanceof Terminated) { log.info( "||||||||{} has terminated, shutting down system" , ref.path()); getContext().system().shutdown(); } else { unhandled(msg); } } } |
下面就是啟動類,如下,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package sample.hello; import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.Props; public class Main2 { public static void main(String[] args) { ActorSystem system = ActorSystem.create( "Hello" ); //actorOf--Create new actor as child of this context with the given name //actor 的名字是helloWorld /** * 當建立helloWorld的actor時,會通過preStart建立一個greeter的actor, * 然後greeter.tell(Greeter.Msg.GREET, getSelf()); */ ActorRef a = system.actorOf(Props.create(HelloWorld. class ), "helloWorld" ); //actorOf--Create new actor as child of this context with the given name //actor 的名字是terminator system.actorOf(Props.create(Terminator. class , a), "terminator" ); } } |
========================================END========================================
from http://my.oschina.net/xinxingegeya/blog/364720
相關文章
- C# ActivatorC#
- 通過使用Byte Buddy,便捷地建立Java AgentJava
- Sample Schemas建庫後手動建立
- ORACLE角色(通過DBA建立)Oracle
- Java記憶體快取-通過Google Guava建立快取Java記憶體快取GoGuava
- Swift通過類名建立物件Swift物件
- 動態呼叫儲存過程 sample:儲存過程
- 2.3 通過DBCA建立資料庫資料庫
- 通過Go來分析和建立XMLGoXML
- C# 通過反射建立例項C#反射
- 通過字串動態建立類物件字串物件
- Maven通過命令建立web專案MavenWeb
- 通過oracle dbms_job建立tableOracle
- Akka系列(九):Akka分散式之Akka Remote分散式REM
- Akka 系列(九):Akka 分散式之 Akka Remote分散式REM
- java類的建立過程Java
- 3、Spring Cloud Rest工程建立(通過IDEA建立)SpringCloudRESTIdea
- java通過JXL寫ExcelJavaExcel
- 通過Go來分析和建立JSONGoJSON
- 如何通過XIB 建立自定義UIViewUIView
- java通過schema校驗xmlJavaXML
- myeclipse下通過maven建立springbootEclipseMavenSpring Boot
- 通過驅動建立與MySQL的連線MySql
- ABP教程-通過ABPboilerplate模版建立專案
- 通過RMAN Duplicate建立Oracle物理standby備庫Oracle
- Akka系列(五):Java和Scala中的FutureJava
- Akka 系列(五):Java 和 Scala 中的 FutureJava
- Javacc sampleJava
- 1.3.2.2.2 通過Non-CDB(非CDB模式)建立PDB模式
- 圖解Flutter建立Isolate的過程及通訊圖解Flutter
- Laravel 通過遷移指令碼建立MySQL檢視Laravel指令碼MySql
- Laravel 通過子查詢建立動態關聯Laravel
- 【譯】Flutter PlatformView: 如何通過原生view建立widgetFlutterPlatformView
- SAP RETAIL 通過分配表建立UB STO單據AI
- 通過trace檔案重新建立控制檔案
- Mysql 通過 Mysql_install_db 建立多例項MySql
- 通過JVMTI和JNI對JAVA加密JVMJava加密
- Akka系列(二):Akka中的Actor系統