public final class RetrySpec extends Retry
Retry strategy with configurable features. Use Retry.max(long),
Retry.maxInARow(long) or Retry.indefinitely() to obtain a preconfigured instance to start with.
Only errors that match the filter(Predicate) are retried (by default all), up to maxAttempts(long) times.
When the maximum attempt of retries is reached, a runtime exception is propagated downstream which
can be pinpointed with Exceptions.isRetryExhausted(Throwable). The cause of
the last attempt's failure is attached as said retryExhausted
exception's cause. This can be customized with onRetryExhaustedThrow(BiFunction).
Additionally, to help dealing with bursts of transient errors in a long-lived Flux as if each burst
had its own attempt counter, one can choose to set transientErrors(boolean) to true.
The comparison to maxAttempts(long) will then be done with the number of subsequent attempts
that failed without an onNext in between.
The RetrySpec is copy-on-write and as such can be stored as a "template" and further configured
by different components without a risk of modifying the original configuration.
Retry.RetrySignal| Modifier and Type | Field and Description |
|---|---|
Predicate<Throwable> |
errorFilter
The configured
Predicate to filter which exceptions to retry. |
boolean |
isTransientErrors
The configured transient error handling flag.
|
long |
maxAttempts
The configured maximum for retry attempts.
|
retryContext| Modifier and Type | Method and Description |
|---|---|
RetrySpec |
doAfterRetry(Consumer<Retry.RetrySignal> doAfterRetry)
Add synchronous behavior to be executed after the retry trigger is emitted in
the companion publisher.
|
RetrySpec |
doAfterRetryAsync(Function<Retry.RetrySignal,Mono<Void>> doAsyncAfterRetry)
Add asynchronous behavior to be executed after the current retry trigger in the companion publisher,
thus delaying the resulting retry trigger with the additional
Mono. |
RetrySpec |
doBeforeRetry(Consumer<Retry.RetrySignal> doBeforeRetry)
Add synchronous behavior to be executed before the retry trigger is emitted in
the companion publisher.
|
RetrySpec |
doBeforeRetryAsync(Function<Retry.RetrySignal,Mono<Void>> doAsyncBeforeRetry)
Add asynchronous behavior to be executed before the current retry trigger in the companion publisher,
thus delaying the resulting retry trigger with the additional
Mono. |
RetrySpec |
filter(Predicate<? super Throwable> errorFilter)
Set the
Predicate that will filter which errors can be retried. |
Flux<Long> |
generateCompanion(Flux<Retry.RetrySignal> flux)
Generates the companion publisher responsible for reacting to incoming
Retry.RetrySignal emissions, effectively
deciding when to retry. |
RetrySpec |
maxAttempts(long maxAttempts)
Set the maximum number of retry attempts allowed.
|
RetrySpec |
modifyErrorFilter(Function<Predicate<Throwable>,Predicate<? super Throwable>> predicateAdjuster)
|
RetrySpec |
onRetryExhaustedThrow(BiFunction<RetrySpec,Retry.RetrySignal,Throwable> retryExhaustedGenerator)
Set the generator for the
Exception to be propagated when the maximum amount of retries
is exhausted. |
RetrySpec |
transientErrors(boolean isTransientErrors)
Set the transient error mode, indicating that the strategy being built should use
Retry.RetrySignal.totalRetriesInARow() rather than
Retry.RetrySignal.totalRetries(). |
RetrySpec |
withRetryContext(ContextView retryContext)
Set the user provided
context that can be used to manipulate state on retries. |
backoff, fixedDelay, from, indefinitely, max, maxInARow, retryContext, withThrowablepublic final long maxAttempts
maxAttempts(long)public final Predicate<Throwable> errorFilter
Predicate to filter which exceptions to retry.filter(Predicate),
modifyErrorFilter(Function)public final boolean isTransientErrors
transientErrors(boolean)public RetrySpec withRetryContext(ContextView retryContext)
context that can be used to manipulate state on retries.public RetrySpec maxAttempts(long maxAttempts)
public RetrySpec filter(Predicate<? super Throwable> errorFilter)
Predicate that will filter which errors can be retried. Exceptions
that don't pass the predicate will be propagated downstream and terminate the retry
sequence. Defaults to allowing retries for all exceptions.public RetrySpec modifyErrorFilter(Function<Predicate<Throwable>,Predicate<? super Throwable>> predicateAdjuster)
set Predicate with
a new condition to allow retries of some exception or not. This can typically be used with
Predicate.and(Predicate) to combine existing predicate(s) with a new one.
For example:
//given
RetrySpec retryTwiceIllegalArgument = Retry.max(2)
.filter(e -> e instanceof IllegalArgumentException);
RetrySpec retryTwiceIllegalArgWithCause = retryTwiceIllegalArgument.modifyErrorFilter(old ->
old.and(e -> e.getCause() != null));
public RetrySpec doBeforeRetry(Consumer<Retry.RetrySignal> doBeforeRetry)
doBeforeRetry - the synchronous hook to execute before retry trigger is emittedRetrySpec which can either be further configured or used as RetryandDelayRetryWith for an asynchronous versionpublic RetrySpec doAfterRetry(Consumer<Retry.RetrySignal> doAfterRetry)
doAfterRetry - the synchronous hook to execute after retry trigger is startedRetrySpec which can either be further configured or used as RetryandRetryThen for an asynchronous versionpublic RetrySpec doBeforeRetryAsync(Function<Retry.RetrySignal,Mono<Void>> doAsyncBeforeRetry)
Mono.public RetrySpec doAfterRetryAsync(Function<Retry.RetrySignal,Mono<Void>> doAsyncAfterRetry)
Mono.public RetrySpec onRetryExhaustedThrow(BiFunction<RetrySpec,Retry.RetrySignal,Throwable> retryExhaustedGenerator)
Exception to be propagated when the maximum amount of retries
is exhausted. By default, throws an Exceptions.retryExhausted(String, Throwable) with the
message reflecting the total attempt index, transient attempt index and maximum retry count.
The cause of the last Retry.RetrySignal is also added as the exception's cause.retryExhaustedGenerator - the Function that generates the Throwable for the last
Retry.RetrySignalRetrySpec which can either be further configured or used as Retrypublic RetrySpec transientErrors(boolean isTransientErrors)
Retry.RetrySignal.totalRetriesInARow() rather than
Retry.RetrySignal.totalRetries().
Transient errors are errors that could occur in bursts but are then recovered from by
a retry (with one or more onNext signals) before another error occurs.
In the case of a simple count-based retry, this means that the maxAttempts(long)
is applied to each burst individually.
public Flux<Long> generateCompanion(Flux<Retry.RetrySignal> flux)
RetryRetry.RetrySignal emissions, effectively
deciding when to retry.
When the source signals an error, that onError signal
will be suppressed. Its Throwable will instead be attached to a Retry.RetrySignal, immediately emitted
on the retrySignals publisher. Right after that emission,
request(1) is called on the companion publisher.
The response to that request decides if a retry should be made. Thus, the outer publisher will wait until a signal is emitted by the companion publisher, making it possible to delay retry attempts.
Any
onNext emitted by the companion publisher triggers a retry,
onError will fail the outer publisher and
onComplete will complete the outer publisher (effectively
suppressing the original error/Throwable).
As an example, the simplest form of retry companion would be to return the incoming Flux of Retry.RetrySignal
without modification. This would render a retry strategy that immediately retries, forever.
generateCompanion in class Retryflux - the errors from the outer publisher as Retry.RetrySignal objects,
containing the Throwable causing the error as well as retry counter metadata.Retry.RetrySignal emissions,
effectively deciding when to retry.