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

10 篇文章 0 订阅

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

二、StudentActor对InitSignal消息作出反应,并且发送了QuoteRequest 消息给TeacherActor

四、StudentActor仅仅将QuoteResponse 作为日志打印到控制台/logger

  为什么我将二、四结合起来写?因为这两个很简单,如果我将他们分开来写,你肯定会讨厌我的!


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

  第二块 - StudentActor接收来自DriverApp的InitSignal 消息,然后给TeacherActor发送一个QuoteRequest。

1 def receive =
2     case InitSignal=> {
3           teacherActorRef!QuoteRequest
4     }
5     ...
6     ...

  这就是第二步要做的!
  第四块,StudentActor将从TeacherActor接收到的消息记到日志里面

<a href="http://source.iteblog.com/pic/Student.png?imageMogr2/quality/75!" target="_blank" data-lightbox="image-1>
如果想及时了解 Spark、Hadoop或者Hbase相关的文章,欢迎关注微信公共帐号: iteblog_hadoop

代码如下:

1 case QuoteResponse(quoteString) => { 
2       log.info ("Received QuoteResponse from Teacher")
3       log.info(s"Printing from Student Actor $quoteString")
4 }

  我同意你认为上面的代码看起来有点想伪代码。所以,StudentActor 的完整代码如下:

01 package me.rerun.akkanotes.messaging.requestresponse
02  
03 import akka.actor.Actor 
04 import akka.actor.ActorLogging 
05 import me.rerun.akkanotes.messaging.protocols.TeacherProtocol._ 
06 import me.rerun.akkanotes.messaging.protocols.StudentProtocol._ 
07 import akka.actor.Props 
08 import akka.actor.ActorRef
09  
10 class StudentActor (teacherActorRef:ActorRef) extends Actor with ActorLogging {
11  
12   def receive = {
13     case InitSignal=> {
14       teacherActorRef!QuoteRequest
15     }
16  
17     case QuoteResponse(quoteString) => {
18       log.info ("Received QuoteResponse from Teacher")
19       log.info(s"Printing from Student Actor $quoteString")
20     }
21   }
22 }

三、TeacherActor用QuoteResponse作出了响应

  TeacherActor接收到了QuoteRequest消息,并且用QuoteResponse 作为响应

01 package me.rerun.akkanotes.messaging.requestresponse
02  
03 import scala.util.Random
04  
05 import akka.actor.Actor 
06 import akka.actor.ActorLogging 
07 import akka.actor.actorRef2Scala 
08 import me.rerun.akkanotes.messaging.protocols.TeacherProtocol._
09  
10  
11 class TeacherActor extends Actor with ActorLogging {
12  
13   val quotes = List(
14     "Moderation is for cowards",
15     "Anything worth doing is worth overdoing",
16     "The trouble is you think you have time",
17     "You never gonna know if you never even try")
18  
19   def receive = {
20  
21     case QuoteRequest => {
22  
23       import util.Random
24  
25       //Get a random Quote from the list and construct a response
26       val quoteResponse = QuoteResponse(quotes(Random.nextInt(quotes.size)))
27  
28       //respond back to the Student who is the original sender of QuoteRequest
29       sender ! quoteResponse
30  
31     }
32   }
33 }

测试用例

现在,我们的测试用例将模拟DriverApp。然而StudentActor仅仅是把消息记录下来,它不会在QuoteResponse 进行检查。我们仅仅在EventStream 查看这些日志。我们的测试用例看起来如下:

01 "A student" must {
02  
03     "log a QuoteResponse eventually when an InitSignal is sent to it" in {
04  
05       import me.rerun.akkanotes.messaging.protocols.StudentProtocol._
06  
07       val teacherRef = system.actorOf(Props[TeacherActor], "teacherActor")
08       val studentRef = system.actorOf(Props(new StudentActor(teacherRef)), "studentActor")
09  
10       EventFilter.info (start="Printing from Student Actor", occurrences=1).intercept{
11         studentRef!InitSignal
12       }
13     }
14   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值