Akka (java), no lock for all children

Let's say I have an actor in a region, and each region has a certain number of people inside it. As you pass on the message to everyone, knowing that the list of people may change over time, broadcast routers seem to be the choice, but then the problem is that they have the maximum number of routes, and that I cannot dynamically add people to the router.

My question is: I know that there is an EventBus, I could subscribe my people to the Bus Bus of events, but I do not want them to receive every sent message, I want them to receive messages from the region.

right now in akka, we need to create a router with a certain number of routes, Example:

Router router = new router(person1, person2) 

it’s bad, because in the beginning there is nobody in this region, I don’t know the people who will join my region.

Is there a way to make a kind of dynamic router: Example:

 Region region = new region() region.router = new Router() Person person1 = new Person() region.router.subscribe(person1); region.router.tell("hello",null); 
+3
java akka broadcast router
source share
1 answer

Your decision is already very close: you need a router, but not one of the specially made ones. Instead, just write an actor who forwards messages to subscribers:

 class MyRouter extends UntypedActor { final Set<ActorRef> subscribers = new HashSet<ActorRef>(); @Override public void onReceive(Object msg) { if (msg instanceof Subscribe) { subscribers.add(getSender()); } else if (msg instanceof Unsubscribe) { subscribers.remove(getSender()); } else { for (ActorRef target: subscribers) { target.tell(msg, getSender()); } } } } 
+3
source share

All Articles