scala akka:演员是否记得它最终应该回复的内容?

发布于 2024-11-18 11:06:09 字数 342 浏览 4 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2024-11-25 11:06:09

参与者B必须更改CA之间的回复消息吗?

  1. 如果没有,参与者B应该使用B转发“msg”而不是B! “消息”。这将保留发件人信息。当C使用回复时,答案会自动发送给A,而不经过B:

    A 向 B 发送消息(b !“msg”)

    B将消息转发给C(c转发“msg”)

    C 回复 A (self.reply !"msg3")

  2. 如果是,则获取发件人 < code>ActorRef 并将其传递消息发送者引用。

例如,使用一个简单的元组:

A sends message to B (b ! "msg1")
B sends message to C, with the sender reference (c ! ("msg2",self.sender) )
C replies to B adding the reference it received (self.reply ! ("msg3",ref) )
B replies to A ( ref ! "msg4" )

这里我使用了一个元组,但如果您有更长的链,请传递一个List[ActorRef]。继续前进时,您可以在前面添加发件人参考。当返回时,您回复列表头并沿着回复传递列表尾。

编辑:考虑维克多的评论。

Must actor B change the reply message between C and A ?

  1. If not, actor B should use B forward "msg" instead of B ! "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")

  2. If yes, get the sender ActorRef and pass it along the message sender reference.

For example, using a simple tuple:

A sends message to B (b ! "msg1")
B sends message to C, with the sender reference (c ! ("msg2",self.sender) )
C replies to B adding the reference it received (self.reply ! ("msg3",ref) )
B replies to A ( ref ! "msg4" )

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文