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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值