Akka: do you communicate outside the acting system?

I have the following driver / main class encapsulating my Akka program:

// Groovy pseudo-code class FizzBuzz { ActorSystem actorSystem static void main(String[] args) { FizzBuzz d = new FizzBuzz() d.run() } void run() { Initialize initCmd = new Initialize() MasterActor master = actorSystem.get(...) // Tells the entire actor system to initialize itself and start doing stuff. // ChickenCluckDetector is an actor managed/supervised by MasterActor. master.tell(initCmd, ...) } // Called when a ChickenCluckDetector actor inside the actor system receives // a 'Cluck' message. void onChickenGoesCluck(Cluck cluck) { // Do something } } 

And the following ChickenCluckDetector actor:

 class ChickenCluckDetector extends UntypedActor { @Override void onReceive(Object message) { if(message instanceof Cluck) { Cluck cluck = message as Cluck // Now, how to pass the message safely/properly to FizzBuzz#onCluck? } } } 

So, the problem is how to safely / correctly pass the Cluck message to FizzBuzz#onCluck(Cluck) , which lives outside the actors system? I could provide a FizzBuzz link to the ChickenCluckDetector like this:

 class ChickenCluckDetector extends UntypedActor { FizzBuzz fizzBuzz @Override void onReceive(Object message) { if(message instanceof Cluck) { Cluck cluck = message as Cluck fizzBuzz.onCluck(cluck) } } } 

But I feel this violates Akka’s best practices and can cause all kinds of concurrency issues, especially if there is only one FizzBuzz (which is) inactive / driver and ten thousand ChickenCluckDetector actors. Ideas?

+7
java actor akka
source share
1 answer

if there is only one FizzBuzz (which is) inactive / driver and ten thousand participants ChickenCluckDetector

Then it would be better to create one common parent for all these ChickenCluckDetectors. This parent could then safely hold the link to FizzBuzz, receive clucks from all of its children, and call the onCluck method.

One question for receiving messages outside the participants is asking. And in Scala there is a DSL actor (added for completeness only). But I believe that you do not need any of them in your example.

 public class ChickenCluckMaster extends UntypedActor { private FizzBuzz fizzBuzz; public ChickenCluckMaster(FizzBuzz fizzBuzz) { this.fizzBuzz = fizzBuzz; } public void onReceive(Object message) throws Exception { if (message instanceOf CreateDetector) { getContext().actorOf( Props.create(ChickenCluckDetector.class, getSelf); // Create child } else if (message instanceof Cluck) { fizzBuzz.onCluck(cluck); } else { unhandled(message); } } } public class ChickenCluckDetector extends UntypedActor { private ActorRef master; public ChickenCluckDetector(ActorRef master) { this.master = master; } public void onReceive(Object message) throws Exception { if (message instanceof Cluck) { Cluck cluck = (Cluck) message; master.tell(cluck, getSelf); } else { unhandled(message); } } } 
+3
source share

All Articles