babanin
babanin

Reputation: 3584

Akka routing with two router configs simultaneously

I have simple UntypedActor which has set of subscribers. Actor can handle 2 types of messages ('SUB' - add sender to subscribers, 'UNSUB' - remove sender from subscribers) and other messages resend to subscribers.

private static class Dispatcher extends UntypedActor {
    private Set<ActorRef> subscribers;

    public Dispatcher() {
        subscribers = Sets.newHashSet();
    }

    @Override
    public void onReceive(Object o) throws Exception {
        if ("SUB".equals(o)) {
            subscribers.add(getSender());
            return;
        }

        if ("UNSUB".equals(o)) {
            subscribers.remove(getSender());
            return;
        }

        for (ActorRef subscriber : subscribers) {
            subscriber.tell(o, getSender());
        }
    }
}

I want to create routers with two different strategies: broadcast, roundrobin.

    final int routeeCount = 2;

    BroadcastRouter broadcastRouter = new BroadcastRouter(routeeCount);
    RoundRobinRouter roundRobinRouter = new RoundRobinRouter(routeeCount);

    ActorRef broadCastRef = actorSystem.actorOf(Props.create(Dispatcher.class)
            .withRouter(broadcastRouter), "Broadcast");
    ActorRef roundRobinRef = actorSystem.actorOf(Props.create(Dispatcher.class)
            .withRouter(roundRobinRouter), "RoundRobin");

Each router will create personal group of routers, but it's not suitable for me. I want to routers using same actors, because I have following use case:

  1. Actors A and B sends message 'SUB' to broadCastRef;
  2. broadCastRef broadcasts message to it's own 2 child actors (X1 and X2) (routeeCount = 2);
  3. From now on I will use roundRobinRef to pass messages to actors A and B.

So the question is how to reuse actors in two different router actors?

Upvotes: 0

Views: 750

Answers (1)

Roland Kuhn
Roland Kuhn

Reputation: 15472

For your use-case you do not need two different routers, since you can just send a akka.routing.Broadcast message containing your subscription request and it will be routed to all routees of the RoundRobinRouter.

In general, if you want to route to the same set of targets using two different routers, then you’ll need to create the routees separately and pass them to the routers as discussed here (scroll down a little bit).

Upvotes: 1

Related Questions