public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport implements AsyncListenableTaskExecutor, SchedulingTaskExecutor, TaskScheduler
TaskScheduler
interface, wrapping
a native ScheduledThreadPoolExecutor
and providing
all applicable configuration options for it.setPoolSize(int)
,
setRemoveOnCancelPolicy(boolean)
,
setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean)
,
setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean)
,
ExecutorConfigurationSupport.setThreadFactory(java.util.concurrent.ThreadFactory)
,
setErrorHandler(org.springframework.util.ErrorHandler)
,
Serialized Formlogger
TIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITE
Constructor and Description |
---|
ThreadPoolTaskScheduler() |
Modifier and Type | Method and Description |
---|---|
protected void |
cancelRemainingTask(Runnable task)
Cancel the given remaining task which never commended execution,
as returned from
ExecutorService.shutdownNow() . |
protected ScheduledExecutorService |
createExecutor(int poolSize,
ThreadFactory threadFactory,
RejectedExecutionHandler rejectedExecutionHandler)
Create a new
ScheduledExecutorService instance. |
void |
execute(Runnable task)
Execute the given
task . |
void |
execute(Runnable task,
long startTimeout)
Deprecated.
|
int |
getActiveCount()
Return the number of currently active threads.
|
Clock |
getClock()
Return the clock to use for scheduling purposes.
|
int |
getPoolSize()
Return the current pool size.
|
ScheduledExecutorService |
getScheduledExecutor()
Return the underlying ScheduledExecutorService for native access.
|
ScheduledThreadPoolExecutor |
getScheduledThreadPoolExecutor()
Return the underlying ScheduledThreadPoolExecutor, if available.
|
protected ExecutorService |
initializeExecutor(ThreadFactory threadFactory,
RejectedExecutionHandler rejectedExecutionHandler)
Create the target
ExecutorService instance. |
boolean |
isRemoveOnCancelPolicy()
Deprecated.
as of 5.3.9, in favor of direct
getScheduledThreadPoolExecutor() access |
ScheduledFuture<?> |
schedule(Runnable task,
Date startTime)
Schedule the given
Runnable , invoking it at the specified execution time. |
ScheduledFuture<?> |
schedule(Runnable task,
Trigger trigger)
Schedule the given
Runnable , invoking it whenever the trigger
indicates a next execution time. |
ScheduledFuture<?> |
scheduleAtFixedRate(Runnable task,
Date startTime,
long period)
Schedule the given
Runnable , invoking it at the specified execution time
and subsequently with the given period. |
ScheduledFuture<?> |
scheduleAtFixedRate(Runnable task,
long period)
Schedule the given
Runnable , starting as soon as possible and
invoking it with the given period. |
ScheduledFuture<?> |
scheduleWithFixedDelay(Runnable task,
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. |
ScheduledFuture<?> |
scheduleWithFixedDelay(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 |
setClock(Clock clock)
Set the clock to use for scheduling purposes.
|
void |
setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean flag)
Set whether to continue existing periodic tasks even when this executor has been shutdown.
|
void |
setErrorHandler(ErrorHandler errorHandler)
Set a custom
ErrorHandler strategy. |
void |
setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean flag)
Set whether to execute existing delayed tasks even when this executor has been shutdown.
|
void |
setPoolSize(int poolSize)
Set the ScheduledExecutorService's pool size.
|
void |
setRemoveOnCancelPolicy(boolean flag)
Set the remove-on-cancel mode on
ScheduledThreadPoolExecutor . |
<T> Future<T> |
submit(Callable<T> task)
Submit a Callable task for execution, receiving a Future representing that task.
|
Future<?> |
submit(Runnable task)
Submit a Runnable task for execution, receiving a Future representing that task.
|
<T> ListenableFuture<T> |
submitListenable(Callable<T> task)
Submit a
Callable task for execution, receiving a ListenableFuture
representing that task. |
ListenableFuture<?> |
submitListenable(Runnable task)
Submit a
Runnable task for execution, receiving a ListenableFuture
representing that task. |
afterPropertiesSet, destroy, initialize, setAwaitTerminationMillis, setAwaitTerminationSeconds, setBeanName, setRejectedExecutionHandler, setThreadFactory, setThreadNamePrefix, setWaitForTasksToCompleteOnShutdown, shutdown
newThread
createThread, getDefaultThreadNamePrefix, getThreadGroup, getThreadNamePrefix, getThreadPriority, isDaemon, nextThreadName, setDaemon, setThreadGroup, setThreadGroupName, setThreadPriority
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
prefersShortLivedTasks
schedule, scheduleAtFixedRate, scheduleAtFixedRate, scheduleWithFixedDelay, scheduleWithFixedDelay
public void setPoolSize(int poolSize)
This setting can be modified at runtime, for example through JMX.
public void setRemoveOnCancelPolicy(boolean flag)
ScheduledThreadPoolExecutor
.
Default is false
. If set to true
, the target executor will be
switched into remove-on-cancel mode (if possible).
This setting can be modified at runtime, for example through JMX.
public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean flag)
Default is false
. If set to true
, the target executor will be
switched into continuing periodic tasks (if possible).
This setting can be modified at runtime, for example through JMX.
ScheduledThreadPoolExecutor.setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean)
public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean flag)
Default is true
. If set to false
, the target executor will be
switched into dropping remaining tasks (if possible).
This setting can be modified at runtime, for example through JMX.
ScheduledThreadPoolExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean)
public void setErrorHandler(ErrorHandler errorHandler)
ErrorHandler
strategy.public void setClock(Clock clock)
The default clock is the system clock for the default time zone.
Clock.systemDefaultZone()
public Clock getClock()
TaskScheduler
getClock
in interface TaskScheduler
Clock.systemDefaultZone()
protected ExecutorService initializeExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler)
ExecutorConfigurationSupport
ExecutorService
instance.
Called by afterPropertiesSet
.initializeExecutor
in class ExecutorConfigurationSupport
threadFactory
- the ThreadFactory to userejectedExecutionHandler
- the RejectedExecutionHandler to useExecutorConfigurationSupport.afterPropertiesSet()
protected ScheduledExecutorService createExecutor(int poolSize, ThreadFactory threadFactory, 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()
,
ScheduledThreadPoolExecutor
public ScheduledExecutorService getScheduledExecutor() throws IllegalStateException
null
)IllegalStateException
- if the ThreadPoolTaskScheduler hasn't been initialized yetpublic ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor() throws IllegalStateException
null
)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
.
public int getActiveCount()
Requires an underlying ScheduledThreadPoolExecutor
.
@Deprecated public boolean isRemoveOnCancelPolicy()
getScheduledThreadPoolExecutor()
accessRequires an underlying ScheduledThreadPoolExecutor
.
public void execute(Runnable task)
TaskExecutor
task
.
The call might return immediately if the implementation uses an asynchronous execution strategy, or might block in the case of synchronous execution.
execute
in interface Executor
execute
in interface TaskExecutor
task
- the Runnable
to execute (never null
)@Deprecated public void execute(Runnable task, long startTimeout)
AsyncTaskExecutor
task
.execute
in interface AsyncTaskExecutor
task
- the Runnable
to execute (never null
)startTimeout
- the time duration (milliseconds) within which the task is
supposed to start. This is intended as a hint to the executor, allowing for
preferred handling of immediate tasks. Typical values are AsyncTaskExecutor.TIMEOUT_IMMEDIATE
or AsyncTaskExecutor.TIMEOUT_INDEFINITE
(the default as used by TaskExecutor.execute(Runnable)
).TaskExecutor.execute(Runnable)
public Future<?> submit(Runnable task)
AsyncTaskExecutor
null
result upon completion.submit
in interface AsyncTaskExecutor
task
- the Runnable
to execute (never null
)public <T> Future<T> submit(Callable<T> task)
AsyncTaskExecutor
submit
in interface AsyncTaskExecutor
task
- the Callable
to execute (never null
)public ListenableFuture<?> submitListenable(Runnable task)
AsyncListenableTaskExecutor
Runnable
task for execution, receiving a ListenableFuture
representing that task. The Future will return a null
result upon completion.submitListenable
in interface AsyncListenableTaskExecutor
task
- the Runnable
to execute (never null
)ListenableFuture
representing pending completion of the taskpublic <T> ListenableFuture<T> submitListenable(Callable<T> task)
AsyncListenableTaskExecutor
Callable
task for execution, receiving a ListenableFuture
representing that task. The Future will return the Callable's result upon
completion.submitListenable
in interface AsyncListenableTaskExecutor
task
- the Callable
to execute (never null
)ListenableFuture
representing pending completion of the taskprotected void cancelRemainingTask(Runnable task)
ExecutorConfigurationSupport
ExecutorService.shutdownNow()
.cancelRemainingTask
in class ExecutorConfigurationSupport
task
- the task to cancel (typically a RunnableFuture
)ExecutorConfigurationSupport.shutdown()
,
Future.cancel(boolean)
@Nullable public ScheduledFuture<?> schedule(Runnable task, Trigger trigger)
TaskScheduler
Runnable
, 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 TaskScheduler
task
- 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)
)CronTrigger
public ScheduledFuture<?> schedule(Runnable task, Date startTime)
TaskScheduler
Runnable
, invoking it at the specified execution time.
Execution will end once the scheduler shuts down or the returned
ScheduledFuture
gets cancelled.
schedule
in interface TaskScheduler
task
- 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 ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period)
TaskScheduler
Runnable
, 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 TaskScheduler
task
- 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 ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period)
TaskScheduler
Runnable
, 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 TaskScheduler
task
- 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 ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay)
TaskScheduler
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.
Execution will end once the scheduler shuts down or the returned
ScheduledFuture
gets cancelled.
scheduleWithFixedDelay
in interface TaskScheduler
task
- 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 ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay)
TaskScheduler
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.
Execution will end once the scheduler shuts down or the returned
ScheduledFuture
gets cancelled.
scheduleWithFixedDelay
in interface TaskScheduler
task
- the Runnable to execute whenever the trigger firesdelay
- the delay between the completion of one execution and the start of the next
(in milliseconds)ScheduledFuture
representing pending completion of the task