Spring ThreadPoolTaskExecutor only running one thread

前端 未结 4 1769
春和景丽
春和景丽 2020-12-13 00:51

We are using ThreadPoolExecutor in our JMS consumer and injecting it into a DefaultMessageListenerContainer. I expect this to be running concurrent threads for many messages

4条回答
  •  别那么骄傲
    2020-12-13 01:31

    I think chosen answer is wrong. IIRC, the way ThreadPoolTaskExecutor (eventually ThreadPoolExecutor in JDK ) working is

    1. ThreadPoolTaskExecutor create threads to up corePoolSize when it's initiated.
    2. It takes request up to corePoolSize and let thread to process the task.
    3. If there are more requests incoming while all threads are busy, ThreadPoolTaskExecutor is start queuing up those request into internal queue. This can be problematic since this queue size will be Integer.MAX_VALUE as default if you don't specify queue queueCapacity.
    4. Request added in #3 will be executed by thread when there is any available threads in the pool.
    5. If requests are keep coming and all threads are busy & queue is full, the ThreadPoolTaskExecutor starts creating new threads up to maxPoolSize to process requests.
    6. If requests over those (increased threads number + queue size), then task will be rejected or following policy you specified.

    So the problem here I think is, either 1) your consumer is fast enough or 2) you are stacking requests too slowly, so one thread you specify with corePoolSize was enough to process new incoming requests + queued task without allowing ThreadPoolTaskExecutor to create new threads. I'm pretty sure if you push it harder or set queue's capacity with small number ( like 5~10 ), you'll be able to see the number of threads is increasing.

提交回复
热议问题