Akka路由_RoundRobinRoutingLogic

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;
    }

    @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,发出消息,

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();
    }

}

运行结果,

[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================

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值