Skip to content

Instantly share code, notes, and snippets.

@maxant
Created December 30, 2014 20:36
Show Gist options
  • Save maxant/701b1186f0dcce2990a0 to your computer and use it in GitHub Desktop.
Save maxant/701b1186f0dcce2990a0 to your computer and use it in GitHub Desktop.
Partitioning the market and creating the router
private Router partitionAndCreateRouter() {
Map<String, ActorRef> kids = new HashMap<>();
java.util.List<Routee> routees = new ArrayList<Routee>();
int chunk = Constants.PRODUCT_IDS.length / NUM_KIDS;
for (int i = 0, j = Constants.PRODUCT_IDS.length; i < j; i += chunk) {
String[] temparray = Arrays.copyOfRange(Constants.PRODUCT_IDS, i, i + chunk);
LOGGER.info("created engine for products " + temparray);
ActorRef actor = getContext().actorOf(Props.create(EngineActor.class));
getContext().watch(actor);
routees.add(new ActorRefRoutee(actor));
for (int k = 0; k < temparray.length; k++) {
LOGGER.debug("mapping productId '" + temparray[k] + "' to engine " + i);
kids.put(temparray[k], actor);
}
LOGGER.info("---started trading");
actor.tell(EngineActor.RUN, ActorRef.noSender());
}
Router router = new Router(new PartitioningRoutingLogic(kids), routees);
return router;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment