Akka学习笔记:Actor消息处理-请求和响应(2)
2015-01-18 21:10
507 查看
接《Akka学习笔记:Actor消息处理-请求和响应(1)》
如果想及时了解Spark、Hadoop或者Hbase相关的文章,欢迎关注微信公共帐号:iteblog_hadoop
第二块 – StudentActor接收来自DriverApp的InitSignal 消息,然后给TeacherActor发送一个QuoteRequest。
第四块,StudentActor将从TeacherActor接收到的消息记到日志里面
如果想及时了解Spark、Hadoop或者Hbase相关的文章,欢迎关注微信公共帐号:iteblog_hadoop
代码如下:
二、StudentActor对InitSignal消息作出反应,并且发送了QuoteRequest 消息给TeacherActor
四、StudentActor仅仅将QuoteResponse 作为日志打印到控制台/logger
为什么我将二、四结合起来写?因为这两个很简单,如果我将他们分开来写,你肯定会讨厌我的!如果想及时了解Spark、Hadoop或者Hbase相关的文章,欢迎关注微信公共帐号:iteblog_hadoop
第二块 – StudentActor接收来自DriverApp的InitSignal 消息,然后给TeacherActor发送一个QuoteRequest。
def receive = { case InitSignal=> { teacherActorRef!QuoteRequest } ... ...这就是第二步要做的!
第四块,StudentActor将从TeacherActor接收到的消息记到日志里面
如果想及时了解Spark、Hadoop或者Hbase相关的文章,欢迎关注微信公共帐号:iteblog_hadoop
代码如下:
case QuoteResponse(quoteString) => { log.info ("Received QuoteResponse from Teacher") log.info(s"Printing from Student Actor $quoteString") }我同意你认为上面的代码看起来有点想伪代码。所以,StudentActor 的完整代码如下:
package me.rerun.akkanotes.messaging.requestresponse import akka.actor.Actor import akka.actor.ActorLogging import me.rerun.akkanotes.messaging.protocols.TeacherProtocol._ import me.rerun.akkanotes.messaging.protocols.StudentProtocol._ import akka.actor.Props import akka.actor.ActorRef class StudentActor (teacherActorRef:ActorRef) extends Actor with ActorLogging { def receive = { case InitSignal=> { teacherActorRef!QuoteRequest } case QuoteResponse(quoteString) => { log.info ("Received QuoteResponse <span id="5_nwp" style="width: auto; height: auto; float: none;"><a id="5_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2Eiteblog%2Ecom%2Farchives%2F1163&p=baidu&c=news&n=10&t=tpclicked3_hc&q=56075110_cpr&k=from&k0=printing&kdi0=8&k1=from&kdi1=8&k2=new&kdi2=8&k3=you&kdi3=8&k4=think&kdi4=8&sid=f729d76689a2b130&ch=0&tu=u1887734&jk=325f1da3f0be6e4f&cf=29&fv=16&stid=9&urlid=0&luki=2&seller_id=1&di=128" target="_blank" mpid="5" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">from</span></a></span> Teacher") log.info(s"Printing from Student Actor $quoteString") } } }
三、TeacherActor用QuoteResponse作出了响应
TeacherActor接收到了QuoteRequest消息,并且用QuoteResponse 作为响应package me.rerun.akkanotes.messaging.requestresponse import scala.util.Random import akka.actor.Actor import akka.actor.ActorLogging import akka.actor.actorRef2Scala import me.rerun.akkanotes.messaging.protocols.TeacherProtocol._ class TeacherActor extends Actor with ActorLogging { val quotes = List( "Moderation is for cowards", "Anything worth doing is worth overdoing", "The trouble is <span id="2_nwp" style="width: auto; height: auto; float: none;"><a id="2_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2Eiteblog%2Ecom%2Farchives%2F1163&p=baidu&c=news&n=10&t=tpclicked3_hc&q=56075110_cpr&k=you&k0=printing&kdi0=8&k1=from&kdi1=8&k2=new&kdi2=8&k3=you&kdi3=8&k4=think&kdi4=8&sid=f729d76689a2b130&ch=0&tu=u1887734&jk=325f1da3f0be6e4f&cf=29&fv=16&stid=9&urlid=0&luki=4&seller_id=1&di=128" target="_blank" mpid="2" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">you</span></a></span> <span id="3_nwp" style="width: auto; height: auto; float: none;"><a id="3_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2Eiteblog%2Ecom%2Farchives%2F1163&p=baidu&c=news&n=10&t=tpclicked3_hc&q=56075110_cpr&k=think&k0=printing&kdi0=8&k1=from&kdi1=8&k2=new&kdi2=8&k3=you&kdi3=8&k4=think&kdi4=8&sid=f729d76689a2b130&ch=0&tu=u1887734&jk=325f1da3f0be6e4f&cf=29&fv=16&stid=9&urlid=0&luki=5&seller_id=1&di=128" target="_blank" mpid="3" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">think</span></a></span> you have time", "You never gonna know if you never even try") def receive = { case QuoteRequest => { import util.Random //Get a random Quote <span id="4_nwp" style="width: auto; height: auto; float: none;"><a id="4_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2Eiteblog%2Ecom%2Farchives%2F1163&p=baidu&c=news&n=10&t=tpclicked3_hc&q=56075110_cpr&k=from&k0=printing&kdi0=8&k1=from&kdi1=8&k2=new&kdi2=8&k3=you&kdi3=8&k4=think&kdi4=8&sid=f729d76689a2b130&ch=0&tu=u1887734&jk=325f1da3f0be6e4f&cf=29&fv=16&stid=9&urlid=0&luki=2&seller_id=1&di=128" target="_blank" mpid="4" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">from</span></a></span> the list and construct a response val quoteResponse = QuoteResponse(quotes(Random.nextInt(quotes.size))) //respond back to the Student who is the original sender of QuoteRequest sender ! quoteResponse } } }
相关文章推荐
- Akka学习笔记:Actor消息处理-请求和响应(1)
- Akka学习笔记:Actor消息处理-请求和响应(2)
- Akka学习笔记:Actor消息处理-请求和响应(1)
- 第108讲:Akka中的Actor消息的处理、消息回复、消息转发学习笔记
- 黑马程序员_学习笔记24_请求处理响应图解
- 第111讲:Akka中的Actor中用become和unbecome动态切换receive的具体处理逻辑实战学习笔记
- DXUT 学习笔记(3)对键盘鼠标的响应 及 消息处理
- Akka学习笔记:Actor消息传递(1)
- Akka学习笔记:Actor消息传递(2)
- Akka学习笔记:Actor消息传递(2)
- VC++学习笔记之消息处理机制和窗口过程函数
- ASP.NET 3.5核心编程学习笔记(1):ASP.Net页面请求处理流程
- Android应用开发学习笔记之多线程与Handler消息处理机制
- php学习笔记(三十)ajax请求和接收参数的实现方式(包括json数据格式的简单处理)
- Android应用开发学习笔记之多线程与Handler消息处理机制
- live555学习之RTSP连接建立以及请求消息处理过程
- Socket网络编程学习笔记(4):TCP消息边界处理
- live555学习之RTSP连接建立以及请求消息处理过程
- Win32学习笔记——消息处理
- nodejs 学习笔记---非阻塞请求响应