|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
public class ThreadPoolTaskExecutor
JavaBean that allows for configuring a JDK 1.5 ThreadPoolExecutor
in bean style (through its "corePoolSize", "maxPoolSize", "keepAliveSeconds", "queueCapacity"
properties), exposing it as a Spring TaskExecutor
.
This is an alternative to configuring a ThreadPoolExecutor instance directly using
constructor injection, with a separate ConcurrentTaskExecutor
adapter wrapping it.
For any custom needs, in particular for defining a
ScheduledThreadPoolExecutor
, it is recommended to
use a straight definition of the Executor instance or a factory method definition
that points to the JDK 1.5 Executors
class.
To expose such a raw Executor as a Spring TaskExecutor
,
simply wrap it with a ConcurrentTaskExecutor
adapter.
NOTE: This class implements Spring's
TaskExecutor
interface as well as the JDK 1.5
Executor
interface, with the former being the primary
interface, the other just serving as secondary convenience. For this reason, the
exception handling follows the TaskExecutor contract rather than the Executor contract,
in particular regarding the TaskRejectedException
.
TaskExecutor
,
Executor
,
ThreadPoolExecutor
,
ScheduledThreadPoolExecutor
,
Executors
,
ConcurrentTaskExecutor
Field Summary | |
---|---|
protected Log |
logger
|
Constructor Summary | |
---|---|
ThreadPoolTaskExecutor()
|
Method Summary | |
---|---|
void |
afterPropertiesSet()
Calls initialize() after the container applied all property values. |
protected BlockingQueue |
createQueue(int queueCapacity)
Create the BlockingQueue to use for the ThreadPoolExecutor. |
void |
destroy()
Calls shutdown when the BeanFactory destroys
the task executor instance. |
void |
execute(Runnable task)
Implementation of both the JDK 1.5 Executor interface and the Spring TaskExecutor interface, delegating to the ThreadPoolExecutor instance. |
int |
getActiveCount()
Return the number of currently active threads. |
int |
getCorePoolSize()
Return the ThreadPoolExecutor's core pool size. |
int |
getKeepAliveSeconds()
Return the ThreadPoolExecutor's keep-alive seconds. |
int |
getMaxPoolSize()
Return the ThreadPoolExecutor's maximum pool size. |
int |
getPoolSize()
Return the current pool size. |
ThreadPoolExecutor |
getThreadPoolExecutor()
Return the underlying ThreadPoolExecutor for native access. |
void |
initialize()
Creates the BlockingQueue and the ThreadPoolExecutor. |
boolean |
prefersShortLivedTasks()
This task executor prefers short-lived work units. |
void |
setBeanName(String name)
Set the name of the bean in the bean factory that created this bean. |
void |
setCorePoolSize(int corePoolSize)
Set the ThreadPoolExecutor's core pool size. |
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. |
void |
setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler)
Set the RejectedExecutionHandler to use for the ThreadPoolExecutor. |
void |
setThreadFactory(ThreadFactory threadFactory)
Set the ThreadFactory to use for the ThreadPoolExecutor's thread pool. |
void |
shutdown()
Perform a shutdown on the ThreadPoolExecutor. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected final Log logger
Constructor Detail |
---|
public ThreadPoolTaskExecutor()
Method Detail |
---|
public void setCorePoolSize(int corePoolSize)
This setting can be modified at runtime, for example through JMX.
public int getCorePoolSize()
public void setMaxPoolSize(int maxPoolSize)
Integer.MAX_VALUE
.
This setting can be modified at runtime, for example through JMX.
public int getMaxPoolSize()
public void setKeepAliveSeconds(int keepAliveSeconds)
This setting can be modified at runtime, for example through JMX.
public int getKeepAliveSeconds()
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
,
SynchronousQueue
public void setThreadFactory(ThreadFactory threadFactory)
Executors.defaultThreadFactory()
public void setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler)
ThreadPoolExecutor.AbortPolicy
public void setBeanName(String name)
BeanNameAware
Invoked after population of normal bean properties but before an
init callback such as InitializingBean.afterPropertiesSet()
or a custom init-method.
setBeanName
in interface BeanNameAware
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 the BeanFactoryUtils.originalBeanName(String)
method to extract the original bean name (without suffix), if desired.public void afterPropertiesSet()
initialize()
after the container applied all property values.
afterPropertiesSet
in interface InitializingBean
initialize()
public void initialize()
createQueue(int)
protected BlockingQueue createQueue(int queueCapacity)
A LinkedBlockingQueue instance will be created for a positive capacity value; a SynchronousQueue else.
queueCapacity
- the specified queue capacity
LinkedBlockingQueue
,
SynchronousQueue
public ThreadPoolExecutor getThreadPoolExecutor() throws IllegalStateException
null
)
IllegalStateException
- if the ThreadPoolTaskExecutor hasn't been initialized yetpublic void execute(Runnable task)
execute
in interface Executor
execute
in interface TaskExecutor
task
- the Runnable
to execute (never null
)Executor.execute(Runnable)
,
TaskExecutor.execute(Runnable)
public boolean prefersShortLivedTasks()
prefersShortLivedTasks
in interface SchedulingTaskExecutor
true
if this TaskExecutor
prefers
short-lived taskspublic int getPoolSize()
ThreadPoolExecutor.getPoolSize()
public int getActiveCount()
ThreadPoolExecutor.getActiveCount()
public void destroy()
shutdown
when the BeanFactory destroys
the task executor instance.
destroy
in interface DisposableBean
shutdown()
public void shutdown()
ThreadPoolExecutor.shutdown()
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |