0
Xin ghóp ý về xử lý Thread?
I used QueueThread for server adaptor receive many request then send to uri target. I defined 5 Thread live at the same time, over 5 will send to BlockingQueue.
I understand that will have max 5 request at the same time send to uri target, that sure server target not overload!, and when my server adaptor restart BlockingQueue will drainTo list (line code below comment // server restart tasks reload) and store in RAM then reload when server live again.
int maxCapacity = StaticConfig.JETTY_MAX_QUEUED == 0 ? Integer.MAX_VALUE :
Math.max(StaticConfig.JETTY_MIN_THREADS, StaticConfig.JETTY_MAX_QUEUED);
BlockingQueue<Runnable> bqueue = new BlockingArrayQueue<Runnable>(StaticConfig.JETTY_MIN_THREADS, StaticConfig.JETTY_MIN_THREADS, maxCapacity);
QueuedThreadPool threadPool = new QueuedThreadPool(StaticConfig.JETTY_MAX_THREADS,
StaticConfig.JETTY_MIN_THREADS, StaticConfig.JETTY_IDE_TIMEOUT,
bqueue);
threadPool.setName(StaticConfig.JETTY_NAME);
threadPool.setDaemon(true);
// server restart tasks reload
if (bqueue.size() > 0) {
java.util.List<Runnable> list = new java.util.ArrayList<Runnable>();
list.add(bqueue.take());
int tasks = bqueue.drainTo(list);
}
Server server = new Server(threadPool);
int port = 0;
try {
port = Integer.parseInt(System.getProperty("server.port"));
} catch (NumberFormatException ex) {
LOGGER.error("Property server.port not found. " + ex.getMessage(), ex);
System.exit(1);
}
try (ServerConnector httpConnector = new ServerConnector(server)) {
httpConnector.setPort(port);
server.setConnectors(new Connector[]{httpConnector});
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
System.exit(1);
}
ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(IndexServlet.class, "/v1.0/*");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{handler});
server.setHandler(handlers);
server.setStopAtShutdown(true);
server.start();
LOGGER.info("Server started on port = " + port);
server.join();
góp
bạn nhé không phảighóp
@dao.thai.son làm sao để ghi log ktra đc khi req đến > 5 (config minThread = 5) thì req đó sẽ đc đẩy vào BlockingQueue để đợi Thread trống sẽ lấy lên xử lý ^.^