[翻译]AKKA笔记 - ACTOR MESSAGING - REQUEST AND RESPONSE -3
上次我们看Actor消息机制,我们看到开火-忘记型消息发出(意思是我们只要发个消息给Actor但是不期望有响应)。
技术上来讲, 我们发消息给Actors就是要它的副作用。 这就是这么设计的。除了不响应, 目标Actor当然也可以对消息做以下事情-
1.发送一个响应给发送者(在我们的case里,TeacherActor应该响应一首名言给StudentActor)
2.转发一个响应给其他的潜在的受众Actor,他们也可能响应/转发/产生副作用。Routers(路由)和Supervisors(监管)就是这个例子。(我们马上就能看到)
REQUEST & RESPONSE
这次,我们只关注第一点- 请求-响应的周期。

图片说明了我们这次在尝试归档做什么。为了简介,我没有在图中加入ActorSystem, Dispathcer或Mailboxes。
DriverApp发送一个InitSignal(初始化信号)消息给StudentActor.
StudentActor反馈给InitSignal消息并且发送一条QuoteRequest(格言请求)消息给TeacherActor。
TeacherActor,像我们在第一次看到的,响应一条QuoteResponse。
StudentActor只记录QuoteResponse(格言响应)到控制台/日志记录器。
我们可以写个testcase来验证。
让我们看下这4点的细节:
1. DRIVERAPP发送一个INITSIGNAL消息给STUDENTACTOR

至此,你可能已经猜出DriverApp会做什么。 就4件事:
1) 初始化ActorSystem
//Initialize the ActorSystem
val system = ActorSystem("UniversityMessageSystem")
- 创建TeacherActor
//create the teacher actor
val teacherRef = system.actorOf(Props[TeacherActor], "teacherActor")
- 创建StudentActor
//create the Student Actor - pass the teacher actorref as a constructor parameter to StudentActor
val studentRef = system.actorOf(Props(new StudentActor(teacherRef)), "studentActor")
你可以看到我把ActorRef的实例TeacherActor给StudentActor的构造函数,这样StudentActor可以使用ActorRef发消息给TeacherActor。还有另外的方式达到这个目的(如传入Props),但是这个方法对我们后面看到的Supervisors和Routers会方便点。我们也会即将看到child actors但现在还不是一个好方式 - Student创建Teacher听起来怪怪的吧?
最后,
4) DriverApp会发一个InitSignal给StudentActor, z何阳StudentActor就能开始发送QuoteRequest(格言请求)给TeacherActor。
//send a message to the Student Actor
studentRef ! InitSignal
这个DriverClass讲差不多了。 Thread.sleep和ActorSystem.shutdown在我们最后关闭ActorSystem的时候会在发消息时会等待几秒保证结束。
** DRIVERAPP.SCALA**
package me.rerun.akkanotes.messaging.requestresponse
import akka.actor.ActorSystem
import akka.actor.Props
import me.rerun.akkanotes.messaging.protocols.StudentProtocol._
import akka.actor.ActorRef
object DriverApp extends App {
//Initialize the ActorSystem
val system = ActorSystem("UniversityMessageSystem")
//construct the teacher actor
val teacherRef = system.actorOf(Props[TeacherActor], "teacherActor")
//construct the Student Actor - pass the teacher actorref as a constructor parameter to StudentActor
val studentRef = system.actorOf(Props(new StudentActor(teacherRef)), "studentActor")
//send a message to the Student Actor
studentRef ! InitSignal
//Let's wait for a couple of seconds before we shut down the system
Thread.sleep(2000)
//Shut down the ActorSystem.
system.shutdown()
}
2. STUDENTACTOR响应给INITSIGNAL消息并且发送一条QUOTEREQUEST消息给TEACHERACTOR###
以及
4. STUDENTACTOR从TEACHERACTOR接收到QUOTERESPONSE并且将日志记录到控制台/日志记录器###
为什么我把2和4条绑在一起?因为实在太简单了,要是拆开来你要恨死我。

