Akka路由_BroadcastRoutingLogic

Akka路由_BroadcastRoutingLogic

路由actor是将收到的消息路由到目的actor的actor. 路由acto将消息发送给它所管理的称为 ‘routees’ 的actor。

在Akka中,受Router管理的actor称作 Routee

BroadcastRoutingLogic——这里做了一个示例,讲的是广播机制的路由策略。大概是这样的,创建3个actor,通过广播机制给这三个actor广播消息。

如下,继承CustomRouterConfig实现router,

package com.usoft10;

import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.routing.ActorRefRoutee;
import akka.routing.BroadcastRoutingLogic;
import akka.routing.CustomRouterConfig;
import akka.routing.Routee;
import akka.routing.Router;

import java.util.ArrayList;
import java.util.List;

public class BurstyMessageRouter extends CustomRouterConfig {

    private int noOfInstances;

    public BurstyMessageRouter(int inNoOfInstances) {
        noOfInstances = inNoOfInstances;
    }

    @Override
    public Router createRouter(ActorSystem system) {
        final List<Routee> routees = new ArrayList<Routee>(noOfInstances);
        for (int i = 0; i < noOfInstances; i++) {
            routees.add(new ActorRefRoutee(system.actorOf(
                    Props.create(MsgEchoActor.class), "actor-" + String.valueOf(i))));
        }
        return new Router(new BroadcastRoutingLogic(), routees);
    }
}


实现接收消息的Actor,如下,

package com.usoft10;

import akka.actor.UntypedActor;

public class MsgEchoActor extends UntypedActor {
    @Override
    public void onReceive(Object msg) throws Exception {
        System.out.println(String.format("Received Message '%s' in Actor %s",
                msg, getSelf().path()));
    }
}


运行,

package com.usoft10;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;

public class Example {
    /**
     * @param args
     */
    public static void main(String[] args) throws InterruptedException {
        ActorSystem _system = ActorSystem.create("CustomRouterExample");
        ActorRef burstyMessageRouter = _system.actorOf(Props.create(
                MsgEchoActor.class).withRouter(new BurstyMessageRouter(3)), "MsgEchoActor");

        //sends series of messages in a broadcast way to all the actors
        burstyMessageRouter.tell("are you ready?", ActorRef.noSender());

        Thread.sleep(2000);
        _system.shutdown();
    }

}

运行结果,可以看到三个actor通过广播的router接收到了消息。。。

[INFO] [01/20/2015 14:58:51.828] [main] [Remoting] Starting remoting

[INFO] [01/20/2015 14:58:52.529] [main] [Remoting] Remoting started; listening on addresses :[akka.tcp://CustomRouterExample@127.0.0.1:2552]

[INFO] [01/20/2015 14:58:52.532] [main] [Remoting] Remoting now listens on addresses: [akka.tcp://CustomRouterExample@127.0.0.1:2552]

Received Message 'are you ready?' in Actor akka://CustomRouterExample/user/actor-0

Received Message 'are you ready?' in Actor akka://CustomRouterExample/user/actor-2

Received Message 'are you ready?' in Actor akka://CustomRouterExample/user/actor-1

[INFO] [01/20/2015 14:58:54.577] [CustomRouterExample-akka.remote.default-remote-dispatcher-6] [akka.tcp://CustomRouterExample@127.0.0.1:2552/system/remoting-terminator] Shutting down remote daemon.

[INFO] [01/20/2015 14:58:54.579] [CustomRouterExample-akka.remote.default-remote-dispatcher-6] [akka.tcp://CustomRouterExample@127.0.0.1:2552/system/remoting-terminator] Remote daemon shut down; proceeding with flushing remote transports.

[INFO] [01/20/2015 14:58:54.637] [ForkJoinPool-3-worker-7] [Remoting] Remoting shut down

[INFO] [01/20/2015 14:58:54.638] [CustomRouterExample-akka.remote.default-remote-dispatcher-6] [akka.tcp://CustomRouterExample@127.0.0.1:2552/system/remoting-terminator] Remoting shut down.


Process finished with exit code 0

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

转载于:https://my.oschina.net/xinxingegeya/blog/369721

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值