Interface Retry<T>

  • Type Parameters:
    T - Application context type
    All Superinterfaces:
    Function<Flux<Throwable>,​org.reactivestreams.Publisher<Long>>
    All Known Implementing Classes:
    DefaultRetry

    public interface Retry<T>
    extends Function<Flux<Throwable>,​org.reactivestreams.Publisher<Long>>
    Retry function that may be used with Flux.retryWhen(Function) and Mono.retryWhen(Function).

    Each change in configuration returns a new instance (copy configuration), which makes Retry suitable for creating configuration templates that can be fine tuned for specific cases without impacting the original general use-case configuration.

    Example usage:

    
       retry = Retry.anyOf(IOException.class)
                     .randomBackoff(Duration.ofMillis(100), Duration.ofSeconds(60))
                     .withApplicationContext(appContext)
                     .doOnRetry(context -> context.applicationContext().rollback());
       flux.retryWhen(retry);
     
    • Method Detail

      • any

        static <T> Retry<T> any()
        Returns a retry function that retries any exception, once. More constraints may be added using retryMax(long) or timeout(Duration).
        Returns:
        retry function that retries on any exception
      • anyOf

        @SafeVarargs
        static <T> Retry<T> anyOf​(Class<? extends Throwable>... retriableExceptions)
        Returns a retry function that retries errors resulting from any of the specified exceptions, once. More constraints may be added using retryMax(long) or timeout(Duration).
        Parameters:
        retriableExceptions - Exceptions that may be retried
        Returns:
        retry function that retries indefinitely, only for specified exceptions
      • allBut

        @SafeVarargs
        static <T> Retry<T> allBut​(Class<? extends Throwable>... nonRetriableExceptions)
        Returns a retry function that retries errors resulting from all exceptions except the specified non-retriable exceptions, once. More constraints may be added using retryMax(long) or timeout(Duration).
        Parameters:
        nonRetriableExceptions - exceptions that may not be retried
        Returns:
        retry function that retries all exceptions except the specified non-retriable exceptions.
      • onlyIf

        static <T> Retry<T> onlyIf​(Predicate<? super RetryContext<T>> predicate)
        Retry function that retries only if the predicate returns true, with no limit to the number of attempts.
        Parameters:
        predicate - Predicate that determines if next retry is performed
        Returns:
        Retry function with predicate
      • withApplicationContext

        Retry<T> withApplicationContext​(T applicationContext)
        Returns a retry function with an application context that may be used to perform any rollbacks before a retry. This application context is provided to any retry predicate onlyIf(Predicate), custom backoff function backoff(Backoff) and retry callback doOnRetry(Consumer). All other properties of this retry function are retained in the returned instance.
        Parameters:
        applicationContext - Application context
        Returns:
        retry function with associated application context
      • doOnRetry

        Retry<T> doOnRetry​(Consumer<? super RetryContext<T>> onRetry)
        Returns a retry function that invokes the provided onRetry callback before every retry. The RetryContext provided to the callback contains the iteration and the any application context set using withApplicationContext(Object). All other properties of this retry function are retained in the returned instance.
        Parameters:
        onRetry - callback to invoke before retries
        Returns:
        retry function with callback
      • retryOnce

        default Retry<T> retryOnce()
        Retry function that retries once.
        Returns:
        Retry function for one retry
      • retryMax

        Retry<T> retryMax​(long maxRetries)
        Retry function that retries n times.
        Parameters:
        maxRetries - number of retries
        Returns:
        Retry function for n retries
      • timeout

        Retry<T> timeout​(Duration timeout)
        Returns a retry function with timeout. The timeout starts from the instant that this function is applied, and the function keeps retrying until the timeout expires (or until the configured maximum number of attempts, if it has been set). All other properties of this retry function are retained in the returned instance.
        Parameters:
        timeout - timeout after which no new retries are initiated
        Returns:
        retry function with global timeout
      • backoff

        Retry<T> backoff​(Backoff backoff)
        Returns a retry function with backoff delay. All other properties of this retry function are retained in the returned instance.
        Parameters:
        backoff - the backoff function to determine backoff delay
        Returns:
        retry function with backoff
      • jitter

        Retry<T> jitter​(Jitter jitter)
        Returns a retry function that applies jitter to the backoff delay. All other properties of this retry function are retained in the returned instance.
        Parameters:
        jitter - Jitter function to randomize backoff delay
        Returns:
        retry function with jitter for backoff
      • withBackoffScheduler

        Retry<T> withBackoffScheduler​(Scheduler scheduler)
        Returns a retry function that uses the scheduler provided for backoff delays. All other properties of this retry function are retained in the returned instance.
        Parameters:
        scheduler - the scheduler for backoff delays
        Returns:
        retry function with backoff scheduler
      • noBackoff

        default Retry<T> noBackoff()
        Returns a retry function with no backoff delay. This is the default. All other properties of this retry function are retained in the returned instance.
        Returns:
        retry function with no backoff delay
      • fixedBackoff

        default Retry<T> fixedBackoff​(Duration backoffInterval)
        Returns a retry function with fixed backoff delay. All other properties of this retry function are retained in the returned instance.
        Parameters:
        backoffInterval - fixed backoff delay applied before every retry
        Returns:
        retry function with fixed backoff delay
      • exponentialBackoff

        default Retry<T> exponentialBackoff​(Duration firstBackoff,
                                            Duration maxBackoff)
        Returns a retry function with exponential backoff delay. All other properties of this retry function are retained in the returned instance.

        Retries are performed after a backoff interval of firstBackoff * (2 ** n) where n is the next iteration number. If maxBackoff is not null, the maximum backoff applied will be limited to maxBackoff.

        Parameters:
        firstBackoff - the delay for the first backoff, which is also used as the coefficient for subsequent backoffs
        maxBackoff - the maximum backoff delay before a retry
        Returns:
        retry function with exponential backoff delay
      • exponentialBackoffWithJitter

        default Retry<T> exponentialBackoffWithJitter​(Duration firstBackoff,
                                                      Duration maxBackoff)
        Returns a retry function with full jitter backoff strategy. All other properties of this retry function are retained in the returned instance.

        Retries are performed after a random backoff interval between firstBackoff and firstBackoff * (2 ** n) where n is the next iteration number. If maxBackoff is not null, the maximum backoff applied will be limited to maxBackoff.

        Parameters:
        firstBackoff - the delay for the first backoff, which is also used as the coefficient for subsequent backoffs
        maxBackoff - the maximum backoff delay before a retry
        Returns:
        retry function with full jitter backoff strategy
      • randomBackoff

        default Retry<T> randomBackoff​(Duration firstBackoff,
                                       Duration maxBackoff)
        Returns a retry function with random de-correlated jitter backoff strategy. All other properties of this retry function are retained in the returned instance.

        Retries are performed after a backoff interval of random_between(firstBackoff, prevBackoff * 3), with a minimum value of firstBackoff. If maxBackoff is not null, the maximum backoff applied will be limited to maxBackoff.

        Parameters:
        firstBackoff - the delay for the first backoff, also used as minimum backoff
        maxBackoff - the maximum backoff delay before a retry
        Returns:
        retry function with de-correlated jitter backoff strategy
      • apply

        default <S> Flux<S> apply​(org.reactivestreams.Publisher<S> source)
        Transforms the source into a retrying Flux based on the properties configured for this function.

        Example usage:

        
            retry = Retry.anyOf(IOException.class)
                         .withApplicationContext(appContext)
                         .doOnRetry(context -> context.applicationContext().rollback())
                         .exponentialBackoff(Duration.ofMillis(100), Duration.ofSeconds(60));
            flux.as(retry);
         
        Parameters:
        source - the source publisher
        Returns:
        Flux with the retry properties of this retry function