Class Retryer

java.lang.Object
com.google.mu.util.concurrent.Retryer

public final class Retryer extends Object
Immutable object that retries actions upon exceptions.

Backoff intervals are configured through chaining upon() calls. It's critical to use the new Retryer instances returned by upon(). Just remember Retryer is immutable.

If the retried operation still fails after retry, the previous exceptions can be accessed through Throwable.getSuppressed().

Since:
2.0
  • Constructor Details

    • Retryer

      public Retryer()
      Constructs an empty Retryer.
  • Method Details

    • upon

      public final <E extends Throwable> Retryer upon(Class<E> exceptionType, List<? extends Retryer.Delay<? super E>> delays)
      Returns a new Retryer that uses delays when an exception is instance of exceptionType.

      InterruptedException is always considered a request to stop retrying. Calling upon(InterruptedException.class, ...) is illegal.

    • upon

      public final <E extends Throwable> Retryer upon(Class<E> exceptionType, Stream<? extends Retryer.Delay<? super E>> delays)
      Returns a new Retryer that uses delays when an exception is instance of exceptionType.

      InterruptedException is always considered a request to stop retrying. Calling upon(InterruptedException.class, ...) is illegal.

    • upon

      public <E extends Throwable> Retryer upon(Class<E> exceptionType, Predicate<? super E> condition, List<? extends Retryer.Delay<? super E>> delays)
      Returns a new Retryer that uses delays when an exception is instance of exceptionType and satisfies condition.

      InterruptedException is always considered a request to stop retrying. Calling upon(InterruptedException.class, ...) is illegal.

    • upon

      public <E extends Throwable> Retryer upon(Class<E> exceptionType, Predicate<? super E> condition, Stream<? extends Retryer.Delay<? super E>> delays)
      Returns a new Retryer that uses delays when an exception is instance of exceptionType and satisfies condition.

      InterruptedException is always considered a request to stop retrying. Calling upon(InterruptedException.class, ...) is illegal.

    • retryBlockingly

      public <T, E extends Throwable> T retryBlockingly(CheckedSupplier<T,E> supplier) throws E
      Invokes and possibly retries supplier upon exceptions, according to the retry strategies specified with upon().

      This method blocks while waiting to retry. If interrupted, retry is canceled.

      If supplier fails despite retrying, the exception from the most recent invocation is propagated.

      Throws:
      E
    • retry

      public <T> CompletionStage<T> retry(CheckedSupplier<T,?> supplier, ScheduledExecutorService executor)
      Invokes and possibly retries supplier upon exceptions, according to the retry strategies specified with upon().

      The first invocation is done in the current thread. Unchecked exceptions thrown by supplier directly are propagated unless explicitly configured to retry. This is to avoid hiding programming errors. Checked exceptions are reported through the returned CompletionStage so callers only need to deal with them in one place.

      Retries are scheduled and performed by executor.

      Canceling the returned future object will cancel currently pending retry attempts. Same if supplier throws InterruptedException.

      NOTE that if executor.shutdownNow() is called, the returned CompletionStage will never be done.

    • retryAsync

      public <T> CompletionStage<T> retryAsync(CheckedSupplier<? extends CompletionStage<T>,?> asyncSupplier, ScheduledExecutorService executor)
      Invokes and possibly retries asyncSupplier upon exceptions, according to the retry strategies specified with upon().

      The first invocation is done in the current thread. Unchecked exceptions thrown by asyncSupplier directly are propagated unless explicitly configured to retry. This is to avoid hiding programming errors. Checked exceptions are reported through the returned CompletionStage so callers only need to deal with them in one place.

      Retries are scheduled and performed by executor.

      Canceling the returned future object will cancel currently pending retry attempts. Same if supplier throws InterruptedException.

      NOTE that if executor.shutdownNow() is called, the returned CompletionStage will never be done.

    • ifReturns

      public <T> Retryer.ForReturnValue<T> ifReturns(Predicate<T> condition, List<? extends Retryer.Delay<? super T>> delays)
      Returns a new object that retries if the return value satisfies condition. delays specify the backoffs between retries.
    • ifReturns

      public <T> Retryer.ForReturnValue<T> ifReturns(Predicate<T> condition, Stream<? extends Retryer.Delay<? super T>> delays)
      Returns a new object that retries if the return value satisfies condition. delays specify the backoffs between retries.
    • uponReturn

      public <T> Retryer.ForReturnValue<T> uponReturn(T returnValue, Stream<? extends Retryer.Delay<? super T>> delays)
      Returns a new object that retries if the function returns returnValue.
      Parameters:
      returnValue - The nullable return value that triggers retry
      delays - specify the backoffs between retries
    • uponReturn

      public <T> Retryer.ForReturnValue<T> uponReturn(T returnValue, List<? extends Retryer.Delay<? super T>> delays)
      Returns a new object that retries if the function returns returnValue.
      Parameters:
      returnValue - The nullable return value that triggers retry
      delays - specify the backoffs between retries