Search code examples
scalaakkaactor

Akka acessing actor from another tree branch


I have Akka application with several actors. Actor tree looks something like this:

             /user
        /one         TARGET
     /two    /three
   SOURCE

In the source actor I have to send message to TARGET actor. However, I don't know how to do it properly.

Resolving ActorRef from ActorSelection every time seems to be very bad.

Are there any patterns?


Solution

  • The two patterns I am aware of and use are:

    1. Pass the ActorRef of TARGET in the original message sent to SOURCE, so that way SOURCE knows to respond to TARGET. This works well when TARGET is different from message to message, and makes unit testing easy.
    2. Resolve the TARGET ActorRef from the ActorSelection once in either an ESB or Service Locator, and pass that through to SOURCE, and use that. This works well when there is exactly one instance of TARGET in the actor system, however it will make unit testing a little more complex.