Package org.springframework.boot.task
Class TaskExecutorBuilder
- java.lang.Object
- org.springframework.boot.task.TaskExecutorBuilder
public class TaskExecutorBuilder extends Object
Builder that can be used to configure and create aTaskExecutor
. Provides convenience methods to set commonThreadPoolTaskExecutor
settings and registertaskDecorator(TaskDecorator)
). For advanced configuration, consider usingTaskExecutorCustomizer
.In a typical auto-configured Spring Boot application this builder is available as a bean and can be injected whenever a
TaskExecutor
is needed.- Since:
- 2.1.0
Constructor Summary
Constructors Constructor Description TaskExecutorBuilder()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TaskExecutorBuilder
additionalCustomizers(Iterable<TaskExecutorCustomizer> customizers)
AddTaskExecutorCustomizers
that should be applied to theThreadPoolTaskExecutor
.TaskExecutorBuilder
additionalCustomizers(TaskExecutorCustomizer... customizers)
AddTaskExecutorCustomizers
that should be applied to theThreadPoolTaskExecutor
.TaskExecutorBuilder
allowCoreThreadTimeOut(boolean allowCoreThreadTimeOut)
Set whether core threads are allow to time out.org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
build()
Build a newThreadPoolTaskExecutor
instance and configure it using this builder.<T extends org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor>
Tbuild(Class<T> taskExecutorClass)
Build a newThreadPoolTaskExecutor
instance of the specified type and configure it using this builder.<T extends org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor>
Tconfigure(T taskExecutor)
Configure the providedThreadPoolTaskExecutor
instance using this builder.TaskExecutorBuilder
corePoolSize(int corePoolSize)
Set the core number of threads.TaskExecutorBuilder
customizers(Iterable<TaskExecutorCustomizer> customizers)
Set theTaskExecutorCustomizers
that should be applied to theThreadPoolTaskExecutor
.TaskExecutorBuilder
customizers(TaskExecutorCustomizer... customizers)
Set theTaskExecutorCustomizers
that should be applied to theThreadPoolTaskExecutor
.TaskExecutorBuilder
keepAlive(Duration keepAlive)
Set the time limit for which threads may remain idle before being terminated.TaskExecutorBuilder
maxPoolSize(int maxPoolSize)
Set the maximum allowed number of threads.TaskExecutorBuilder
queueCapacity(int queueCapacity)
Set the capacity of the queue.TaskExecutorBuilder
taskDecorator(org.springframework.core.task.TaskDecorator taskDecorator)
Set theTaskDecorator
to use ornull
to not use any.TaskExecutorBuilder
threadNamePrefix(String threadNamePrefix)
Set the prefix to use for the names of newly created threads.
Constructor Detail
TaskExecutorBuilder
public TaskExecutorBuilder()
Method Detail
queueCapacity
public TaskExecutorBuilder queueCapacity(int queueCapacity)
Set the capacity of the queue. An unbounded capacity does not increase the pool and therefore ignoresmaxPoolSize
.- Parameters:
queueCapacity
- the queue capacity to set- Returns:
- a new builder instance
corePoolSize
public TaskExecutorBuilder corePoolSize(int corePoolSize)
Set the core number of threads. Effectively that maximum number of threads as long as the queue is not full.Core threads can grow and shrink if
allowCoreThreadTimeOut(boolean)
is enabled.- Parameters:
corePoolSize
- the core pool size to set- Returns:
- a new builder instance
maxPoolSize
public TaskExecutorBuilder maxPoolSize(int maxPoolSize)
Set the maximum allowed number of threads. When thequeue
is full, the pool can expand up to that size to accommodate the load.If the
queue capacity
is unbounded, this setting is ignored.- Parameters:
maxPoolSize
- the max pool size to set- Returns:
- a new builder instance
allowCoreThreadTimeOut
public TaskExecutorBuilder allowCoreThreadTimeOut(boolean allowCoreThreadTimeOut)
Set whether core threads are allow to time out. When enabled, this enables dynamic growing and shrinking of the pool.- Parameters:
allowCoreThreadTimeOut
- if core threads are allowed to time out- Returns:
- a new builder instance
keepAlive
public TaskExecutorBuilder keepAlive(Duration keepAlive)
Set the time limit for which threads may remain idle before being terminated.- Parameters:
keepAlive
- the keep alive to set- Returns:
- a new builder instance
threadNamePrefix
public TaskExecutorBuilder threadNamePrefix(String threadNamePrefix)
Set the prefix to use for the names of newly created threads.- Parameters:
threadNamePrefix
- the thread name prefix to set- Returns:
- a new builder instance
taskDecorator
public TaskExecutorBuilder taskDecorator(org.springframework.core.task.TaskDecorator taskDecorator)
Set theTaskDecorator
to use ornull
to not use any.- Parameters:
taskDecorator
- the task decorator to use- Returns:
- a new builder instance
customizers
public TaskExecutorBuilder customizers(TaskExecutorCustomizer... customizers)
Set theTaskExecutorCustomizers
that should be applied to theThreadPoolTaskExecutor
. Customizers are applied in the order that they were added after builder configuration has been applied. Setting this value will replace any previously configured customizers.- Parameters:
customizers
- the customizers to set- Returns:
- a new builder instance
- See Also:
additionalCustomizers(TaskExecutorCustomizer...)
customizers
public TaskExecutorBuilder customizers(Iterable<TaskExecutorCustomizer> customizers)
Set theTaskExecutorCustomizers
that should be applied to theThreadPoolTaskExecutor
. Customizers are applied in the order that they were added after builder configuration has been applied. Setting this value will replace any previously configured customizers.- Parameters:
customizers
- the customizers to set- Returns:
- a new builder instance
- See Also:
additionalCustomizers(TaskExecutorCustomizer...)
additionalCustomizers
public TaskExecutorBuilder additionalCustomizers(TaskExecutorCustomizer... customizers)
AddTaskExecutorCustomizers
that should be applied to theThreadPoolTaskExecutor
. Customizers are applied in the order that they were added after builder configuration has been applied.- Parameters:
customizers
- the customizers to add- Returns:
- a new builder instance
- See Also:
customizers(TaskExecutorCustomizer...)
additionalCustomizers
public TaskExecutorBuilder additionalCustomizers(Iterable<TaskExecutorCustomizer> customizers)
AddTaskExecutorCustomizers
that should be applied to theThreadPoolTaskExecutor
. Customizers are applied in the order that they were added after builder configuration has been applied.- Parameters:
customizers
- the customizers to add- Returns:
- a new builder instance
- See Also:
customizers(TaskExecutorCustomizer...)
build
public org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor build()
Build a newThreadPoolTaskExecutor
instance and configure it using this builder.- Returns:
- a configured
ThreadPoolTaskExecutor
instance. - See Also:
build(Class)
,configure(ThreadPoolTaskExecutor)
build
public <T extends org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor> T build(Class<T> taskExecutorClass)
Build a newThreadPoolTaskExecutor
instance of the specified type and configure it using this builder.- Type Parameters:
T
- the type of task executor- Parameters:
taskExecutorClass
- the template type to create- Returns:
- a configured
ThreadPoolTaskExecutor
instance. - See Also:
build()
,configure(ThreadPoolTaskExecutor)
configure
public <T extends org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor> T configure(T taskExecutor)
Configure the providedThreadPoolTaskExecutor
instance using this builder.- Type Parameters:
T
- the type of task executor- Parameters:
taskExecutor
- theThreadPoolTaskExecutor
to configure- Returns:
- the task executor instance
- See Also:
build()
,build(Class)