actor中!(tell)与forward的区别

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-1 18:44   25   0

! 的源码:

def !(message: Any)(implicit sender: ActorRef = Actor.noSender): Unit

tell 的源码:

final def tell(msg: Any, sender: ActorRef): Unit = this.!(msg)(sender)

forward 的源码:

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

通过源码,我们可以轻而易举的知道 :

! 发送的消息的发送者通过一个隐式转换,把一个ActorRef转换成sender,如果找不到ActorRef的话,那个发送者就是noSender。

forward 发送消息的发送者是当前消息的上一个发送者。

即:

A To B, B To C

如果通过!的方式发送消息,则C接收到的消息的发送者是B.

如果通过 forward 的方式发送消息,则C接收到的消息的发送者是A.

如果希望 !的方式发送消息的同时携带发送者消息,则可以使用 .!(msg)(sender)


如果希望关掉actor,直接shutdown或者使用父亲actor的stop,在actor内部还可以使用context.stop(self)

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP