Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring ThreadPoolTaskScheduler vs ThreadPoolTaskExecutor

It is mentioned in the Spring documentation that:

ThreadPoolTaskScheduler actually implements Spring's TaskExecutor interface as well, so that a single instance can be used for asynchronous execution as soon as possible as well as scheduled, and potentially recurring, executions.

So which are the scenarios where we would want to use ThreadPoolTaskExecutor instance over ThreadPoolTaskScheduler instance?

I am using currently using Spring XML. I am creating bean of ThreadPoolTaskScheduler as follows:

<task:scheduler id="myScheduler" pool-size="1"/>

while bean of ThreadPoolTaskExecutor instance can be created as

<task:executor id="executor" pool-size="10"/>
like image 249
chammu Avatar asked Oct 31 '15 16:10

chammu


People also ask

What is ThreadPoolTaskExecutor in Spring?

ThreadPoolTaskExecutor is a java bean that allows for configuring a ThreadPoolExecutor in a bean style by setting up the values for the instance variables like corePoolSize, maxPoolSize, keepAliveSeconds, queueCapacity and exposing it as a Spring TaskExecutor.

What is ThreadPoolTaskScheduler?

ThreadPoolTaskScheduler ThreadPoolTaskScheduler is well suited for internal thread management, as it delegates tasks to the ScheduledExecutorService and implements the TaskExecutor interface – so that single instance of it is able to handle asynchronous potential executions as well as the @Scheduled annotation.

What is Spring task scheduling pool size?

The default scheduler pool size in spring-boot is only one.

What is the use of TaskExecutor interface in Spring?

The TaskExecutor was originally created to give other Spring components an abstraction for thread pooling where needed. Components such as the ApplicationEventMulticaster , JMS's AbstractMessageListenerContainer , and Quartz integration all use the TaskExecutor abstraction to pool threads.


1 Answers

  • ThreadPoolTaskExecutor is a specialized class for executing tasks.
  • ThreadPoolTaskScheduler is a specialized class for scheduling tasks.

The sentence you quoted in the Spring documentation is only saying that you can use a scheduler to execute tasks, but that it is not its main purpose. A ThreadPoolTaskExecutor provides fine-grained configuration over the thread pool through its corePoolSize, maxPoolSize, keepAliveSeconds and queueCapacity properties. A scheduler such as ThreadPoolTaskScheduler does not provide such configuration.

As such, choosing between the two comes down the following question: do I need to execute or schedule execution of tasks?

like image 126
Tunaki Avatar answered Oct 17 '22 08:10

Tunaki