在 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。
最佳答案
Actor B 是否必须更改 C 和 A 之间的回复消息?
B forward "msg"
而不是 B ! "msg"
.这将保留发件人信息。当 C 使用回复时,回复自动发送给 A,不经过 B:A sends message to B (b ! "msg")
B forward the message to C (c forward "msg")
C replies to A (self.reply ! "msg3")
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]
.继续前进时,您将发送者引用放在前面。返回时,您回复列表头并在回复中传递列表尾。编辑:考虑到维克多的评论。
关于斯卡拉 Akka : have actor remember something it should eventually reply to?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6567267/