Akka学习笔记:Actor消息处理-请求和响应(1)

10 篇文章 0 订阅

在前面的文章《Akka学习笔记:Actor消息传递(1)》《Akka学习笔记:Actor消息传递(2)》。我们仅仅将消息发送到Actor,并没有期待从Actor发来的响应。
  从技术上讲,我们给Actor发送消息,除了没有响应,目标的Actor将对这条消息进行如下操作:
  1、给发送者发送一个响应,在我们的例子中,TeacherActor将发送一个quote 到StudentActor作为响应。或者
  2、给其他的Actor响应,而这个Actor可能也会作出响应或者转发等。Routers和Supervisors就是这个例子,我们将在后面看到。

请求和响应

  这里我们只研究场景1


如果想及时了解 Spark、Hadoop或者Hbase相关的文章,欢迎关注微信公共帐号: iteblog_hadoop

  上图传达了我们此刻想表达的意思。为了简单,我没有在图中画出ActorSystem、Dispatcher 和Mailboxes。
  1、DriverApp个StudentActor发送了一个InitSignal消息;
  2、StudentActor对InitSignal消息作出反应,并且发送了QuoteRequest 消息给TeacherActor;
  3、TeacherActor用QuoteResponse作出了响应;
  4、StudentActor仅仅将QuoteResponse 作为日志打印到控制台/logger。
  我们将在testcase中进行确认。让我们先来看看上面4点:

一、DriverApp个StudentActor发送了一个InitSignal消息


如果想及时了解 Spark、Hadoop或者Hbase相关的文章,欢迎关注微信公共帐号: iteblog_hadoop

  到现在为止,你可能已经猜到了DriverApp将要做的事,仅仅4件事
  1)、初始化ActorSystem

1 //Initialize the ActorSystem
2   val system = ActorSystem("UniversityMessageSystem")

  2)、创建TeacherActor

1 //create the teacher actor
2   val teacherRef = system.actorOf(Props[TeacherActor], "teacherActor")

  3)、创建StudentActor

1 //create the Student Actor - pass the teacher actorref
2 //as a constructor parameter to StudentActor
3   val studentRef = system.actorOf(Props(new StudentActor(teacherRef)), "studentActor")

  你可能注意到,我用TeacherActor 作为StudentActor构造参数,并传入到ActorRef 中,这样StudentActor 将可以用ActorRef发送消息到TeacherActor。还有其他的方法可以实现类似的功能(比如利用Props),但是当我们想查看 Supervisors 和Routers的时候,这个方法很方便。我们很快看到一个自Actors,但是在这里的语义不对:Student创建Teacher听起来很不好。不是吗?
  4)、最后,DriverApp将给StudentActor发送InitSignal。所以StudentActor可以给TeacherActor发送QuoteRequest 消息。

1 //send a message to the Student Actor
2   studentRef ! InitSignal

  这就是DriverClass所做的。完整的代码如下

01 package me.rerun.akkanotes.messaging.requestresponse
02  
03 import akka.actor.ActorSystem 
04 import akka.actor.Props 
05 import me.rerun.akkanotes.messaging.protocols.StudentProtocol._ 
06 import akka.actor.ActorRef
07  
08 object DriverApp extends App {
09  
10   //Initialize the ActorSystem
11   val system = ActorSystem("UniversityMessageSystem")
12  
13   //construct the teacher actor
14   val teacherRef = system.actorOf(Props[TeacherActor], "teacherActor")
15  
16   //construct the Student Actor - pass the teacher actorref
17  // as a constructor parameter to StudentActor
18   val studentRef = system.actorOf(Props(new StudentActor(teacherRef)), "studentActor")
19  
20   //send a message to the Student Actor
21   studentRef ! InitSignal
22  
23   //Let's wait for a couple of seconds before we shut down the system
24   Thread.sleep(2000)
25  
26   //Shut down the ActorSystem.
27   system.shutdown()
28  
29 }

本文英文地址:http://rerun.me/2014/10/06/akka-notes-actor-messaging-request-and-response-3/

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值