public class Watchdog
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.Constructor and Description |
---|
Watchdog(long timeoutInMillis,
boolean sendInterruptOnFutureCancel)
Constructs a new
Watchdog . |
Watchdog(SubmitterScheduler scheduler,
long timeoutInMillis,
boolean sendInterruptOnFutureCancel)
Constructs a new
Watchdog 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.
|
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 Watchdog(long timeoutInMillis, boolean sendInterruptOnFutureCancel)
Watchdog
. 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 want to avoid the thread creation (which is shared among all instances
that were constructed with this constructor or WatchdogCache.WatchdogCache(boolean)
).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 Watchdog(SubmitterScheduler scheduler, long timeoutInMillis, boolean sendInterruptOnFutureCancel)
Watchdog
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 boolean isActive()
true
if this watchdog is currently in usepublic 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
Watchdog
was constructed.future
- Future to inspect to ensure completion