Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maxant/62bc726f1cc1e46cdaec to your computer and use it in GitHub Desktop.
Save maxant/62bc726f1cc1e46cdaec to your computer and use it in GitHub Desktop.
Trading engine servlet based on actors
@WebServlet(urlPatterns = { "/sell2", "/buy2", "/result2" })
public class TradingEngineServletWithActors extends HttpServlet {
private static final ActorSystem teSystem = ActorSystem.create("TradingEngines");
private static final Map<String, ActorRef> kids = new HashMap<>();
static {
int chunk = PRODUCT_IDS.length / NUM_KIDS;
for (int i = 0, j = PRODUCT_IDS.length; i < j; i += chunk) {
String[] temparray = Arrays.copyOfRange(PRODUCT_IDS, i, i + chunk);
LOGGER.info("created engine for products " + temparray);
ActorRef actor = teSystem.actorOf(Props.create(TradingEngineActor.class), "engine-" + i);
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(TradingEngineActor.RUN, ActorRef.noSender());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment