2019独角兽企业重金招聘Python工程师标准>>>
Akka路由_RoundRobinRoutingLogic
使用Round Robin算法的Router,代码中有注释,基本和上篇文章中的代码一样
http://my.oschina.net/xinxingegeya/blog/369721,
具体如下,关于Round Robin,请移步,http://my.oschina.net/xinxingegeya/blog/369781
下面是主要代码,
package com.usoft10;import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.routing.ActorRefRoutee;
import akka.routing.CustomRouterConfig;
import akka.routing.RoundRobinRoutingLogic;
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;}@Overridepublic 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))));}/*** 使用轮询调度算法的router*/return new Router(new RoundRobinRoutingLogic(), routees);}
}
启动router,发出消息,
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(5)), "MsgEchoActor");/*** 在这里模拟发出10个消息,使用RoundRobinRoutingLogic的router会轮询调度每个actor接收消息* 也就是说每次tell,router只会选择其中一个actor进行消息的响应*/for (int i = 1; i <= 10; i++) {//sends series of messages in a round robin way to all the actorsburstyMessageRouter.tell("are you ready?" + String.valueOf(i), ActorRef.noSender());}Thread.sleep(2000);_system.shutdown();}}
运行结果,
[INFO] [01/20/2015 16:08:24.668] [main] [Remoting] Starting remoting
[INFO] [01/20/2015 16:08:25.242] [main] [Remoting] Remoting started; listening on addresses :[akka.tcp://CustomRouterExample@127.0.0.1 :2552]
[INFO] [01/20/2015 16:08:25.246] [main] [Remoting] Remoting now listens on addresses: [akka.tcp://CustomRouterExample@127.0.0.1 :2552]
Received Message 'are you ready?1' in Actor akka://CustomRouterExample/user/actor-0
Received Message 'are you ready?6' in Actor akka://CustomRouterExample/user/actor-0
Received Message 'are you ready?2' in Actor akka://CustomRouterExample/user/actor-1
Received Message 'are you ready?7' in Actor akka://CustomRouterExample/user/actor-1
Received Message 'are you ready?3' in Actor akka://CustomRouterExample/user/actor-2
Received Message 'are you ready?8' in Actor akka://CustomRouterExample/user/actor-2
Received Message 'are you ready?4' in Actor akka://CustomRouterExample/user/actor-3
Received Message 'are you ready?9' in Actor akka://CustomRouterExample/user/actor-3
Received Message 'are you ready?5' in Actor akka://CustomRouterExample/user/actor-4
Received Message 'are you ready?10' in Actor akka://CustomRouterExample/user/actor-4
[INFO] [01/20/2015 16:08:27.294] [CustomRouterExample-akka.remote.default-remote-dispatcher-8] [akka.tcp://CustomRouterExample@127.0.0.1 :2552/system/remoting-terminator] Shutting down remote daemon.
[INFO] [01/20/2015 16:08:27.297] [CustomRouterExample-akka.remote.default-remote-dispatcher-8] [akka.tcp://CustomRouterExample@127.0.0.1 :2552/system/remoting-terminator] Remote daemon shut down; proceeding with flushing remote transports.
[INFO] [01/20/2015 16:08:27.348] [ForkJoinPool-3-worker-7] [Remoting] Remoting shut down
[INFO] [01/20/2015 16:08:27.348] [CustomRouterExample-akka.remote.default-remote-dispatcher-7] [akka.tcp://CustomRouterExample@127.0.0.1 :2552/system/remoting-terminator] Remoting shut down.
Process finished with exit code 0
================END================