Class ExecutorConfigurationSupport
- All Implemented Interfaces:
Serializable
,ThreadFactory
,Aware
,BeanNameAware
,DisposableBean
,InitializingBean
- Direct Known Subclasses:
ScheduledExecutorFactoryBean
,ThreadPoolExecutorFactoryBean
,ThreadPoolTaskExecutor
,ThreadPoolTaskScheduler
ExecutorService
(typically a ThreadPoolExecutor
or
ScheduledThreadPoolExecutor
).
Defines common configuration settings and common lifecycle handling,
inheriting thread customization options (name, priority, etc) from
CustomizableThreadCreator
.
- Since:
- 3.0
- Author:
- Juergen Hoeller
- See Also:
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Callsinitialize()
after the container applied all property values.protected void
cancelRemainingTask
(Runnable task) Cancel the given remaining task which never commended execution, as returned fromExecutorService.shutdownNow()
.void
destroy()
Callsshutdown
when the BeanFactory destroys the executor instance.void
Set up the ExecutorService.protected abstract ExecutorService
initializeExecutor
(ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) Create the targetExecutorService
instance.void
setAwaitTerminationMillis
(long awaitTerminationMillis) Variant ofsetAwaitTerminationSeconds(int)
with millisecond precision.void
setAwaitTerminationSeconds
(int awaitTerminationSeconds) Set the maximum number of seconds that this executor is supposed to block on shutdown in order to wait for remaining tasks to complete their execution before the rest of the container continues to shut down.void
setBeanName
(String name) Set the name of the bean in the bean factory that created this bean.void
setRejectedExecutionHandler
(RejectedExecutionHandler rejectedExecutionHandler) Set the RejectedExecutionHandler to use for the ExecutorService.void
setThreadFactory
(ThreadFactory threadFactory) Set the ThreadFactory to use for the ExecutorService's thread pool.void
setThreadNamePrefix
(String threadNamePrefix) Specify the prefix to use for the names of newly created threads.void
setWaitForTasksToCompleteOnShutdown
(boolean waitForJobsToCompleteOnShutdown) Set whether to wait for scheduled tasks to complete on shutdown, not interrupting running tasks and executing all tasks in the queue.void
shutdown()
Perform a shutdown on the underlying ExecutorService.Methods inherited from class org.springframework.scheduling.concurrent.CustomizableThreadFactory
newThread
Methods inherited from class org.springframework.util.CustomizableThreadCreator
createThread, getDefaultThreadNamePrefix, getThreadGroup, getThreadNamePrefix, getThreadPriority, isDaemon, nextThreadName, setDaemon, setThreadGroup, setThreadGroupName, setThreadPriority
-
Field Details
-
logger
-
-
Constructor Details
-
ExecutorConfigurationSupport
public ExecutorConfigurationSupport()
-
-
Method Details
-
setThreadFactory
Set the ThreadFactory to use for the ExecutorService's thread pool. Default is the underlying ExecutorService's default thread factory.In a Jakarta EE or other managed environment with JSR-236 support, consider specifying a JNDI-located ManagedThreadFactory: by default, to be found at "java:comp/DefaultManagedThreadFactory". Use the "jee:jndi-lookup" namespace element in XML or the programmatic
JndiLocatorDelegate
for convenient lookup. Alternatively, consider using Spring'sDefaultManagedAwareThreadFactory
with its fallback to local threads in case of no managed thread factory found. -
setThreadNamePrefix
Description copied from class:CustomizableThreadCreator
Specify the prefix to use for the names of newly created threads. Default is "SimpleAsyncTaskExecutor-".- Overrides:
setThreadNamePrefix
in classCustomizableThreadCreator
-
setRejectedExecutionHandler
public void setRejectedExecutionHandler(@Nullable RejectedExecutionHandler rejectedExecutionHandler) Set the RejectedExecutionHandler to use for the ExecutorService. Default is the ExecutorService's default abort policy.- See Also:
-
setWaitForTasksToCompleteOnShutdown
public void setWaitForTasksToCompleteOnShutdown(boolean waitForJobsToCompleteOnShutdown) Set whether to wait for scheduled tasks to complete on shutdown, not interrupting running tasks and executing all tasks in the queue.Default is "false", shutting down immediately through interrupting ongoing tasks and clearing the queue. Switch this flag to "true" if you prefer fully completed tasks at the expense of a longer shutdown phase.
Note that Spring's container shutdown continues while ongoing tasks are being completed. If you want this executor to block and wait for the termination of tasks before the rest of the container continues to shut down - e.g. in order to keep up other resources that your tasks may need -, set the
"awaitTerminationSeconds"
property instead of or in addition to this property. -
setAwaitTerminationSeconds
public void setAwaitTerminationSeconds(int awaitTerminationSeconds) Set the maximum number of seconds that this executor is supposed to block on shutdown in order to wait for remaining tasks to complete their execution before the rest of the container continues to shut down. This is particularly useful if your remaining tasks are likely to need access to other resources that are also managed by the container.By default, this executor won't wait for the termination of tasks at all. It will either shut down immediately, interrupting ongoing tasks and clearing the remaining task queue - or, if the
"waitForTasksToCompleteOnShutdown"
flag has been set totrue
, it will continue to fully execute all ongoing tasks as well as all remaining tasks in the queue, in parallel to the rest of the container shutting down.In either case, if you specify an await-termination period using this property, this executor will wait for the given time (max) for the termination of tasks. As a rule of thumb, specify a significantly higher timeout here if you set "waitForTasksToCompleteOnShutdown" to
true
at the same time, since all remaining tasks in the queue will still get executed - in contrast to the default shutdown behavior where it's just about waiting for currently executing tasks that aren't reacting to thread interruption. -
setAwaitTerminationMillis
public void setAwaitTerminationMillis(long awaitTerminationMillis) Variant ofsetAwaitTerminationSeconds(int)
with millisecond precision.- Since:
- 5.2.4
- See Also:
-
setBeanName
Description copied from interface:BeanNameAware
Set the name of the bean in the bean factory that created this bean.Invoked after population of normal bean properties but before an init callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method.- Specified by:
setBeanName
in interfaceBeanNameAware
- Parameters:
name
- the name of the bean in the factory. Note that this name is the actual bean name used in the factory, which may differ from the originally specified name: in particular for inner bean names, the actual bean name might have been made unique through appending "#..." suffixes. Use theBeanFactoryUtils.originalBeanName(String)
method to extract the original bean name (without suffix), if desired.
-
afterPropertiesSet
public void afterPropertiesSet()Callsinitialize()
after the container applied all property values.- Specified by:
afterPropertiesSet
in interfaceInitializingBean
- See Also:
-
initialize
public void initialize()Set up the ExecutorService. -
initializeExecutor
protected abstract ExecutorService initializeExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) Create the targetExecutorService
instance. Called byafterPropertiesSet
.- Parameters:
threadFactory
- the ThreadFactory to userejectedExecutionHandler
- the RejectedExecutionHandler to use- Returns:
- a new ExecutorService instance
- See Also:
-
destroy
public void destroy()Callsshutdown
when the BeanFactory destroys the executor instance.- Specified by:
destroy
in interfaceDisposableBean
- See Also:
-
shutdown
public void shutdown()Perform a shutdown on the underlying ExecutorService. -
cancelRemainingTask
Cancel the given remaining task which never commended execution, as returned fromExecutorService.shutdownNow()
.- Parameters:
task
- the task to cancel (typically aRunnableFuture
)- Since:
- 5.0.5
- See Also:
-