Skip to content

Instantly share code, notes, and snippets.

@maxant
Created December 25, 2014 20:29
Show Gist options
  • Save maxant/f4308426aa60341e3f57 to your computer and use it in GitHub Desktop.
Save maxant/f4308426aa60341e3f57 to your computer and use it in GitHub Desktop.
Trading Engine Servlet, using threads
@WebServlet(urlPatterns = { "/sell", "/buy", "/result" })
public class TradingEngineServlet extends HttpServlet {
private static final Map<String, TradingEngineThread> 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);
TradingEngineThread engineThread = new TradingEngineThread(DELAY, TIMEOUT, (type, data) -> event(type, data));
for (int k = 0; k < temparray.length; k++) {
LOGGER.debug("mapping productId '" + temparray[k] + "' to engine " + i);
kids.put(temparray[k], engineThread);
}
LOGGER.info("---started trading");
engineThread.start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment