akka学习教程(八) Actor中的Future-询问模式
来源:互联网 发布:mac照片应用储存位置 编辑:程序博客网 时间:2024/06/06 14:26
akka系列文章目录
- akka学习教程(十四) akka分布式实战
- akka学习教程(十三) akka分布式
- akka学习教程(十二) Spring与Akka的集成
- akka学习教程(十一) akka持久化
- akka学习教程(十) agent
- akka学习教程(九) STM软件事务内存
- akka学习教程(八) Actor中的Future-询问模式
- akka学习教程(七) 内置状态转换Procedure
- akka学习教程(六) 路由器Router
- akka学习教程(五) inbox消息收件箱
- akka学习教程(四) actor生命周期
- akka学习教程(三) 不可变对象
- akka学习教程(二)HelloWord
- akka学习教程(一)简介
和java线程中的future挺像的,可以将一个actor的返回结果重定向到另一个actor中进行处理,主actor或者进程无需等待actor的返回结果。
package akka.future;import akka.actor.ActorRef;import akka.actor.ActorSystem;import akka.actor.PoisonPill;import akka.actor.Props;import akka.pattern.Patterns;import com.typesafe.config.ConfigFactory;import scala.concurrent.Await;import scala.concurrent.Future;import scala.concurrent.duration.Duration;import java.util.concurrent.TimeUnit;/** * Created by liubenlong on 2017/1/16. */public class AskMain { public static void main(String[] args) throws Exception { ActorSystem system = ActorSystem.create("strategy", ConfigFactory.load("akka.config")); ActorRef printActor = system.actorOf(Props.create(PrintActor.class), "PrintActor"); ActorRef workerActor = system.actorOf(Props.create(WorkerActor.class), "WorkerActor"); //等等future返回 Future<Object> future = Patterns.ask(workerActor, 5, 1000); int result = (int) Await.result(future, Duration.create(3, TimeUnit.SECONDS)); System.out.println("result:" + result); //不等待返回值,直接重定向到其他actor,有返回值来的时候将会重定向到printActor Future<Object> future1 = Patterns.ask(workerActor, 8, 1000); Patterns.pipe(future1, system.dispatcher()).to(printActor); workerActor.tell(PoisonPill.getInstance(), ActorRef.noSender()); }}
package akka.future;import akka.actor.UntypedActor;import akka.event.Logging;import akka.event.LoggingAdapter;/** * Created by liubenlong on 2017/1/12. */public class PrintActor extends UntypedActor { private final LoggingAdapter log = Logging.getLogger(getContext().system(), this); @Override public void onReceive(Object o) throws Throwable { log.info("akka.future.PrintActor.onReceive:" + o); if (o instanceof Integer) { log.info("print:" + o); } else { unhandled(o); } }}
package akka.future;import akka.actor.*;import akka.event.Logging;import akka.event.LoggingAdapter;/** * Created by liubenlong on 2017/1/12. */public class WorkerActor extends UntypedActor { private final LoggingAdapter log = Logging.getLogger(getContext().system(), this); @Override public void onReceive(Object o) throws Throwable { log.info("akka.future.WorkerActor.onReceive:" + o); if (o instanceof Integer) { Thread.sleep(1000); int i = Integer.parseInt(o.toString()); getSender().tell(i*i, getSelf()); } else { unhandled(o); } }}
输出结果:
[INFO] [01/16/2017 16:22:32.983] [strategy-akka.actor.default-dispatcher-5] [akka://strategy/user/WorkerActor] akka.future.WorkerActor.onReceive:5result:25[INFO] [01/16/2017 16:22:33.984] [strategy-akka.actor.default-dispatcher-5] [akka://strategy/user/WorkerActor] akka.future.WorkerActor.onReceive:8[INFO] [01/16/2017 16:22:34.997] [strategy-akka.actor.default-dispatcher-6] [akka://strategy/user/PrintActor] akka.future.PrintActor.onReceive:64[INFO] [01/16/2017 16:22:34.997] [strategy-akka.actor.default-dispatcher-6] [akka://strategy/user/PrintActor] print:64
其实这里和之前的文章中那样,在workerActor执行完毕以后,向printActor发消息不也行吗?
参考资料
- 书籍《java高并发程序设计》
- AKKA官方文档
0 0
- akka学习教程(八) Actor中的Future-询问模式
- akka学习教程(八) Actor中的Future-询问模式
- 《Java高并发程序设计》学习 --7.9 询问模式:Actor中的Future
- akka学习教程(四) actor生命周期
- java中的Actor模式 Akka实例
- 学习akka之future
- 第102讲:通过案例解析Akka中的Actor运行机制以及Actor的生命周期学习笔记
- Akka actor
- Scala---Akka Actor(二)actor创建以及Ask模式
- Akka学习笔记(二):Actor Systems
- Akka学习笔记(三):什么是Actor
- Akka学习笔记04--Actor生命周期
- Akka学习笔记05--Actor的创建
- Akka学习笔记06--Actor的消息
- Akka学习笔记04--Actor生命周期
- Akka学习笔记05--Actor的创建
- Akka学习笔记06--Actor的消息
- Akka学习笔记(3)-Actor
- Morphia快速入门
- 简单模拟 CodeForces - 404B Marathon
- 自定义view-仿支付宝芝麻分圆盘
- 加快构建P2P洗钱风险防控体系
- Java实现通过poi把数据查出来放在模板Excel中并作为附件发送邮件
- akka学习教程(八) Actor中的Future-询问模式
- python2X和python3X中,”/“和”//“的区别
- Java Executor 框架详细讲解
- 全选与非全选
- linux ssh无密码登录配置
- malloc与new
- java设计模式——状态设计模式
- icomoon 制造字体图标
- 学生信息管理系统