Class Retrier

  • All Implemented Interfaces:
    java.io.Serializable

    public class Retrier
    extends java.lang.Object
    implements java.io.Serializable
    Wrapper that does retry with exponential backoff.
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Retrier.FailureReporter
      Holds functions to call whenever the code being retried fails.
    • Constructor Summary

      Constructors 
      Constructor Description
      Retrier​(Sleeper sleeper, int transientFailureRetries)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void callWithRetry​(VoidCallable callable, Retrier.FailureReporter failureReporter, java.lang.Class<? extends java.lang.Throwable> retryableError, java.lang.Class<? extends java.lang.Throwable>... moreRetryableErrors)
      Retries a unit of work in the face of transient errors, without returning a value.
      void callWithRetry​(VoidCallable callable, java.lang.Class<? extends java.lang.Throwable> retryableError, java.lang.Class<? extends java.lang.Throwable>... moreRetryableErrors)
      Retries a unit of work in the face of transient errors, without returning a value.
      <V> V callWithRetry​(java.util.concurrent.Callable<V> callable, Retrier.FailureReporter failureReporter, java.lang.Class<? extends java.lang.Throwable> retryableError, java.lang.Class<? extends java.lang.Throwable>... moreRetryableErrors)
      Retries a unit of work in the face of transient errors and returns the result.
      <V> V callWithRetry​(java.util.concurrent.Callable<V> callable, java.lang.Class<? extends java.lang.Throwable> retryableError, java.lang.Class<? extends java.lang.Throwable>... moreRetryableErrors)
      Retries a unit of work in the face of transient errors and returns the result.
      <V> V callWithRetry​(java.util.concurrent.Callable<V> callable, java.util.function.Predicate<java.lang.Throwable> isRetryable)
      Retries a unit of work in the face of transient errors.
      • Methods inherited from class java.lang.Object

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

      • Retrier

        @Inject
        public Retrier​(Sleeper sleeper,
                       @Named("transientFailureRetries")
                       int transientFailureRetries)
    • Method Detail

      • callWithRetry

        public <V> V callWithRetry​(java.util.concurrent.Callable<V> callable,
                                   java.util.function.Predicate<java.lang.Throwable> isRetryable)
        Retries a unit of work in the face of transient errors.

        Retrying is done a fixed number of times, with exponential backoff, if the exception that is thrown is deemed retryable by the predicate. If the error is not considered retryable, or if the thread is interrupted, or if the allowable number of attempts has been exhausted, the original exception is propagated through to the caller. Checked exceptions are wrapped in a RuntimeException, while unchecked exceptions are propagated as-is.

        Returns:
        the value returned by the Callable.
      • callWithRetry

        @SafeVarargs
        public final <V> V callWithRetry​(java.util.concurrent.Callable<V> callable,
                                         java.lang.Class<? extends java.lang.Throwable> retryableError,
                                         java.lang.Class<? extends java.lang.Throwable>... moreRetryableErrors)
        Retries a unit of work in the face of transient errors and returns the result.

        Retrying is done a fixed number of times, with exponential backoff, if the exception that is thrown is on an allow list of retryable errors. If the error is not on the allow list, or if the thread is interrupted, or if the allowable number of attempts has been exhausted, the original exception is propagated through to the caller. Checked exceptions are wrapped in a RuntimeException, while unchecked exceptions are propagated as-is.

        Uses a default FailureReporter that logs before each retry.

        Returns:
        the value returned by the Callable.
      • callWithRetry

        @SafeVarargs
        public final void callWithRetry​(VoidCallable callable,
                                        java.lang.Class<? extends java.lang.Throwable> retryableError,
                                        java.lang.Class<? extends java.lang.Throwable>... moreRetryableErrors)
        Retries a unit of work in the face of transient errors, without returning a value.
        See Also:
        callWithRetry(Callable, Class, Class[])
      • callWithRetry

        @SafeVarargs
        public final <V> V callWithRetry​(java.util.concurrent.Callable<V> callable,
                                         Retrier.FailureReporter failureReporter,
                                         java.lang.Class<? extends java.lang.Throwable> retryableError,
                                         java.lang.Class<? extends java.lang.Throwable>... moreRetryableErrors)
        Retries a unit of work in the face of transient errors and returns the result.

        Retrying is done a fixed number of times, with exponential backoff, if the exception that is thrown is on an allow list of retryable errors. If the error is not on the allow list, or if the thread is interrupted, or if the allowable number of attempts has been exhausted, the original exception is propagated through to the caller. Checked exceptions are wrapped in a RuntimeException, while unchecked exceptions are propagated as-is.

        Returns:
        the value returned by the Callable.