Search code examples
akkaactorbroadcast

How to broadcast-all in Akka?


Java/Akka (v2.3.9) here. Each of my Akka UntypedActor subclasses has the ability to respond to several "generic" messages, such as ExecuteOrder66:

// Groovy pseudo-code
class StormTrooper extends UntypedActor {
    @Override
    void onReceive(Object message) throws Exception {
        if(message instanceof ExecuteOrder66) {
            // Betray the Jedi, serve only the emperor.
        }
    }
}

Let's say I have 100 different actor subclasses that each support ExecuteOrder66. I need a way to broadcast instances of this message to every single one of my actors; so like a public broadcast announce which everybody gets.

I think that link to the Akka docs above gets me close, but I'm not seeing one that sends an ExecuteOrder66 to every single one of my actors. Any ideas?


Solution

  • The problem is that it is not quite clear who "everybody" is. What if some actor a gets a handshake message from some other actor b from remote actor system, stores b's reference, exchanges a few messages, then fails and restarts without the reference of b? Is b part of "everybody"? Who is responsible for finding the actor b again? How is one even supposed to know that b is still alive?

    However, if you have a single specific actor system, a path selection with wildcards could do what you want. Something like this might do the job:

    mySystem.actorSelection("akka://mySystemName/**")
    

    This actor selection can then be used to tell (!) your broadcasted message to every actor on the system. You might also consider to be a little more restrictive, and select only the actors under /user, without touching the system actors.

    Disclaimer: a little ad-hoc actor system I've just set up in the REPL doesn't complain about the path as indicated above, but I did not test it thoroughly. A runnable toy-example might be helpful.