akka学习教程(八) Actor中的Future-询问模式

来源:互联网 发布:淘宝的全球购是真货吗 编辑:程序博客网 时间:2024/06/01 20:49

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());    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
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);        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
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);        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

输出结果:

[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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

其实这里和之前的文章中那样,在workerActor执行完毕以后,向printActor发消息不也行吗?

参考资料

  • 书籍《java高并发程序设计》
  • AKKA官方文档
原创粉丝点击