Class SubmitterSchedulerTaskInterceptor
- java.lang.Object
-
- org.threadly.concurrent.wrapper.interceptor.ExecutorTaskInterceptor
-
- org.threadly.concurrent.wrapper.interceptor.SubmitterSchedulerTaskInterceptor
-
- All Implemented Interfaces:
java.util.concurrent.Executor
,SubmitterExecutor
,SubmitterScheduler
- Direct Known Subclasses:
SchedulerServiceTaskInterceptor
public class SubmitterSchedulerTaskInterceptor extends ExecutorTaskInterceptor implements SubmitterScheduler
Class to wrapSubmitterScheduler
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 toSubmitterSchedulerTaskInterceptor(SubmitterScheduler, BiFunction)
, orwrapTask(Runnable, boolean)
can be overridden to provide the task which should be submitted to theSubmitterScheduler
. Please see the javadocs ofwrapTask(Runnable, boolean)
for more details about ways a task can be modified or wrapped.Other variants of task wrappers:
ExecutorTaskInterceptor
,SchedulerServiceTaskInterceptor
,PrioritySchedulerTaskInterceptor
.- Since:
- 4.6.0
-
-
Constructor Summary
Constructors Constructor Description SubmitterSchedulerTaskInterceptor(SubmitterScheduler parentScheduler, java.util.function.BiFunction<java.lang.Runnable,java.lang.Boolean,java.lang.Runnable> taskManipulator)
Constructs a wrapper forSubmitterScheduler
pool so that tasks can be intercepted and modified, before being submitted to the pool.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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.lang.Runnable task, T result, long delayInMs)
Schedule a task with a given delay.<T> ListenableFuture<T>
submitScheduled(java.util.concurrent.Callable<T> task, long delayInMs)
Schedule aCallable
with a given delay.java.lang.Runnable
wrapTask(java.lang.Runnable task)
Overridden version which delegates towrapTask(Runnable, boolean)
.java.lang.Runnable
wrapTask(java.lang.Runnable task, boolean recurring)
Implementation to modify a provided task.-
Methods inherited from class org.threadly.concurrent.wrapper.interceptor.ExecutorTaskInterceptor
execute, submit, submit
-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.threadly.concurrent.SubmitterExecutor
submit, submit, submit
-
Methods inherited from interface org.threadly.concurrent.SubmitterScheduler
submitScheduled
-
-
-
-
Constructor Detail
-
SubmitterSchedulerTaskInterceptor
public SubmitterSchedulerTaskInterceptor(SubmitterScheduler parentScheduler, java.util.function.BiFunction<java.lang.Runnable,java.lang.Boolean,java.lang.Runnable> taskManipulator)
Constructs a wrapper forSubmitterScheduler
pool so that tasks can be intercepted and modified, before being submitted to the pool.- Parameters:
parentScheduler
- An instance ofSubmitterScheduler
to wraptaskManipulator
- A lambda to manipulate aRunnable
that was submitted for execution
-
-
Method Detail
-
wrapTask
public final java.lang.Runnable wrapTask(java.lang.Runnable task)
Overridden version which delegates towrapTask(Runnable, boolean)
. There should be no reason to override this, instead just ensure thatwrapTask(Runnable, boolean)
is implemented.- Overrides:
wrapTask
in classExecutorTaskInterceptor
- Parameters:
task
- A runnable that was submitted for execution- Returns:
- A non-null task that will be provided to the parent executor
-
wrapTask
public java.lang.Runnable wrapTask(java.lang.Runnable task, boolean recurring)
Implementation to modify a provided task. The provided runnable will be the one submitted to the Executor, unless aCallable
was submitted, in which case aListenableFutureTask
will be provided. In the last condition the original callable can be retrieved by invokingListenableFutureTask.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 desiredDoNothingRunnable.instance()
may be provided. However caution should be used in that if aListenableFutureTask
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 forListenableFutureTask
and may not execute/return the provided task, then you should be careful to ensureFuture.cancel(boolean)
is invoked.Public visibility for javadoc visibility.
- Parameters:
task
- A runnable that was submitted for executionrecurring
-true
if the provided task is a recurring task- Returns:
- A non-null task that will be provided to the parent executor
-
schedule
public void schedule(java.lang.Runnable task, long delayInMs)
Description copied from interface:SubmitterScheduler
Schedule a one time task with a given delay.- Specified by:
schedule
in interfaceSubmitterScheduler
- Parameters:
task
- runnable to executedelayInMs
- time in milliseconds to wait to execute task
-
submitScheduled
public <T> ListenableFuture<T> submitScheduled(java.lang.Runnable task, T result, long delayInMs)
Description copied from interface:SubmitterScheduler
Schedule a task with a given delay. TheFuture.get()
method will return the provided result once the runnable has completed.- Specified by:
submitScheduled
in interfaceSubmitterScheduler
- Type Parameters:
T
- type of result returned from the future- Parameters:
task
- runnable to executeresult
- result to be returned from resulting future .get() when runnable completesdelayInMs
- time in milliseconds to wait to execute task- Returns:
- a future to know when the task has completed
-
submitScheduled
public <T> ListenableFuture<T> submitScheduled(java.util.concurrent.Callable<T> task, long delayInMs)
Description copied from interface:SubmitterScheduler
Schedule aCallable
with a given delay. This is needed when a result needs to be consumed from the callable.- Specified by:
submitScheduled
in interfaceSubmitterScheduler
- Type Parameters:
T
- type of result returned from the future- Parameters:
task
- callable to be executeddelayInMs
- time in milliseconds to wait to execute task- Returns:
- a future to know when the task has completed and get the result of the callable
-
scheduleWithFixedDelay
public void scheduleWithFixedDelay(java.lang.Runnable task, long initialDelay, long recurringDelay)
Description copied from interface:SubmitterScheduler
Schedule a fixed delay recurring task to run. The recurring delay time will be from the point where execution has finished. So the execution frequency is therecurringDelay + 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).- Specified by:
scheduleWithFixedDelay
in interfaceSubmitterScheduler
- Parameters:
task
- runnable to be executedinitialDelay
- delay in milliseconds until first runrecurringDelay
- delay in milliseconds for running task after last finish
-
scheduleAtFixedRate
public void scheduleAtFixedRate(java.lang.Runnable task, long initialDelay, long period)
Description copied from interface:SubmitterScheduler
Schedule a fixed rate recurring task to run. The recurring delay will be the same, regardless of how long task execution takes. A given runnable will not run concurrently (unless it is submitted to the scheduler multiple times). Instead of execution takes longer than the period, the next run will occur immediately (given thread availability in the pool).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).- Specified by:
scheduleAtFixedRate
in interfaceSubmitterScheduler
- Parameters:
task
- runnable to be executedinitialDelay
- delay in milliseconds until first runperiod
- amount of time in milliseconds between the start of recurring executions
-
-