Search code examples
scalaakkaactordead-letter

Akka Cluster - message coming from dead letters?


I am using Akka Cluster and I have an interesting issue. I have one actor Actor1 on node A (akka.tcp://[email protected]:2554). This actor finds another actor on another node using

val actor2sel = context.actorSelection(RootActorPath(m.address) / "user" / "actor2")

where m is a member of the cluster. actor2sel is

ActorSelection[Anchor(akka.tcp://[email protected]:2553/), Path(/user/actor2)]

Later, Actor1 forwards a message to Actor2, which correctly gets the message, but the sender is deadLetters:

akka.tcp://[email protected]:2554/deadLetters

Do you have any pointer on what the cause might be?


Solution

  • Forwarding a message only makes sense if there are at least three actors in the message chain:

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

    In actor3:

    def receive = {
      case Message1 =>
        sender ! Response1
    }
    

    sender above is a reference to actor1, given the above message chain.

    If there are only two actors involved, forwarding is not the right tool:

    actor1 --[forwards Message1]--> actor2
    

    In actor2, if it is forwarded a Message1 from actor1, without a "previous" actor in the message chain, then the sender will be dead letters:

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

    If Actor1 doesn't receive the message from another actor before forwarding that message to Actor2, simply have Actor1 send (!) the message to Actor2 instead of forwarding it.

    If Actor1 does receive the message from another actor before forwarding it, ensure that this "previous" actor is running before Actor2 accesses sender.