pawan
pawan

Reputation: 33

How to create a custom router in Java using Akka 2.3.11?

I a newbie to akka and kind of stuck. Any help much appreciated.

In akka 2.1.4 documentation http://doc.akka.io/docs/akka/2.1.4/java/routing.html the sample custom router code is provided where the custom route logic is to route the message as per instance.

return new CustomRoute() {
    @Override
    public Iterable<Destination> destinationsFor(ActorRef sender, Object msg) {
      switch ((Message) msg) {
      case DemocratVote:
      case DemocratCountResult:
        return Arrays.asList(
          new Destination[] { new Destination(sender, democratActor) });
      case RepublicanVote:
      case RepublicanCountResult:
        return Arrays.asList(
          new Destination[] { new Destination(sender, republicanActor) });
      default:
        throw new IllegalArgumentException("Unknown message: " + msg);
      }
    }
};

I am trying to implement the similar logic using akka 2.3.11 router API's http://doc.akka.io/docs/akka/2.3.11/java/routing.html but not able to understand the below code snippet.

@Override
public Routee select(Object message, IndexedSeq<Routee> routees) {
    List<Routee> targets = new ArrayList<Routee>();
    for (int i = 0; i < nbrCopies; i++) {
      targets.add(roundRobin.select(message, routees));
    }
    return new SeveralRoutees(targets);
}

Not able to get the relation of routee and actor. How i can correlate the routee with message instance?

Upvotes: 1

Views: 586

Answers (1)

Diego Martinoia
Diego Martinoia

Reputation: 4662

You don't HAVE TO(see the send method notes) choose the destination routee from the provided list. You can have something like this:

Routee democratRoutee = new ActorRefRoutee(democratActorRef);
Routee republicanRoutee = new ActorRefRoutee(republicanActorRef);

@Override
public Routee select(Object message, IndexedSeq<Routee> routees) {
  switch ((Message) msg) {
    case DemocratVote: return new NoRoutee();
    case DemocratCountResult:
      democratRoutee;
    case RepublicanVote: return new NoRoutee();
    case RepublicanCountResult:
      return republicanRoutee;      
    default:
      throw new IllegalArgumentException("Unknown message: " + msg);
  }
}

//Note: I don't know the exact syntax of the NoRoutee instance getter in Java, so it may vary.

Upvotes: 1

Related Questions