Created
December 30, 2014 20:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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