Akka路由_RoundRobinRoutingLogic
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================
转载于:https://my.oschina.net/xinxingegeya/blog/369785
Akka路由_RoundRobinRoutingLogic相关推荐
- akka 简介_Akka HTTP路由简介
akka 简介 by Miguel Lopez 由Miguel Lopez Akka HTTP路由简介 (An introduction to Akka HTTP routing) Akka HTTP ...
- akka之分发和路由
一 Dispatchers 1.1 理解分发器 分发器是一个通信协调员的角色,主要负责消息的接收和传递.主要依据一定的分发策略,用于控制执行流程,然后将到来的消息或者请求路由给相关的业务进程. 提供飞 ...
- AKKA Router路由
路由概念 大量的actor在并行工作的时候,处理到来的消息流,这时候就需要一个组件或者东西来引导消息从源到目的地Actor,这个组件或者东西就是Router 在Akka中,router也是一种acto ...
- Akka in JAVA(三)
2019独角兽企业重金招聘Python工程师标准>>> Akka in JAVA(三) 上两个部分讲了Akka的基本知识和常见的用法.接下来讲一讲Akka的远程调用以及集群的使用.因 ...
- quasar_Quasar和Akka –比较
quasar actor模型是用于容错和高度可扩展系统的设计模式. 角色是独立的工作程序模块,它们仅通过消息传递与其他角色进行通信,可以与其他角色隔离而失败,但是可以监视其他角色的故障并在发生这种情况 ...
- akka框架——异步非阻塞高并发处理框架
akka actor, akka cluster akka是一系列框架,包括akka-actor, akka-remote, akka-cluster, akka-stream等,分别具有高并发处理模 ...
- [转] AKKA简介
[From] https://blog.csdn.net/linuxarmsummary/article/details/79399602 Akka in JAVA(一) AKKA简介 什么是AKKA ...
- Spray + Akka高性能异步IO并发
http://ju.outofmemory.cn/entry/70913 http://www.jdon.com/concurrent/spray-akka.html 如何使用Java建立像No ...
- AKKA文档(java版)——准备开始
http://ifeve.com/akka-doc-java-getting-started/ AKKA文档(java版)--准备开始 原文:http://doc.akka.io/docs/ak ...
最新文章
- vPower系列- 前言
- 天润融通java面试_【天润融通面试|面试题】-看准网
- 功率增长步长(powerRampingStep)
- 如何加快Vivado的编译速度
- C++中的继承(三)
- html 页面工具,html页面工具-htmlUnit
- java jar包示例_Java包getImplementationTitle()方法和示例
- 【软件工程】集成开发
- Deno 冲上榜首,Vue.js 首次屈居第二,JavaScript 2020 年度“新起之秀”都有谁?
- python统计合格数_python—基本统计值计算
- 【bat】一个脚本文件,关闭IE,重置IE,配置IE,设置IE的ActionX等选项.并自动管理员身份运行
- Docker概述(一)(标贝科技)
- 广告法违禁词替换工具_广告法违禁词、敏感词检测工具
- 网页去广告服务器,使用 AdGuardHome,实现网页加速和去广告
- 【免费域名】freenom免费申请域名步骤
- 联想拯救者Y9000P 2022 配置
- DFS和BFS求字符串的所有非空子集———Java
- windows文件隐藏之谜
- LeetCode LCP 19 秋叶收藏集 HERODING的LeetCode之路
- 统信软件根社区斩获CSDN两项大奖