cs4r
cs4r

Reputation: 624

create a router actor parameterizing the creation of each one of its routees in akka java

I need to know how to create a RoundRobinPool Router Actor in which each one of its routees has its own reference to another object . That reference that is held by every routee must be different for each routee.

A way to do that would be something like this:

ActorRef ref = system.actorOf(new RoundRobinPool(5).props(Props.create(Worker.class, new AnotherObject())), 
"router");

but the trouble with this approach is that every worker has the same reference to AnotherObject and I do not wish that side effect.

It would be something like this, but with a Router Actor:

List<Routee> routees = new ArrayList<Routee>();
    for (int i = 0; i < 5; i++) {
      ActorRef r = system.actorOf(Props.create(Worker.class, new AnotherObject()));
      routees.add(new ActorRefRoutee(r));
    }
    Router router = new Router(new RoundRobinRoutingLogic(), routees); // That is a Router I need an ActorRef

Has someone any idea how to do that?

Cheers

Upvotes: 0

Views: 234

Answers (2)

nickebbitt
nickebbitt

Reputation: 1761

Check out the Akka Java documentation regarding Router groups. You can create your routees individually and then provide them to your router group either via config or programmatically. In your case programmatically is probably what you are after so you can pass a different reference to each routee.

Upvotes: 1

Ravi Kiran
Ravi Kiran

Reputation: 1139

You can use a RoundRobinGroup. With a group, you create the actors you want and then pass the paths of the actors to the Group when creating the group. The difference between a pool and a group is only how the routees are created.

Edited with the actual code

String parent = new String("/user/<whatever is your actual path to routees>");
List<String> routees = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
  ActorRef r = system.actorOf(Props.create(Worker.class, new AnotherObject()), “route” + i);
  routees.add(parent + “routee” + i);
}
system.actorOf(new RoundRobinGroup(routees).props(), “router”);

Upvotes: 0

Related Questions