Derrops
Derrops

Reputation: 8117

Possible to specify custom Routees or custom constructor arguments for each Actor

I imagine this might not be possible because of supervision strategies needing to recreate actors, nevertheless am I able to feed custom actors to a Router? Or at the very least: custom constructor arguments?

If no, why doesn't akka allow this? Is routing not suppose to be used in this way? I am using a BalancingPool by the way.

This seems to be possible in Scala How to create routers in akka with parameterized actors? but I haven't been able to figure this out in java.

Upvotes: 1

Views: 63

Answers (2)

mattinbits
mattinbits

Reputation: 10428

It is not possible with a pool, since a pool creates multiple instances of from the same Props definition, but it is possible with routing in general. From the docs:

This type of router actor comes in two distinct flavors:

  • Pool - The router creates routees as child actors and removes them from the router if they terminate.
  • Group - The routee actors are created externally to the router and the router sends messages to the specified path using actor selection, without watching for termination.

Do if you create a group rather than a pool, the group can contain whichever actors you want.

Upvotes: 2

Jean Logeart
Jean Logeart

Reputation: 53819

From the Routing documentation:

public class Master extends UntypedActor {

    Router router;
    {
    List<Routee> routees = new ArrayList<Routee>();
    for (int i = 0; i < 5; i++) {
        ActorRef r = getContext().actorOf(Props.create(Worker.class));
        getContext().watch(r);
        routees.add(new ActorRefRoutee(r));
    }
    router = new Router(new RoundRobinRoutingLogic(), routees);
    }
// ...
}

So you can create the routees the way you want.

Upvotes: 1

Related Questions