Akka Remote Actor_简单示例二

怎甘沉沦 提交于 2019-12-10 06:59:42

Akka Remote Actor_简单示例二


在上一篇文章中,http://my.oschina.net/xinxingegeya/blog/369445

使用Patterns.ask方法来与remote actor交互,还有没有另一种方式与remote actor交互?那就是使用remote actor path,得到ActorSelection,从而和remote actor交互。

那么先来了解一下remote actor path的构成,如下,

akka.<protocol>://<actorsystemname>@<hostname>:<port>/<actor path>

那么有上篇文章中remote actor的配置可知,其remote actor path为:

akka.tcp://CalculatorWorkerSystem@127.0.0.1 :2552/user/CalculatorActor

示例代码如下,

package com.usoft9;

import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;
import com.typesafe.config.ConfigFactory;

/**
 * Created by liyanxin on 2015/1/19.
 */
public class RemoteActorSelectionDemo {

    public static class HandlerResult extends UntypedActor {

        @Override
        public void preStart() throws Exception {
            ActorSelection selection = this.getContext().actorSelection(
                    "akka.tcp://CalculatorWorkerSystem@127.0.0.1:8989/user/CalculatorActor");
            selection.tell(new Op.Add(1, 2), this.getSelf());
        }

        @Override
        public void onReceive(Object message) throws Exception {
            if (message instanceof Op.AddResult) {
                System.out.println("add result=" + ((Op.AddResult) message).getResult());
            } else if (message instanceof Op.SubtractResult) {
                System.out.println("subtract result=" + ((Op.SubtractResult) message).getResult());
            } else if (message instanceof Op.MultiplicationResult) {
                System.out.println("multiply result=" + ((Op.MultiplicationResult) message).getResult());
            } else if (message instanceof Op.DivisionResult) {
                System.out.println("divide result=" + ((Op.DivisionResult) message).getResult());
            }
        }
    }

    public static void main(String args[]) {
        //不使用默认的配置,而是选择加载选定的remote actor配置
        final ActorSystem system = ActorSystem.create("CalculatorWorkerSystem",
                ConfigFactory.load(("usoft9/calculator")));
        //初始化远程actor
        system.actorOf(Props.create(CalculatorActor.class), "CalculatorActor");
        System.out.println("Started CalculatorWorkerSystem");

        //初始化本地的Actor
        final ActorSystem localSystem = ActorSystem.create("localSystem");
        localSystem.actorOf(Props.create(HandlerResult.class), "handlerResult");
    }
}

运行结果,

[INFO] [01/19/2015 19:43:12.184] [main] [Remoting] Starting remoting

[INFO] [01/19/2015 19:43:12.918] [main] [Remoting] Remoting started; listening on addresses :[akka.tcp://CalculatorWorkerSystem@127.0.0.1:8989]

[INFO] [01/19/2015 19:43:12.920] [main] [Remoting] Remoting now listens on addresses: [akka.tcp://CalculatorWorkerSystem@127.0.0.1:8989]

Started CalculatorWorkerSystem

[INFO] [01/19/2015 19:43:12.973] [main] [Remoting] Starting remoting

[INFO] [01/19/2015 19:43:13.021] [main] [Remoting] Remoting started; listening on addresses :[akka.tcp://localSystem@127.0.0.1:2552]

[INFO] [01/19/2015 19:43:13.022] [main] [Remoting] Remoting now listens on addresses: [akka.tcp://localSystem@127.0.0.1:2552]

Calculating 1 + 2

add result=3

===================END===================

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!