Spring ThreadPoolTaskExecutor只运行一个线程 [英] Spring ThreadPoolTaskExecutor only running one thread
问题描述
我们在JMS使用者中使用ThreadPoolExecutor并将其注入DefaultMessageListenerContainer。我希望这可以为许多消息运行并发线程,但是我们的日志显示线程id不会改变。我们的日志记录显示,对于不同的消息处理,线程ID在24处始终相同。
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 however our logs show that the thread id won't change.Our logging shows that for different processing of messages, the thread id is always the same at 24.
这是该场景中的弹簧配置:
This is the spring configuration in that scenario:
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"
p:connectionFactory-ref="cachedConnectionFactory"
p:destination-ref="formsCRRDestination"
p:messageListener-ref="formServicePojo"
p:concurrentConsumers="5"
p:idleTaskExecutionLimit="1"
p:maxConcurrentConsumers="25"
p:taskExecutor-ref="threadPoolExecutor"
destroy-method="doShutdown"
>
<bean id="threadPoolExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >
<property name="corePoolSize" value="1"/>
<property name="maxPoolSize" value="15"/>
<property name="keepAliveSeconds" value="30"/>
</bean>
在不将threadPoolExectuor bean注入DefaultMessageListenerContainer之后,消息现在正在不同的线程中执行。
After not injecting the threadPoolExectuor bean into the DefaultMessageListenerContainer, the messages are now being executed in different threads.
这是结果配置:
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"
p:connectionFactory-ref="cachedConnectionFactory"
p:destination-ref="formsCRRDestination"
p:messageListener-ref="formServicePojo"
p:concurrentConsumers="5"
p:idleTaskExecutionLimit="1"
p:maxConcurrentConsumers="25"
destroy-method="doShutdown"
>
我试过阅读文档,但我不明白为什么会这样。有没有解释?
I have tried reading the documentation and I don't understand why this is happening. Any explanation?
推荐答案
完成 Spring中的ThreadPoolTaskExecutor代码并阅读 ThreadPoolTaskExecutor 我认为这就是答案:
After going through the ThreadPoolTaskExecutor code in Spring and reading the Java docs for ThreadPoolTaskExecutor I think this is the answer:
无界队列。使用无界
队列(例如
LinkedBlockingQueue而没有
预定义容量)将导致新的
任务在所有
corePoolSize线程忙的情况下排队。因此,
不超过corePoolSize线程将创建
。 (并且
maximumPoolSize的值因此没有
任何影响。)
Unbounded queues. Using an unbounded queue (for example a LinkedBlockingQueue without a predefined capacity) will cause new tasks to be queued in cases where all corePoolSize threads are busy. Thus, no more than corePoolSize threads will ever be created. (And the value of the maximumPoolSize therefore doesn't have any effect.)
在我们的配置中上面,我们默认使用LinkedBlockingQueue,我们的corePoolSize为1.这就是maximumPoolSize不会产生任何影响的原因。
In our configuration above, we were using the LinkedBlockingQueue by default and our corePoolSize is 1. This is why the maximumPoolSize won't have any effect.
这篇关于Spring ThreadPoolTaskExecutor只运行一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!