scala akka:演员是否记得它最终应该回复的内容?
在akka中,假设有一个线性的actor链,每个actor从上游接收消息,向下游发送自己的消息并等待回复,然后向上游发送回消息。当actor必须稍后回复上游actor时,它如何记住上游actor的句柄?
例如:
A sends message to B (b ! "msg1")
B sends message to C (c ! "msg2")
C replies to B (self.reply ! "msg3")
B replies to A <--- ???
基本上,B 如何记住 A 的句柄?此时执行 self.reply 将引用 C,因为 C 将当前消息发送给 B。
in akka suppose there is a linear chain of actors such that each actor receives a message from upstream, sends its own message downstream and waits for a reply, and then sends a message back upstream. how can the actor remember the handle of the upstream actor when it has to reply to that actor later?
for example:
A sends message to B (b ! "msg1")
B sends message to C (c ! "msg2")
C replies to B (self.reply ! "msg3")
B replies to A <--- ???
basically, how can B remember a handle for A? doing self.reply at this point would refer to C, since C sent the current message to B.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
参与者B必须更改C和A之间的回复消息吗?
如果没有,参与者B应该使用
B转发“msg”
而不是B! “消息”。这将保留发件人信息。当C使用回复时,答案会自动发送给A,而不经过B:
A 向 B 发送消息(b !“msg”)
B将消息转发给C(c转发“msg”)
C 回复 A (self.reply !"msg3")
如果是,则获取发件人 < code>ActorRef 并将其传递消息发送者引用。
例如,使用一个简单的元组:
这里我使用了一个元组,但如果您有更长的链,请传递一个
List[ActorRef]
。继续前进时,您可以在前面添加发件人参考。当返回时,您回复列表头并沿着回复传递列表尾。编辑:考虑维克多的评论。
Must actor B change the reply message between C and A ?
If not, actor B should use
B forward "msg"
instead ofB ! "msg"
. That will preserve the sender information. When C use reply, the answer is automatically sent to A without passing by B:A sends message to B (b ! "msg")
B forward the message to C (c forward "msg")
C replies to A (self.reply ! "msg3")
If yes, get the sender
ActorRef
and pass it along the message sender reference.For example, using a simple tuple:
Here I used a Tuple, but if you have a longer chain, pass a
List[ActorRef]
. When going forward, you prepend senders refs. And when going back, you reply to the list head and pass the list tail along the reply.Edit: Taking Victor comment into account.