Akka路由_RoundRobinRoutingLogicjava
使用Round Robin算法的Router,代碼中有註釋,基本和上篇文章中的代碼同樣算法
http://my.oschina.net/xinxingegeya/blog/369721,tcp
具體以下,關於Round Robin,請移步,http://my.oschina.net/xinxingegeya/blog/369781ide
下面是主要代碼,spa
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; } @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)))); } /** * 使用輪詢調度算法的router */ return new Router(new RoundRobinRoutingLogic(), routees); } }
啓動router,發出消息,.net
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 actors burstyMessageRouter.tell("are you ready?" + String.valueOf(i), ActorRef.noSender()); } Thread.sleep(2000); _system.shutdown(); } }
運行結果,code
[INFO] [01/20/2015 16:08:24.668] [main] [Remoting] Starting remotingrouter
[INFO] [01/20/2015 16:08:25.242] [main] [Remoting] Remoting started; listening on addresses :[akka.tcp://CustomRouterExample@127.0.0.1 :2552]blog
[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================