public class SubmitterSchedulerTaskInterceptor extends ExecutorTaskInterceptor implements SubmitterScheduler
SubmitterScheduler
pool so that tasks can be intercepted and either
wrapped, or modified, before being submitted to the pool. This class can be passed a lambda to
SubmitterSchedulerTaskInterceptor(SubmitterScheduler, BiFunction)
, or
wrapTask(Runnable, boolean)
can be overridden to provide the task which should be
submitted to the SubmitterScheduler
. Please see the javadocs of
wrapTask(Runnable, boolean)
for more details about ways a task can be modified or
wrapped.
Other variants of task wrappers: ExecutorTaskInterceptor
,
SchedulerServiceTaskInterceptor
, PrioritySchedulerTaskInterceptor
.
Constructor and Description |
---|
SubmitterSchedulerTaskInterceptor(SubmitterScheduler parentScheduler,
java.util.function.BiFunction<java.lang.Runnable,java.lang.Boolean,java.lang.Runnable> taskManipulator)
Constructs a wrapper for
SubmitterScheduler pool so that tasks can be intercepted
and modified, before being submitted to the pool. |
Modifier and Type | Method and Description |
---|---|
void |
schedule(java.lang.Runnable task,
long delayInMs)
Schedule a one time task with a given delay.
|
void |
scheduleAtFixedRate(java.lang.Runnable task,
long initialDelay,
long period)
Schedule a fixed rate recurring task to run.
|
void |
scheduleWithFixedDelay(java.lang.Runnable task,
long initialDelay,
long recurringDelay)
Schedule a fixed delay recurring task to run.
|
<T> ListenableFuture<T> |
submitScheduled(java.util.concurrent.Callable<T> task,
long delayInMs)
Schedule a
Callable with a given delay. |
<T> ListenableFuture<T> |
submitScheduled(java.lang.Runnable task,
T result,
long delayInMs)
Schedule a task with a given delay.
|
java.lang.Runnable |
wrapTask(java.lang.Runnable task)
Overridden version which delegates to
wrapTask(Runnable, boolean) . |
java.lang.Runnable |
wrapTask(java.lang.Runnable task,
boolean recurring)
Implementation to modify a provided task.
|
execute, submit, submit
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
submitScheduled
submit, submit, submit
public SubmitterSchedulerTaskInterceptor(SubmitterScheduler parentScheduler, java.util.function.BiFunction<java.lang.Runnable,java.lang.Boolean,java.lang.Runnable> taskManipulator)
SubmitterScheduler
pool so that tasks can be intercepted
and modified, before being submitted to the pool.parentScheduler
- An instance of SubmitterScheduler
to wraptaskManipulator
- A lambda to manipulate a Runnable
that was submitted for executionpublic final java.lang.Runnable wrapTask(java.lang.Runnable task)
wrapTask(Runnable, boolean)
. There should be
no reason to override this, instead just ensure that wrapTask(Runnable, boolean)
is
implemented.wrapTask
in class ExecutorTaskInterceptor
task
- A runnable that was submitted for executionpublic java.lang.Runnable wrapTask(java.lang.Runnable task, boolean recurring)
Callable
was submitted, in which case a
ListenableFutureTask
will be provided. In the last condition the original callable
can be retrieved by invoking ListenableFutureTask.getContainedCallable()
. The returned
task can not be null, but could be either the original task, a modified task, a wrapper to the
provided task, or if no action is desired
DoNothingRunnable.instance()
may be provided. However caution
should be used in that if a ListenableFutureTask
is provided, and then never returned
(and not canceled), then the future will never complete (and thus possibly forever blocked).
So if you are doing conditional checks for ListenableFutureTask
and may not
execute/return the provided task, then you should be careful to ensure
FutureTask.cancel(boolean)
is invoked.
Public visibility for javadoc visibility.
task
- A runnable that was submitted for executionrecurring
- true
if the provided task is a recurring taskpublic void schedule(java.lang.Runnable task, long delayInMs)
SubmitterScheduler
schedule
in interface SubmitterScheduler
task
- runnable to executedelayInMs
- time in milliseconds to wait to execute taskpublic <T> ListenableFuture<T> submitScheduled(java.lang.Runnable task, T result, long delayInMs)
SubmitterScheduler
Future.get()
method will return
the provided result once the runnable has completed.submitScheduled
in interface SubmitterScheduler
T
- type of result returned from the futuretask
- runnable to executeresult
- result to be returned from resulting future .get() when runnable completesdelayInMs
- time in milliseconds to wait to execute taskpublic <T> ListenableFuture<T> submitScheduled(java.util.concurrent.Callable<T> task, long delayInMs)
SubmitterScheduler
Callable
with a given delay. This is needed when a result needs to be
consumed from the callable.submitScheduled
in interface SubmitterScheduler
T
- type of result returned from the futuretask
- callable to be executeddelayInMs
- time in milliseconds to wait to execute taskpublic void scheduleWithFixedDelay(java.lang.Runnable task, long initialDelay, long recurringDelay)
SubmitterScheduler
recurringDelay + runtime
for the provided task.
Unlike ScheduledExecutorService
if the task throws an exception,
subsequent executions are NOT suppressed or prevented. So if the task throws an exception on
every run, the task will continue to be executed at the provided recurring delay (possibly
throwing an exception on each execution).
scheduleWithFixedDelay
in interface SubmitterScheduler
task
- runnable to be executedinitialDelay
- delay in milliseconds until first runrecurringDelay
- delay in milliseconds for running task after last finishpublic void scheduleAtFixedRate(java.lang.Runnable task, long initialDelay, long period)
SubmitterScheduler
Unlike ScheduledExecutorService
if the task throws an exception,
subsequent executions are NOT suppressed or prevented. So if the task throws an exception on
every run, the task will continue to be executed at the provided recurring delay (possibly
throwing an exception on each execution).
scheduleAtFixedRate
in interface SubmitterScheduler
task
- runnable to be executedinitialDelay
- delay in milliseconds until first runperiod
- amount of time in milliseconds between the start of recurring executions