Class Poller<T>

  • Type Parameters:
    T - type of content returned upon resolution
    Direct Known Subclasses:
    Poller.SimplePoller

    public abstract class Poller<T>
    extends Object
    Class that facilitates polling for an arbitrary condition. Polling is the act of repeatedly querying the state at defined intervals. Polling stops when this poller's evaluation function answers with a reason to stop, or the iterator of intervals to wait between polls is exhausted. Reasons to stop include resolution, meaning the poller is satisfied with the result, or abortion meaning polling must stop early without a resolution.
    • Constructor Detail

      • Poller

        public Poller()
        Creates a new poller. The poller waits between polls using the default Sleeper. Subclasses can use an alternate sleeper, is helpful if you want to test your poller without actually waiting.
      • Poller

        protected Poller​(Sleeper sleeper)
    • Method Detail

      • poll

        public Poller.PollOutcome<T> poll​(long intervalMs,
                                          int maxNumPolls)
                                   throws InterruptedException
        Polls at regular intervals.
        Parameters:
        intervalMs - the interval in milliseconds
        maxNumPolls - the maximum number of polls to be executed
        Returns:
        the poll outcome
        Throws:
        InterruptedException - if waiting is interrupted
      • check

        protected abstract Poller.PollAnswer<T> check​(int pollAttemptsSoFar)
        Checks whether the state being questioned has been resolved. Subclasses must override this method to return an answer that may or may not contain a content object. In a conventional implementation, if the state has been resolved, this method would return an answer with content object representing a resolution along with Poller.PollAction.RESOLVE; if the state has not yet been resolved, this method would return null as the answer content along with Poller.PollAction.CONTINUE if we should continue polling or Poller.PollAction.ABORT if polling should stop immediately anyway.

        Implementations of this method should return an answer constructed with the continuePolling(), abortPolling(), or resolve(Object) methods.

        Unconventional implementations may elect to return a non-null content object with Poller.PollAction.ABORT to provide the poll() caller a degenerate answer, perhaps indicating why state will never be resolved.

        Parameters:
        pollAttemptsSoFar - the number of poll attempts prior to this poll attempt
        Returns:
        a poll answer
      • checking

        public static Poller<Void> checking​(Supplier<Boolean> condition)
        Creates a simple poller that evaluates a condition on each poll.
        Parameters:
        condition - the condition to evaluate; poll will be resolved if it returns true, and if it returns false, the poller will keep polling
        Returns:
        the poller