
本示例来自于官方示例(http://doc.akka.io/docs/akka/2.4.4/intro/getting-started.html):
文中找到 Using Akka with Maven 。点击“Akka Main in Java”下载示例。
http://www.lightbend.com/activator/template/akka-sample-main-java

注意:新版本的akka需要使用jdk8
里面有两个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
|
package sample.hello; import akka.actor.Props; import akka.actor.UntypedActor; import akka.actor.ActorRef; public class extends UntypedActor { public void preStart() { final ActorRef greeter = getContext().actorOf(Props.create(Greeter.class), "greeter"); greeter.tell(Greeter.Msg.GREET, getSelf()); } public void onReceive(Object msg) { if (msg == Greeter.Msg.DONE) { getContext().stop(getSelf()); } else unhandled(msg); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
package sample.hello; import akka.actor.UntypedActor; public class Greeter extends UntypedActor { public static enum Msg { GREET, DONE; } public void onReceive(Object msg) throws InterruptedException { if (msg == Msg.GREET) { System.out.println("Hello World!"); Thread.sleep(1000); getSender().tell(Msg.DONE, getSelf()); } else unhandled(msg); } }
|
main方法
1 2 3 4 5 6 7 8
|
package sample.hello; public class Main { public static void main(String[] args) { akka.Main.main(new String[] { HelloWorld.class.getName() }); } }
|
另一种main写法,通过创建actor的方式
1 2 3 4 5
|
public static void main(String[] args) { ActorSystem system = ActorSystem.create("Hello"); ActorRef a = system.actorOf(Props.create(HelloWorld.class), "helloWorld"); System.out.println(a.path()); }
|
helloWord示例比较简单,不过多解释
近期评论