public class ConstantTimeWatchdog
extends java.lang.Object
ListenableFuture
is completed within a
timeout. Once the timeout is reached, if the future has not already completed this will
attempt to invoke Future.cancel(boolean)
. The future should then throw a
CancellationException
on a Future.get()
call.
Using CancelDebuggingListenableFuture
to wrap the futures
before providing to this class can provide an easier understanding of the state of a Future when
it was timed out by this class.
Constructor and Description |
---|
ConstantTimeWatchdog(long timeoutInMillis,
boolean sendInterruptOnFutureCancel)
Constructs a new
ConstantTimeWatchdog . |
ConstantTimeWatchdog(SubmitterScheduler scheduler,
long timeoutInMillis,
boolean sendInterruptOnFutureCancel)
Constructs a new
ConstantTimeWatchdog with a scheduler of your choosing. |
Modifier and Type | Method and Description |
---|---|
long |
getTimeoutInMillis()
Request the timeout in milliseconds until futures that have not completed are canceled.
|
int |
getWatchingCount()
Check how many futures are currently being monitored for completion.
|
boolean |
isActive()
Checks to see if this watchdog is currently active.
|
void |
watch(ListenableFuture<?> future)
Watch a given
ListenableFuture to ensure that it completes within the constructed
time limit. |
public ConstantTimeWatchdog(long timeoutInMillis, boolean sendInterruptOnFutureCancel)
ConstantTimeWatchdog
. This constructor will use a default static scheduler
(which is lazily constructed). This should be fine in most cases, but you can provide your
own scheduler if you have specific needs where the CentralThreadlyPool
default is not
a good option.timeoutInMillis
- Time in milliseconds that futures will be set to error if they are not donesendInterruptOnFutureCancel
- If true
, and a thread is provided with the future,
an interrupt will be sent on timeoutpublic ConstantTimeWatchdog(SubmitterScheduler scheduler, long timeoutInMillis, boolean sendInterruptOnFutureCancel)
ConstantTimeWatchdog
with a scheduler of your choosing. It is critical that
this scheduler has a free thread available to inspect futures which may not have completed in
the given timeout. You may want to use a org.threadly.concurrent.limiter to ensure that
there are threads available.scheduler
- Scheduler to schedule task to look for expired futurestimeoutInMillis
- Time in milliseconds that futures will be set to error if they are not donesendInterruptOnFutureCancel
- If true
, and a thread is provided with the future,
an interrupt will be sent on timeoutpublic long getTimeoutInMillis()
Future.cancel(boolean)
invokedpublic void watch(ListenableFuture<?> future)
ListenableFuture
to ensure that it completes within the constructed
time limit. If the future is not marked as done by the time limit then it will be completed
by invoking Future.cancel(boolean)
. Weather a true
or
false
will be provided to interrupt the running thread is dependent on how this
ConstantTimeWatchdog
was constructed.future
- Future to inspect to ensure completionpublic boolean isActive()
true
if this watchdog is currently in usepublic int getWatchingCount()