所以,第二点 - StudentActor从DriverApp接到InitSignal消息并且发送QuoteRequest给TeacherActor。
def receive = {
case InitSignal=> {
teacherActorRef!QuoteRequest
}
...
...
就这么多!
第四点 - StudentActor记录从TeacherActor发来的日志消息。

就像我们承诺的:
case QuoteResponse(quoteString) => {
log.info ("Received QuoteResponse from Teacher")
log.info(s"Printing from Student Actor $quoteString")
}
我想你肯定同意现在这个看起来跟伪代码一样。
所以整个的StudentActor类看起来就是这样:
STUDENTACTOR.SCALA###
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 from Teacher")
log.info(s"Printing from Student Actor $quoteString")
}
}
}
3. TEACHERACTOR响应QUOTERESPONSE消息###
这就跟我们在fire-n-forget 那里写的代码差不多。
TeacherActor接受一条QuoteRequest消息并且返回一条QuoteResponse。
TEACHERACTOR.SCALA###
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 you think you have time",
"You never gonna know if you never even try")
def receive = {
case QuoteRequest => {
import util.Random
//Get a random Quote from 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
}
}
}
测试用例TESTCASES###
现在,我们的测试用例会模拟DriverApp。由于StudentActor只是记录消息,我们无法对QuoteResponse消息进行断言,我们只断言消息在EventStream里出现(就像我们之前
说的)
所以,我们的测试用例就像这样:
"A student" must {
"log a QuoteResponse eventually when an InitSignal is sent to it" in {
import me.rerun.akkanotes.messaging.protocols.StudentProtocol._
val teacherRef = system.actorOf(Props[TeacherActor], "teacherActor")
val studentRef = system.actorOf(Props(new StudentActor(teacherRef)), "studentActor")
EventFilter.info (start="Printing from Student Actor", occurrences=1).intercept{
studentRef!InitSignal
}
}
}
源码###
整个工程可以在github下载。
下一步,我们会看到怎样使用Akka的schedulers并且用Kamon监控你的Akka应用。
文章来自微信平台「麦芽面包」,微信号「darkjune_think」。转载请注明。
[翻译]AKKA笔记 - ACTOR MESSAGING - REQUEST AND RESPONSE -3的更多相关文章
- [翻译]AKKA笔记 -ACTOR SUPERVISION - 8
失败更像是分布式系统的一个特性.因此Akka用一个容忍失败的模型,在你的业务逻辑与失败处理逻辑(supervision逻辑)中间你能有一个清晰的边界.只需要一点点工作,这很赞.这就是我们要讨论的主题. ...
- [翻译]AKKA笔记 - ACTOR生命周期 - 基本 -5
原文地址:http://rerun.me/2014/10/21/akka-notes-actor-lifecycle-basic/ (请注意这了讨论的生命周期并不包括 preRestart 或者pos ...
- 翻译:AKKA笔记 - Actor消息 -1(一)
从第一篇Akka笔记的介绍中,我们是从很高的高度去观察Akka工具箱中的Actors.在这篇笔记的第二篇,我们会看一下Actors中的消息部分.而且延续上一次的例子,我们还会使用同样的学生与老师的例子 ...
- 翻译:AKKA笔记 - Actor消息 -1(二)
消息 我们只是让QuoteRequest到ActorRef去但是我们根本没见过消息类! 它是这样的: (一个最佳实践是把你的消息类包装在一个完整的对象里以利于更好的组织) TeacherProtoco ...
- [翻译]AKKA笔记 - DEATHWATCH -7
当我们说Actor生命周期的时候,我们能看到Actor能被很多种方式停掉(用ActorSystem.stop或ActorContext.stop或发送一个PoisonPill - 也有一个kill和g ...
- [翻译] AKKA笔记- ACTORSYSTEM (配置CONFIGURATION 与调度SCHEDULING) - 4(一)
原文在http://rerun.me/2014/10/06/akka-notes-actorsystem-in-progress/ 像我们前面看到的,我们可以用ActorSystem的actorof方 ...
- [翻译]AKKA笔记 - LOGGING与测试ACTORS -2 (一)
在前两章 ( 一 , 二 ) ,我们大致讲了Actor和message是怎么工作的,让我们看一下日志和测试我们的 TeacherActor . RECAP 这是上一节我们的Actor代码: class ...
- [翻译]AKKA笔记 - 有限状态机 -1
原文地址:http://rerun.me/2016/05/21/akka-notes-finite-state-machines-1/ 我最近有个机会在工作上使用了Akka FSM,是个非常有趣的例子 ...
- [翻译]AKKA笔记 - CHILD ACTORS与ACTORPATH -6
原文:http://rerun.me/2014/10/21/akka-notes-child-actors-and-path/ Actor是完全的继承结构.你创建的任何Actor肯定都是一个其他Act ...
随机推荐
- CSV的导入导出
using System; using System.Data; using System.IO; namespace COMMON { public class CSVhelperClass { / ...
- Layout in Rails
参考链接:http://guides.rubyonrails.org/layouts_and_rendering.html#structuring-layouts layout layout最基本的使 ...
- Codeforces Round #379 (Div. 2) Analyses By Team:Red & Black
A.Anton and Danik Problems: 给你长度为N的,只含'A','D'的序列,统计并输出何者出现的较多,相同为"Friendship" Analysis: lu ...
- 健忘vs总结
上周入职新公司,报道之前自己也曾想过要从头开始,用一个新的精神面貌来迎接新的起点,培养一些新的习惯. 周四是15日,新公司的发薪日(当然还没有我的份~),小组群内一个刚毕业的新人兴冲冲的说终于领到第一 ...
- response.setHeader()的用法
一秒刷新页面一次 response.setHeader("refresh","1"); 二秒跳到其他页面 response.setHeader("re ...
- eclipse 搭建Swt 环境
我本是想用java开发一个记事本,开发记事本使用到SWT插件,我从网上找了许多的资料去集成插件,创建我的第一个SWT项目,以下是我搭建SWT环境的过程. 一.查看当前使用的exlipse 版本型号 在 ...
- 机器学习利器——Scikit-learn的安装
由于笔者最近在进行毕业论文的准备,且毕业论文中需要用到Python版本的机器学习库——scikit-learn.所以最近三天一直在Windows上部署这个框架,终于部署成功了... 首先打开加州大学底 ...
- 20151012 C# 第一篇 字符与字符串
20151012 字符与字符串: Char.String等类来表示 字符类Char 1. 字符类Char 表示一个 Unicode 字符,(Unicode字符是计算机通用的字符编码,对不同语言中的每个 ...
- Tomcat中使用JNDI加载JDBC数据源
以前写JDBC的时候总是手工写一个类,用硬代码写上className.url.用户名和密码什么的,然后通过DriverManager获取到Connection.那样写是很方便,但是如果想更改的话,需要 ...
- 零值初始化&字符串常数作为函数模板参数
1.在定义一个局部变量时,并希望该局部变量的初始化一个值,可以显示调用其默认构造函数,使其值为0(bool类型默认值为false). template <typename T> void ...