Purpose of taskExecutor property in Spring's DefaultMessageListenerContainer
Asked Answered
P

2

11

The Spring's DefaultMessageListenerContainer (DMLC) has concurrentConsumer and taskExecutor property. The taskExecutor bean can be given corePoolSize property. What is then the difference between specifying concurrentConsumer and corePoolSize ? When concurrentConsumer property is defined it means that Spring will create specified number of consumer/messageListeners to process the message. When does corePoolSize comes into picture ?

Code snippet

<bean id="myMessageListener"
    class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="destination" ref="myQueue" />
    <property name="messageListener" ref="myListener" />
    <property name="cacheLevelName" value="CACHE_CONSUMER"/>
    <property name="maxConcurrentConsumers" value="10"/>
    <property name="concurrentConsumers" value="3"/>
    <property name="taskExecutor" ref="myTaskExecutor"/>
</bean>

 <bean id="myTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >
    <property name="corePoolSize" value="100"/>
    <property name="maxPoolSize" value="100"/>
    <property name="keepAliveSeconds" value="30"/>
     <property name="threadNamePrefix" value="myTaskExecutor"/>
</bean>
Policyholder answered 10/4, 2013 at 9:15 Comment(0)
I
0

According to 4.3.6 version, the taskExecutor contains instances of AsyncMessageListenerInvoker which responsible for message processing. corePoolSize is a number of physical threads in the defined pool, while concurrentConsumer is a number of tasks in this pool. I guess this abstraction was designed for more flexible control.

Inerrant answered 15/8, 2017 at 12:28 Comment(0)
B
0

The Purpose of TaskExecutor Property

Set the Spring TaskExecutor to use for running the listener threads. Default is a SimpleAsyncTaskExecutor, starting up a number of new threads, according to the specified number of concurrent consumers.

Specify an alternative TaskExecutor for integration with an existing thread pool.

Above is from [Spring Official Documentation][1]

When you specify the alternative task executor, then instead of using the asyncTaskExcutor the listener threads will use the defined task executor.

This can be easily illustrated when we define two jmsListeners with the same containerFactory. when you specify the concurrency, the concurrency should support the taskExecutor corePoolSize and maxPoolSize.

If you set the concurrency as 5-20 and you have two listeners then you should set the core poolSize more than 10 and the maxPoolSize more than 40. then listeners can get the threads accordingly their concurrency limit.

In this case, If you set the maxPoolsize to less than 10 then the listener containers will not be upon 10. From the spring you will get below warning as well

The number of scheduled consumers has dropped below concurrent consumers limit, probably due to tasks having been rejected. Check your thread pool configuration! Automatic recovery to be triggered by remaining consumers.

basically, the listener threads will act based on the taskExecutor property. 


Bridoon answered 22/10, 2021 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.