Yêu cầu thg 6 5, 2020 6:22 SA 124 0 0
  • 124 0 0
0

Xin ghóp ý về xử lý Thread?

Chia sẻ
  • 124 0 0

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();
Avatar Son Dao Thai @dao.thai.son
thg 6 5, 2020 6:24 SA

góp bạn nhé 😃 không phải ghóp

Avatar Mario @mario
thg 6 5, 2020 10:28 SA

@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ý ^.^

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí