首页 > 其他 > 详细

Akka HelloWorld

时间:2015-01-08 13:30:45      阅读:308      评论:0      收藏:0      [点我收藏+]

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

原文:http://my.oschina.net/xinxingegeya/blog/365018

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!