我们在 JMS 消费者中使用 ThreadPoolExecutor 并将其注入(inject)到 DefaultMessageListenerContainer 中。我希望这会为许多消息运行并发线程,但是我们的日志显示线程 id 不会改变。我们的日志显示,对于不同的消息处理,线程 id 总是相同的 24。
这是该场景中的 Spring 配置:
<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 注入(inject) DefaultMessageListenerContainer 之后,消息现在正在不同的线程中执行。
这是生成的配置:
<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"
>
我已尝试阅读文档,但我不明白为什么会发生这种情况。有什么解释吗?
最佳答案
试试这个:
<bean id="threadPoolTaskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10" />
<property name="maxPoolSize" value="25" />
<property name="queueCapacity" value="30" />
</bean>
- 这将在初始化时创建 10 个线程。
- 如果所有 10 个线程都忙并且有新任务出现,那么它将保持任务在队列中。
- 如果队列已满,它将创建第 11 个线程并一直运行到 25。
- 然后会抛出TaskRejected Exception。
关于java - Spring ThreadPoolTaskExecutor 只运行一个线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4269559/