public class PollingWatchdog
extends java.lang.Object
ConstantTimeWatchdog
and MixedTimeWatchdog
as possibly more efficient
options. This class however can allow the condition to be independent from the submission to be
watched. For example if you want to time something out after a lack of progress, but don't care
about the absolute time it has been running you can track that progress and provide a
Supplier
to watch(Supplier, ListenableFuture)
which will return true
only once there has been no progress in X milliseconds. The supplier will be invoked
regularly to check the state associated to the matching future.Constructor and Description |
---|
PollingWatchdog(long pollFrequencyMillis,
boolean sendInterruptOnFutureCancel)
Constructs a new
PollingWatchdog . |
PollingWatchdog(SubmitterScheduler scheduler,
long pollFrequencyMillis,
boolean sendInterruptOnFutureCancel)
Constructs a new
PollingWatchdog with a scheduler of your choosing. |
Modifier and Type | Method and Description |
---|---|
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(java.util.function.Supplier<java.lang.Boolean> cancelTest,
ListenableFuture<?> future)
Watch a given
ListenableFuture to ensure that it completes within the constructed
time limit. |
public PollingWatchdog(long pollFrequencyMillis, boolean sendInterruptOnFutureCancel)
PollingWatchdog
. 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.pollFrequencyMillis
- Time in milliseconds that futures will be checked if they should be timed outsendInterruptOnFutureCancel
- If true
, and a thread is provided with the future,
an interrupt will be sent on timeoutpublic PollingWatchdog(SubmitterScheduler scheduler, long pollFrequencyMillis, boolean sendInterruptOnFutureCancel)
PollingWatchdog
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 futurespollFrequencyMillis
- Time in milliseconds that futures will be checked if they should be timed outsendInterruptOnFutureCancel
- If true
, and a thread is provided with the future,
an interrupt will be sent on timeoutpublic void watch(java.util.function.Supplier<java.lang.Boolean> cancelTest, 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
PollingWatchdog
was constructed.
The provided Supplier
will be invoked regularly as the state is polled. Once it
returns true
it will never be invoked again, and instead
Future.cancel(boolean)
will be invoked on the provided future.
cancelTest
- Supplier to return true
when the future should be canceledfuture
- Future to inspect to ensure completionpublic boolean isActive()
true
if this watchdog is currently in usepublic int getWatchingCount()