public class ThreadPoolExecutorFactoryBean extends ExecutorConfigurationSupport implements org.springframework.beans.factory.FactoryBean<java.util.concurrent.ExecutorService>, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean
ThreadPoolExecutor
in bean style (through its "corePoolSize", "maxPoolSize", "keepAliveSeconds",
"queueCapacity" properties) and exposing it as a bean reference of its native
ExecutorService type.
For an alternative, you may set up a ThreadPoolExecutor instance directly
using constructor injection, or use a factory method definition that points to the
Executors class.
This is strongly recommended in particular for common @Bean methods in
configuration classes, where this FactoryBean variant would force you to
return the FactoryBean type instead of the actual Executor type.
If you need a timing-based ScheduledExecutorService
instead, consider ScheduledExecutorFactoryBean.
ExecutorService,
Executors,
ThreadPoolExecutor,
Serialized Formlogger| Constructor and Description |
|---|
ThreadPoolExecutorFactoryBean() |
| Modifier and Type | Method and Description |
|---|---|
protected java.util.concurrent.ThreadPoolExecutor |
createExecutor(int corePoolSize,
int maxPoolSize,
int keepAliveSeconds,
java.util.concurrent.BlockingQueue<java.lang.Runnable> queue,
java.util.concurrent.ThreadFactory threadFactory,
java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler)
Create a new instance of
ThreadPoolExecutor or a subclass thereof. |
protected java.util.concurrent.BlockingQueue<java.lang.Runnable> |
createQueue(int queueCapacity)
Create the BlockingQueue to use for the ThreadPoolExecutor.
|
java.util.concurrent.ExecutorService |
getObject() |
java.lang.Class<? extends java.util.concurrent.ExecutorService> |
getObjectType() |
protected java.util.concurrent.ExecutorService |
initializeExecutor(java.util.concurrent.ThreadFactory threadFactory,
java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler)
Create the target
ExecutorService instance. |
boolean |
isSingleton() |
void |
setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut)
Specify whether to allow core threads to time out.
|
void |
setCorePoolSize(int corePoolSize)
Set the ThreadPoolExecutor's core pool size.
|
void |
setExposeUnconfigurableExecutor(boolean exposeUnconfigurableExecutor)
Specify whether this FactoryBean should expose an unconfigurable
decorator for the created executor.
|
void |
setKeepAliveSeconds(int keepAliveSeconds)
Set the ThreadPoolExecutor's keep-alive seconds.
|
void |
setMaxPoolSize(int maxPoolSize)
Set the ThreadPoolExecutor's maximum pool size.
|
void |
setQueueCapacity(int queueCapacity)
Set the capacity for the ThreadPoolExecutor's BlockingQueue.
|
afterPropertiesSet, destroy, initialize, setAwaitTerminationSeconds, setBeanName, setRejectedExecutionHandler, setThreadFactory, setThreadNamePrefix, setWaitForTasksToCompleteOnShutdown, shutdownnewThreadcreateThread, getDefaultThreadNamePrefix, getThreadGroup, getThreadNamePrefix, getThreadPriority, isDaemon, nextThreadName, setDaemon, setThreadGroup, setThreadGroupName, setThreadPriorityclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitpublic void setCorePoolSize(int corePoolSize)
public void setMaxPoolSize(int maxPoolSize)
Integer.MAX_VALUE.public void setKeepAliveSeconds(int keepAliveSeconds)
public void setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut)
Default is "false".
ThreadPoolExecutor.allowCoreThreadTimeOut(boolean)public void setQueueCapacity(int queueCapacity)
Integer.MAX_VALUE.
Any positive value will lead to a LinkedBlockingQueue instance; any other value will lead to a SynchronousQueue instance.
LinkedBlockingQueue,
SynchronousQueuepublic void setExposeUnconfigurableExecutor(boolean exposeUnconfigurableExecutor)
Default is "false", exposing the raw executor as bean reference. Switch this flag to "true" to strictly prevent clients from modifying the executor's configuration.
Executors.unconfigurableExecutorService(java.util.concurrent.ExecutorService)protected java.util.concurrent.ExecutorService initializeExecutor(java.util.concurrent.ThreadFactory threadFactory,
java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler)
ExecutorConfigurationSupportExecutorService instance.
Called by afterPropertiesSet.initializeExecutor in class ExecutorConfigurationSupportthreadFactory - the ThreadFactory to userejectedExecutionHandler - the RejectedExecutionHandler to useExecutorConfigurationSupport.afterPropertiesSet()protected java.util.concurrent.ThreadPoolExecutor createExecutor(int corePoolSize,
int maxPoolSize,
int keepAliveSeconds,
java.util.concurrent.BlockingQueue<java.lang.Runnable> queue,
java.util.concurrent.ThreadFactory threadFactory,
java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler)
ThreadPoolExecutor or a subclass thereof.
The default implementation creates a standard ThreadPoolExecutor.
Can be overridden to provide custom ThreadPoolExecutor subclasses.
corePoolSize - the specified core pool sizemaxPoolSize - the specified maximum pool sizekeepAliveSeconds - the specified keep-alive time in secondsqueue - the BlockingQueue to usethreadFactory - the ThreadFactory to userejectedExecutionHandler - the RejectedExecutionHandler to useExecutorConfigurationSupport.afterPropertiesSet()protected java.util.concurrent.BlockingQueue<java.lang.Runnable> createQueue(int queueCapacity)
A LinkedBlockingQueue instance will be created for a positive capacity value; a SynchronousQueue else.
queueCapacity - the specified queue capacityLinkedBlockingQueue,
SynchronousQueuepublic java.util.concurrent.ExecutorService getObject()
getObject in interface org.springframework.beans.factory.FactoryBean<java.util.concurrent.ExecutorService>public java.lang.Class<? extends java.util.concurrent.ExecutorService> getObjectType()
getObjectType in interface org.springframework.beans.factory.FactoryBean<java.util.concurrent.ExecutorService>public boolean isSingleton()
isSingleton in interface org.springframework.beans.factory.FactoryBean<java.util.concurrent.ExecutorService>