首页 > 代码库 > Akka HelloWorld
Akka HelloWorld
Akka HelloWorld
示例代码,
package com.usoft; import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.Props; import akka.actor.UntypedActor; /** * Created by liyanxin on 2015/1/8. */ public class HelloWorld { /** * 在这里实现这样一个功能,A actor给 B actor 发送消息,接收后返回消息说已收到 */ public static class A extends UntypedActor { @Override public void preStart() throws Exception { // 使用当前actor的context 创建了一个子actor,B actor就是A actor的子actor // using an actor’s context will create a child actor final ActorRef child = getContext().actorOf(Props.create(B.class), "myChild"); child.tell("good moring", this.getSelf()); } @Override public void onReceive(Object message) throws Exception { if (message instanceof String) { System.out.println("接收到B Actor的消息:" + message); getContext().stop(getSelf()); } } } public static class B extends UntypedActor { @Override public void onReceive(Object message) throws Exception { if (message instanceof String) { System.out.println("接收到A Actor的消息:" + message); this.getSender().tell("thank you!", this.getSelf()); } } } public static void main(String args[]) { ActorSystem system = ActorSystem.create("myActorSystem"); // Actors are created by passing a Props instance into the actorOf factory method which is available on // ActorSystem and ActorContext. // 通过ActorSystem 和 ActorContext的工场方法actorOf创建actor // 工场方法需要接收一个Props instance system.actorOf(Props.create(A.class), "helloWorld"); } }
==============END==============
Akka HelloWorld
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。