Search code examples
scalaakkaactorself

What is the right approach to let an actor ask "itself" in akka?


I am currently looking for a proper way to send a message from one actor to another of the same kind. At first I tried to ask "itself" (self), but soon I understood that this is not possible. You cant send a message to self and wait for response while it still handles the "waiting" process.

Now my question:

What is the right approach to ask an actor of the same type? SHould I create a temporary actor which I destroy after I used it? If so.. what about the naming (must be unique)!? Is there any helpfull pattern?

Thanks in advance


Solution

  • An actor most definitely can ask itself (i.e. with self ask). You can't block on the future (with Await.result), but you shouldn't be blocking on futures anyway. However, there is little sane reason to do this. Instead of sending yourself a message and getting a response, execute whatever code you use to handle that message directly.

    As an additional point, the ask pattern actually does create a temporary actor and destroy it after it is no longer needed. There is no need to reimplement this.