public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport implements org.springframework.core.task.AsyncListenableTaskExecutor, SchedulingTaskExecutor, TaskScheduler
TaskScheduler interface, wrapping
a native ScheduledThreadPoolExecutor.setPoolSize(int),
setRemoveOnCancelPolicy(boolean),
ExecutorConfigurationSupport.setThreadFactory(java.util.concurrent.ThreadFactory),
setErrorHandler(org.springframework.util.ErrorHandler),
Serialized Formlogger| Constructor and Description |
|---|
ThreadPoolTaskScheduler() |
| Modifier and Type | Method and Description |
|---|---|
protected java.util.concurrent.ScheduledExecutorService |
createExecutor(int poolSize,
java.util.concurrent.ThreadFactory threadFactory,
java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler)
Create a new
ScheduledExecutorService instance. |
void |
execute(java.lang.Runnable task) |
void |
execute(java.lang.Runnable task,
long startTimeout) |
int |
getActiveCount()
Return the number of currently active threads.
|
int |
getPoolSize()
Return the current pool size.
|
java.util.concurrent.ScheduledExecutorService |
getScheduledExecutor()
Return the underlying ScheduledExecutorService for native access.
|
java.util.concurrent.ScheduledThreadPoolExecutor |
getScheduledThreadPoolExecutor()
Return the underlying ScheduledThreadPoolExecutor, if available.
|
protected java.util.concurrent.ExecutorService |
initializeExecutor(java.util.concurrent.ThreadFactory threadFactory,
java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler)
Create the target
ExecutorService instance. |
boolean |
isRemoveOnCancelPolicy()
Return the current setting for the remove-on-cancel mode.
|
boolean |
prefersShortLivedTasks()
Does this
TaskExecutor prefer short-lived tasks over
long-lived tasks? |
java.util.concurrent.ScheduledFuture<?> |
schedule(java.lang.Runnable task,
java.util.Date startTime)
Schedule the given
Runnable, invoking it at the specified execution time. |
java.util.concurrent.ScheduledFuture<?> |
schedule(java.lang.Runnable task,
Trigger trigger)
Schedule the given
Runnable, invoking it whenever the trigger
indicates a next execution time. |
java.util.concurrent.ScheduledFuture<?> |
scheduleAtFixedRate(java.lang.Runnable task,
java.util.Date startTime,
long period)
Schedule the given
Runnable, invoking it at the specified execution time
and subsequently with the given period. |
java.util.concurrent.ScheduledFuture<?> |
scheduleAtFixedRate(java.lang.Runnable task,
long period)
Schedule the given
Runnable, starting as soon as possible and
invoking it with the given period. |
java.util.concurrent.ScheduledFuture<?> |
scheduleWithFixedDelay(java.lang.Runnable task,
java.util.Date startTime,
long delay)
Schedule the given
Runnable, invoking it at the specified execution time
and subsequently with the given delay between the completion of one execution
and the start of the next. |
java.util.concurrent.ScheduledFuture<?> |
scheduleWithFixedDelay(java.lang.Runnable task,
long delay)
Schedule the given
Runnable, starting as soon as possible and
invoking it with the given delay between the completion of one execution
and the start of the next. |
void |
setErrorHandler(org.springframework.util.ErrorHandler errorHandler)
Set a custom
ErrorHandler strategy. |
void |
setPoolSize(int poolSize)
Set the ScheduledExecutorService's pool size.
|
void |
setRemoveOnCancelPolicy(boolean removeOnCancelPolicy)
Set the remove-on-cancel mode on
ScheduledThreadPoolExecutor (JDK 7+). |
<T> java.util.concurrent.Future<T> |
submit(java.util.concurrent.Callable<T> task) |
java.util.concurrent.Future<?> |
submit(java.lang.Runnable task) |
<T> org.springframework.util.concurrent.ListenableFuture<T> |
submitListenable(java.util.concurrent.Callable<T> task) |
org.springframework.util.concurrent.ListenableFuture<?> |
submitListenable(java.lang.Runnable task) |
afterPropertiesSet, destroy, initialize, setAwaitTerminationSeconds, setBeanName, setRejectedExecutionHandler, setThreadFactory, setThreadNamePrefix, setWaitForTasksToCompleteOnShutdown, shutdownnewThreadpublic void setPoolSize(int poolSize)
This setting can be modified at runtime, for example through JMX.
@UsesJava7 public void setRemoveOnCancelPolicy(boolean removeOnCancelPolicy)
ScheduledThreadPoolExecutor (JDK 7+).
Default is false. If set to true, the target executor will be
switched into remove-on-cancel mode (if possible, with a soft fallback otherwise).
This setting can be modified at runtime, for example through JMX.
public void setErrorHandler(org.springframework.util.ErrorHandler errorHandler)
ErrorHandler strategy.@UsesJava7
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.ScheduledExecutorService createExecutor(int poolSize,
java.util.concurrent.ThreadFactory threadFactory,
java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler)
ScheduledExecutorService instance.
The default implementation creates a ScheduledThreadPoolExecutor.
Can be overridden in subclasses to provide custom ScheduledExecutorService instances.
poolSize - the specified pool sizethreadFactory - the ThreadFactory to userejectedExecutionHandler - the RejectedExecutionHandler to useExecutorConfigurationSupport.afterPropertiesSet(),
ScheduledThreadPoolExecutorpublic java.util.concurrent.ScheduledExecutorService getScheduledExecutor()
throws java.lang.IllegalStateException
null)java.lang.IllegalStateException - if the ThreadPoolTaskScheduler hasn't been initialized yetpublic java.util.concurrent.ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor()
throws java.lang.IllegalStateException
null)java.lang.IllegalStateException - if the ThreadPoolTaskScheduler hasn't been initialized yet
or if the underlying ScheduledExecutorService isn't a ScheduledThreadPoolExecutorgetScheduledExecutor()public int getPoolSize()
Requires an underlying ScheduledThreadPoolExecutor.
getScheduledThreadPoolExecutor(),
ThreadPoolExecutor.getPoolSize()@UsesJava7 public boolean isRemoveOnCancelPolicy()
Requires an underlying ScheduledThreadPoolExecutor.
public int getActiveCount()
Requires an underlying ScheduledThreadPoolExecutor.
getScheduledThreadPoolExecutor(),
ThreadPoolExecutor.getActiveCount()public void execute(java.lang.Runnable task)
execute in interface java.util.concurrent.Executorexecute in interface org.springframework.core.task.TaskExecutorpublic void execute(java.lang.Runnable task,
long startTimeout)
execute in interface org.springframework.core.task.AsyncTaskExecutorpublic java.util.concurrent.Future<?> submit(java.lang.Runnable task)
submit in interface org.springframework.core.task.AsyncTaskExecutorpublic <T> java.util.concurrent.Future<T> submit(java.util.concurrent.Callable<T> task)
submit in interface org.springframework.core.task.AsyncTaskExecutorpublic org.springframework.util.concurrent.ListenableFuture<?> submitListenable(java.lang.Runnable task)
submitListenable in interface org.springframework.core.task.AsyncListenableTaskExecutorpublic <T> org.springframework.util.concurrent.ListenableFuture<T> submitListenable(java.util.concurrent.Callable<T> task)
submitListenable in interface org.springframework.core.task.AsyncListenableTaskExecutorpublic boolean prefersShortLivedTasks()
SchedulingTaskExecutorTaskExecutor prefer short-lived tasks over
long-lived tasks?
A SchedulingTaskExecutor implementation can indicate
whether it prefers submitted tasks to perform as little work as they
can within a single task execution. For example, submitted tasks
might break a repeated loop into individual subtasks which submit a
follow-up task afterwards (if feasible).
This should be considered a hint. Of course TaskExecutor
clients are free to ignore this flag and hence the
SchedulingTaskExecutor interface overall. However, thread
pools will usually indicated a preference for short-lived tasks, to be
able to perform more fine-grained scheduling.
prefersShortLivedTasks in interface SchedulingTaskExecutortrue if this TaskExecutor prefers
short-lived taskspublic java.util.concurrent.ScheduledFuture<?> schedule(java.lang.Runnable task,
Trigger trigger)
TaskSchedulerRunnable, invoking it whenever the trigger
indicates a next execution time.
Execution will end once the scheduler shuts down or the returned
ScheduledFuture gets cancelled.
schedule in interface TaskSchedulertask - the Runnable to execute whenever the trigger firestrigger - an implementation of the Trigger interface,
e.g. a CronTrigger object
wrapping a cron expressionScheduledFuture representing pending completion of the task,
or null if the given Trigger object never fires (i.e. returns
null from Trigger.nextExecutionTime(org.springframework.scheduling.TriggerContext))CronTriggerpublic java.util.concurrent.ScheduledFuture<?> schedule(java.lang.Runnable task,
java.util.Date startTime)
TaskSchedulerRunnable, invoking it at the specified execution time.
Execution will end once the scheduler shuts down or the returned
ScheduledFuture gets cancelled.
schedule in interface TaskSchedulertask - the Runnable to execute whenever the trigger firesstartTime - the desired execution time for the task
(if this is in the past, the task will be executed immediately, i.e. as soon as possible)ScheduledFuture representing pending completion of the taskpublic java.util.concurrent.ScheduledFuture<?> scheduleAtFixedRate(java.lang.Runnable task,
java.util.Date startTime,
long period)
TaskSchedulerRunnable, invoking it at the specified execution time
and subsequently with the given period.
Execution will end once the scheduler shuts down or the returned
ScheduledFuture gets cancelled.
scheduleAtFixedRate in interface TaskSchedulertask - the Runnable to execute whenever the trigger firesstartTime - the desired first execution time for the task
(if this is in the past, the task will be executed immediately, i.e. as soon as possible)period - the interval between successive executions of the task (in milliseconds)ScheduledFuture representing pending completion of the taskpublic java.util.concurrent.ScheduledFuture<?> scheduleAtFixedRate(java.lang.Runnable task,
long period)
TaskSchedulerRunnable, starting as soon as possible and
invoking it with the given period.
Execution will end once the scheduler shuts down or the returned
ScheduledFuture gets cancelled.
scheduleAtFixedRate in interface TaskSchedulertask - the Runnable to execute whenever the trigger firesperiod - the interval between successive executions of the task (in milliseconds)ScheduledFuture representing pending completion of the taskpublic java.util.concurrent.ScheduledFuture<?> scheduleWithFixedDelay(java.lang.Runnable task,
java.util.Date startTime,
long delay)
TaskSchedulerRunnable, invoking it at the specified execution time
and subsequently with the given delay between the completion of one execution
and the start of the next.
Execution will end once the scheduler shuts down or the returned
ScheduledFuture gets cancelled.
scheduleWithFixedDelay in interface TaskSchedulertask - the Runnable to execute whenever the trigger firesstartTime - the desired first execution time for the task
(if this is in the past, the task will be executed immediately, i.e. as soon as possible)delay - the delay between the completion of one execution and the start
of the next (in milliseconds)ScheduledFuture representing pending completion of the taskpublic java.util.concurrent.ScheduledFuture<?> scheduleWithFixedDelay(java.lang.Runnable task,
long delay)
TaskSchedulerRunnable, starting as soon as possible and
invoking it with the given delay between the completion of one execution
and the start of the next.
Execution will end once the scheduler shuts down or the returned
ScheduledFuture gets cancelled.
scheduleWithFixedDelay in interface TaskSchedulertask - the Runnable to execute whenever the trigger firesdelay - the interval between successive executions of the task (in milliseconds)ScheduledFuture representing pending completion of the task