public class MonitoringTaskSubmitter
extends java.lang.Object
ScheduledExecutorService
that allows you to monitor the number of submitted, failed and succeeded task, plus has the
ability to wait until all tasks scheduled trough MonitoringTaskSubmitter are completed
= there are no scheduled or running tasks.| Modifier and Type | Field and Description |
|---|---|
protected java.util.concurrent.ScheduledExecutorService |
executor |
protected ReusableCountLatch |
latch |
protected org.slf4j.Logger |
logger |
| Constructor and Description |
|---|
MonitoringTaskSubmitter(java.util.concurrent.ScheduledExecutorService executor) |
| Modifier and Type | Method and Description |
|---|---|
int |
failedCount() |
int |
failedSubmissionCount() |
protected void |
onWorkException(java.lang.Exception e) |
protected void |
onWorkThrowable(java.lang.Throwable t) |
void |
resetCounters()
Resets failedSubmission, succeeded and failed counter.
|
<T> java.util.concurrent.Future<T> |
run(java.util.concurrent.Callable<T> callable)
Schedules a
Callable for immediate execution. |
java.util.concurrent.Future<java.lang.Void> |
run(Task task)
Schedules a
Task for immediate execution
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task. |
<T> java.util.concurrent.Future<T> |
runCallable(java.util.concurrent.Callable<T> callable)
Schedules a
Callable for immediate execution. |
<T> java.util.concurrent.ScheduledFuture<T> |
runCallableIn(long delay,
java.util.concurrent.TimeUnit unit,
java.util.concurrent.Callable<T> callable)
Schedules a
Callable for delayed execution. |
<T> java.util.concurrent.ScheduledFuture<T> |
runIn(long delay,
java.util.concurrent.TimeUnit unit,
java.util.concurrent.Callable<T> callable)
Schedules a
Callable for delayed execution. |
java.util.concurrent.ScheduledFuture<java.lang.Void> |
runIn(long delay,
java.util.concurrent.TimeUnit unit,
Task task)
|
java.util.concurrent.Future<java.lang.Void> |
runRunnable(java.lang.Runnable runnable)
Schedules a
Runnable for immediate execution
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task. |
java.util.concurrent.ScheduledFuture<java.lang.Void> |
runRunnableIn(long delay,
java.util.concurrent.TimeUnit unit,
java.lang.Runnable runnable)
Schedules a
Runnable for delayed execution
The delay is defined using the delay and unit parameters. |
java.util.concurrent.Future<java.lang.Void> |
runTask(Task task)
Schedules a
Task for immediate execution
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task. |
java.util.concurrent.ScheduledFuture<java.lang.Void> |
runTaskIn(long delay,
java.util.concurrent.TimeUnit unit,
Task task)
|
int |
succeededCount() |
int |
toBeCompletedCount() |
MonitoringTaskSubmitter |
waitTillDone()
Causes the current thread to wait until there are no unfinished tasks,
unless the thread is interrupted.
|
boolean |
waitTillDone(long timeout,
java.util.concurrent.TimeUnit unit)
Causes the current thread to wait until there are no unfinished tasks,
unless the thread is interrupted.
|
protected final org.slf4j.Logger logger
protected final ReusableCountLatch latch
protected final java.util.concurrent.ScheduledExecutorService executor
public MonitoringTaskSubmitter(java.util.concurrent.ScheduledExecutorService executor)
executor - executor that will be used to execute taskspublic <T> java.util.concurrent.Future<T> run(java.util.concurrent.Callable<T> callable)
Callable for immediate execution.
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.T - type of return valuecallable - Callable to be executedFuture referring to the state of submitted Callablepublic java.util.concurrent.Future<java.lang.Void> run(Task task)
Task for immediate execution
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.public <T> java.util.concurrent.ScheduledFuture<T> runIn(long delay,
java.util.concurrent.TimeUnit unit,
java.util.concurrent.Callable<T> callable)
Callable for delayed execution.
The delay is defined using the delay and unit parameters.
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.T - type of return valuedelay - the value of the delayunit - the TimeUnit of the delay argumentcallable - Callable to be executedFuture referring to the state of submitted Callablepublic java.util.concurrent.ScheduledFuture<java.lang.Void> runIn(long delay,
java.util.concurrent.TimeUnit unit,
Task task)
Task for delayed execution
The delay is defined using the delay and unit parameters.
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.public <T> java.util.concurrent.Future<T> runCallable(java.util.concurrent.Callable<T> callable)
Callable for immediate execution.
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.T - type of return valuecallable - Callable to be executedFuture referring to the state of submitted Callablepublic java.util.concurrent.Future<java.lang.Void> runRunnable(java.lang.Runnable runnable)
Runnable for immediate execution
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.runnable - Runnable to be executedFuture referring to the state of submitted Runnablepublic java.util.concurrent.Future<java.lang.Void> runTask(Task task)
Task for immediate execution
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.public <T> java.util.concurrent.ScheduledFuture<T> runCallableIn(long delay,
java.util.concurrent.TimeUnit unit,
java.util.concurrent.Callable<T> callable)
Callable for delayed execution.
The delay is defined using the delay and unit parameters.
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.T - type of return valuedelay - the value of the delayunit - the TimeUnit of the delay argumentcallable - Callable to be executedFuture referring to the state of submitted Callablepublic java.util.concurrent.ScheduledFuture<java.lang.Void> runRunnableIn(long delay,
java.util.concurrent.TimeUnit unit,
java.lang.Runnable runnable)
Runnable for delayed execution
The delay is defined using the delay and unit parameters.
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.delay - the value of the delayunit - the TimeUnit of the delay argumentrunnable - Runnable to be executedFuture referring to the state of submitted Runnablepublic java.util.concurrent.ScheduledFuture<java.lang.Void> runTaskIn(long delay,
java.util.concurrent.TimeUnit unit,
Task task)
Task for delayed execution
The delay is defined using the delay and unit parameters.
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.public int toBeCompletedCount()
public int failedSubmissionCount()
public int succeededCount()
public int failedCount()
public void resetCounters()
public MonitoringTaskSubmitter waitTillDone()
If the current count of to be finished tasks is zero then this method returns immediately.
If the current count is greater than zero then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happen:
If the current thread:
RuntimeException wrapping an InterruptedException is thrown
and the current thread's interrupted status is cleared.MonitoringTaskSubmitter instancejava.lang.RuntimeException - wrapping a InterruptedException if the current thread
is interrupted while waitingpublic boolean waitTillDone(long timeout,
java.util.concurrent.TimeUnit unit)
If the current count of to be finished tasks is zero then this method returns immediately.
If the current count is greater than zero then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happen:
If the count reaches zero then the method returns with the value true.
If the current thread:
RuntimeException wrapping an InterruptedException is thrown
and the current thread's interrupted status is cleared.
If the specified waiting time elapses then the value false
is returned. If the time is less than or equal to zero, the method
will not wait at all.
timeout - the maximum time to waitunit - the time unit of the timeout argumenttrue if there are no tasks to be finished and false
if the waiting time elapsed before the count reached zerojava.lang.RuntimeException - wrapping a InterruptedException if the current thread
is interrupted while waitingprotected void onWorkException(java.lang.Exception e)
protected void onWorkThrowable(java.lang.Throwable t)