Class Retryer


  • public final class Retryer
    extends Object
    A retryer, which executes a call, and retries it until it succeeds, or a stop strategy decides to stop retrying. A wait strategy is used to sleep between attempts. The strategy to decide if the call succeeds or not is also configurable.

    A retryer can also wrap the callable into a RetryerCallable, which can be submitted to an executor.

    Retryer instances are better constructed with a RetryerBuilder. A retryer is thread-safe, provided the arguments passed to its constructor are thread-safe.

    • Method Detail

      • call

        public <T> T call​(Callable<T> callable)
                   throws RetryException,
                          InterruptedException
        Executes the given callable, retrying if necessary. If the retry predicate accepts the attempt, the stop strategy is used to decide if a new attempt must be made. Then the wait strategy is used to decide how much time to sleep and a new attempt is made.
        Type Parameters:
        T - the return type of the Callable
        Parameters:
        callable - the callable task to be executed
        Returns:
        the computed result of the given callable
        Throws:
        RetryException - if all the attempts failed before the stop strategy decided to abort
        InterruptedException - If this thread is interrupted. This can happen because Thread.sleep(long) is invoked between attempts
      • run

        public void run​(Runnable runnable)
                 throws RetryException,
                        InterruptedException
        Executes the given runnable, retrying if necessary. If the retry predicate accepts the attempt, the stop strategy is used to decide if a new attempt must be made. Then the wait strategy is used to decide how much time to sleep and a new attempt is made.
        Parameters:
        runnable - the runnable task to be executed
        Throws:
        RetryException - if all the attempts failed before the stop strategy decided to abort
        InterruptedException - If this thread is interrupted. This can happen because Thread.sleep(long) is invoked between attempts