T - Application context typepublic interface Retry<T> extends java.util.function.Function<Flux<Throwable>,Publisher<Long>>
Flux.retryWhen(reactor.util.retry.Retry) and Mono.retryWhen(reactor.util.retry.Retry), after conversion via Retry.withThrowable(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);
| Modifier and Type | Method and Description |
|---|---|
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.
|
static <T> Retry<T> |
any()
Returns a retry function that retries any exception, once.
|
static <T> Retry<T> |
anyOf(Class<? extends Throwable>... retriableExceptions)
Returns a retry function that retries errors resulting from any of the
specified exceptions, once.
|
default <S> Flux<S> |
apply(Publisher<S> source)
Transforms the source into a retrying
Flux based on the properties
configured for this function. |
Retry<T> |
backoff(Backoff backoff)
Returns a retry function with backoff delay.
|
Retry<T> |
doOnRetry(java.util.function.Consumer<? super RetryContext<T>> onRetry)
Returns a retry function that invokes the provided onRetry
callback before every retry.
|
default Retry<T> |
exponentialBackoff(java.time.Duration firstBackoff,
java.time.Duration maxBackoff)
Returns a retry function with exponential backoff delay.
|
default Retry<T> |
exponentialBackoffWithJitter(java.time.Duration firstBackoff,
java.time.Duration maxBackoff)
Returns a retry function with full jitter backoff strategy.
|
default Retry<T> |
fixedBackoff(java.time.Duration backoffInterval)
Returns a retry function with fixed backoff delay.
|
Retry<T> |
jitter(Jitter jitter)
Returns a retry function that applies jitter to the backoff delay.
|
default Retry<T> |
noBackoff()
Returns a retry function with no backoff delay.
|
static <T> Retry<T> |
onlyIf(java.util.function.Predicate<? super RetryContext<T>> predicate)
Retry function that retries only if the predicate returns true, with no limit to
the number of attempts.
|
default Retry<T> |
randomBackoff(java.time.Duration firstBackoff,
java.time.Duration maxBackoff)
Returns a retry function with random de-correlated jitter backoff strategy.
|
Retry<T> |
retryMax(long maxRetries)
Retry function that retries n times.
|
default Retry<T> |
retryOnce()
Retry function that retries once.
|
Retry<T> |
timeout(java.time.Duration timeout)
Returns a retry function with timeout.
|
Retry<T> |
withApplicationContext(T applicationContext)
Returns a retry function with an application context that may be
used to perform any rollbacks before a retry.
|
Retry<T> |
withBackoffScheduler(Scheduler scheduler)
Returns a retry function that uses the scheduler provided for
backoff delays.
|
andThen, apply, compose, identitystatic <T> Retry<T> any()
retryMax(long) or timeout(Duration).@SafeVarargs static <T> Retry<T> anyOf(Class<? extends Throwable>... retriableExceptions)
retryMax(long)
or timeout(Duration).retriableExceptions - Exceptions that may be retried@SafeVarargs static <T> Retry<T> allBut(Class<? extends Throwable>... nonRetriableExceptions)
retryMax(long) or timeout(Duration).nonRetriableExceptions - exceptions that may not be retriedstatic <T> Retry<T> onlyIf(java.util.function.Predicate<? super RetryContext<T>> predicate)
predicate - Predicate that determines if next retry is performedRetry<T> withApplicationContext(T applicationContext)
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.applicationContext - Application contextRetry<T> doOnRetry(java.util.function.Consumer<? super RetryContext<T>> onRetry)
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.onRetry - callback to invoke before retriesdefault Retry<T> retryOnce()
Retry<T> retryMax(long maxRetries)
maxRetries - number of retriesRetry<T> timeout(java.time.Duration timeout)
timeout - timeout after which no new retries are initiatedRetry<T> backoff(Backoff backoff)
backoff - the backoff function to determine backoff delayRetry<T> jitter(Jitter jitter)
jitter - Jitter function to randomize backoff delayRetry<T> withBackoffScheduler(Scheduler scheduler)
scheduler - the scheduler for backoff delaysdefault Retry<T> noBackoff()
default Retry<T> fixedBackoff(java.time.Duration backoffInterval)
backoffInterval - fixed backoff delay applied before every retrydefault Retry<T> exponentialBackoff(java.time.Duration firstBackoff, java.time.Duration maxBackoff)
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.
firstBackoff - the delay for the first backoff, which is also used as the coefficient for subsequent backoffsmaxBackoff - the maximum backoff delay before a retrydefault Retry<T> exponentialBackoffWithJitter(java.time.Duration firstBackoff, java.time.Duration maxBackoff)
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.
firstBackoff - the delay for the first backoff, which is also used as the coefficient for subsequent backoffsmaxBackoff - the maximum backoff delay before a retrydefault Retry<T> randomBackoff(java.time.Duration firstBackoff, java.time.Duration maxBackoff)
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.
firstBackoff - the delay for the first backoff, also used as minimum backoffmaxBackoff - the maximum backoff delay before a retrydefault <S> Flux<S> apply(Publisher<S> source)
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);
source - the source publisherFlux with the retry properties of this retry function