您的位置:首页 > 其它

第108讲:Akka中的Actor消息的处理、消息回复、消息转发学习笔记

2015-09-26 21:33 483 查看
第108讲:Akka中的Actor消息的处理、消息回复、消息转发学习笔记

本讲主要讲解:

1.Actor消息的处理

2.Actor消息的回复

3.Actor消息的转发

处理消息一般通过复写receive方法,通过模式匹配的方式。

/**

* This defines the initial actor behavior, it must return a partial function

* with the actor logic.

*/

Protected def receive: Receive

如果要回复消息给发送者的话要获得发送者的actorRef,

就是说代理的句柄。Akka中提供了sender这个ActorRef指向当前收到的消息的发送者。但这有一个风险:消息到当前actor后,它的发送者已不存在了,这时给sender回复消息时,此时的消息就会变成deadLetters。

/**

* The reference sender Actor of the last received message.

* Is defined if the message was sent from another Actor,

* else ‘deadLetters’ in [[akka.actor.ActorSystem]].

*/

Final def sender: ActorRef = context.sender

消息的转发:

Forward的方式。

mapActor.forward(message)

Forward的源码:

/**

* Forwards the message and passer the original sender actor as the sender.

* Works with ! And ?/ask

Def forward(message: Any)(implicit context: ActorContext) = tell(message,context.sender)

以上内容是从王家林老师DT大数据课程第108讲的学习笔记。

DT大数据微信公众账号:DT_Spark

王家林老师QQ:1740415547

王家林老师微信号:18610086859

DT大数据梦工厂1至110集scala的所有视频、PPT和代码在百度云盘的链接:http://pan.baidu.com/share/home?uk=4013289088#category/type=0&qq-pf-to=pcqq.group

第108讲视频网站地址:

酷6网

http://v.ku6.com/show/SIfOF_-ZPl3jdllLZy6fZg...html
51CTO

http://edu.51cto.com/lesson/id-76214.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: