Class PollingWatchdog


  • public class PollingWatchdog
    extends java.lang.Object
    This class is designed to help ensure a future completes with an arbitrary condition to determine when a future should be canceled. If the condition is time based then see 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.
    Since:
    5.40
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • PollingWatchdog

        public PollingWatchdog​(long pollFrequencyMillis,
                               boolean sendInterruptOnFutureCancel)
        Constructs a new 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.
        Parameters:
        pollFrequencyMillis - Time in milliseconds that futures will be checked if they should be timed out
        sendInterruptOnFutureCancel - If true, and a thread is provided with the future, an interrupt will be sent on timeout
      • PollingWatchdog

        public PollingWatchdog​(SubmitterScheduler scheduler,
                               long pollFrequencyMillis,
                               boolean sendInterruptOnFutureCancel)
        Constructs a new 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.
        Parameters:
        scheduler - Scheduler to schedule task to look for expired futures
        pollFrequencyMillis - Time in milliseconds that futures will be checked if they should be timed out
        sendInterruptOnFutureCancel - If true, and a thread is provided with the future, an interrupt will be sent on timeout
    • Method Detail

      • watch

        public 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. 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.

        Parameters:
        cancelTest - Supplier to return true when the future should be canceled
        future - Future to inspect to ensure completion
      • isActive

        public boolean isActive()
        Checks to see if this watchdog is currently active. Meaning there are futures on it which either have not been completed yet, or have not been inspected for completion. If this returns false, it means that there are no futures waiting to complete, and no scheduled tasks currently scheduled to inspect them.
        Returns:
        true if this watchdog is currently in use
      • getWatchingCount

        public int getWatchingCount()
        Check how many futures are currently being monitored for completion.
        Returns:
        The number of futures being monitored