Skip to content

Instantly share code, notes, and snippets.

@maxant
Created December 30, 2014 20:46
Show Gist options
  • Save maxant/1de16c9ab99c28fa9878 to your computer and use it in GitHub Desktop.
Save maxant/1de16c9ab99c28fa9878 to your computer and use it in GitHub Desktop.
The routing logic used to decide which actor will handle a sales or purchase order.
public static class PartitioningRoutingLogic implements RoutingLogic {
private Map<String, ActorRef> kids;
public PartitioningRoutingLogic(Map<String, ActorRef> kids) {
this.kids = kids;
}
@Override
public Routee select(Object message, IndexedSeq<Routee> routees) {
//find which product ID is relevant here
String productId = null;
if(message instanceof PurchaseOrder){
productId = ((PurchaseOrder) message).getProductId();
}else if(message instanceof SalesOrder){
productId = ((SalesOrder) message).getProductId();
}
ActorRef actorHandlingProduct = kids.get(productId);
//no go find the routee for the relevant actor
for(Routee r : JavaConversions.asJavaIterable(routees)){
ActorRef a = ((ActorRefRoutee) r).ref(); //cast ok, since the are by definition in this program all routees to ActorRefs
if(a.equals(actorHandlingProduct)){
return r;
}
}
return akka.routing.NoRoutee$.MODULE$; //none found, return NoRoutee
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment