Akka Cluster - 来自死信的消息?

时间:2017-10-16 12:50:56

标签: scala akka actor dead-letter

我正在使用Akka Cluster,我有一个有趣的问题。我在节点A上有一个actor Actor1(akka.tcp://as@127.0.0.1:2554)。该actor使用

在另一个节点上找到另一个actor
val actor2sel = context.actorSelection(RootActorPath(m.address) / "user" / "actor2")

其中m是群集的成员。 actor2sel是

ActorSelection[Anchor(akka.tcp://as@127.0.0.1:2553/), Path(/user/actor2)]

稍后,Actor1会向Actor2转发一条消息,该消息正确获取消息,但发送者是deadLetters:

akka.tcp://as@127.0.0.1:2554/deadLetters

你有什么指示原因可能是什么?

1 个答案:

答案 0 :(得分:1)

如果消息链中至少有三个actor,则转发消息才有意义:

actor1 --[sends Messsage1]--> actor2 --[forwards Message1]--> actor3

actor3

def receive = {
  case Message1 =>
    sender ! Response1
}
鉴于上述消息链,上面的

sender是对actor1的引用。

如果只涉及两个参与者,则转发不是正确的工具:

actor1 --[forwards Message1]--> actor2

actor2中,如果从Message1转发actor1,而在消息链中没有“前一个”演员,则发件人将会死信:

def receive = {
  case Message1 =>
    sender ! Response1
    // sender is dead letters if there are only two actors in the forwarding chain 
}

如果Actor1在将该消息转发给Actor2之前没有收到来自另一个actor的消息,只需让Actor1向Actor2发送(!)消息而不是转发它。

如果Actor1在转发之前确实收到了来自另一个actor的消息,请确保在Actor2访问sender之前该“前一个”actor正在运行。