Interface Retry<T>
- Type Parameters:
T
- Application context type
- All Known Implementing Classes:
DefaultRetry
This class is deprecated since reactor now ships with its own retry logic.
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 Summary
Modifier and TypeMethodDescriptionstatic <T> Retry<T>
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>
Returns a retry function that retries errors resulting from any of the specified exceptions, once.default <S> Flux<S>
apply
(org.reactivestreams.Publisher<S> source) Transforms the source into a retryingFlux
based on the properties configured for this function.Returns a retry function with backoff delay.doOnRetry
(Consumer<? super RetryContext<T>> onRetry) Returns a retry function that invokes the provided onRetry callback before every retry.exponentialBackoff
(Duration firstBackoff, Duration maxBackoff) Returns a retry function with exponential backoff delay.exponentialBackoffWithJitter
(Duration firstBackoff, Duration maxBackoff) Returns a retry function with full jitter backoff strategy.fixedBackoff
(Duration backoffInterval) Returns a retry function with fixed backoff delay.Returns a retry function that applies jitter to the backoff delay.Returns a retry function with no backoff delay.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.randomBackoff
(Duration firstBackoff, Duration maxBackoff) Returns a retry function with random de-correlated jitter backoff strategy.retryMax
(long maxRetries) Retry function that retries n times.Retry function that retries once.Returns a retry function with timeout.default Retry
Converts this retry function to a reactor retry function for compatibility.withApplicationContext
(T applicationContext) Returns a retry function with an application context that may be used to perform any rollbacks before a retry.withBackoffScheduler
(Scheduler scheduler) Returns a retry function that uses the scheduler provided for backoff delays.
-
Method Details
-
any
Returns a retry function that retries any exception, once. More constraints may be added usingretryMax(long)
ortimeout(Duration)
.- Returns:
- retry function that retries on any exception
-
anyOf
Returns a retry function that retries errors resulting from any of the specified exceptions, once. More constraints may be added usingretryMax(long)
ortimeout(Duration)
.- Parameters:
retriableExceptions
- Exceptions that may be retried- Returns:
- retry function that retries indefinitely, only for specified exceptions
-
allBut
Returns a retry function that retries errors resulting from all exceptions except the specified non-retriable exceptions, once. More constraints may be added usingretryMax(long)
ortimeout(Duration)
.- Parameters:
nonRetriableExceptions
- exceptions that may not be retried- Returns:
- retry function that retries all exceptions except the specified non-retriable exceptions.
-
onlyIf
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
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 predicateonlyIf(Predicate)
, custom backoff functionbackoff(Backoff)
and retry callbackdoOnRetry(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
Returns a retry function that invokes the provided onRetry callback before every retry. TheRetryContext
provided to the callback contains the iteration and the any application context set usingwithApplicationContext(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
Retry function that retries once.- Returns:
- Retry function for one retry
-
retryMax
Retry function that retries n times.- Parameters:
maxRetries
- number of retries- Returns:
- Retry function for n retries
-
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
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
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
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
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
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
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. IfmaxBackoff
is not null, the maximum backoff applied will be limited tomaxBackoff
.- Parameters:
firstBackoff
- the delay for the first backoff, which is also used as the coefficient for subsequent backoffsmaxBackoff
- the maximum backoff delay before a retry- Returns:
- retry function with exponential backoff delay
-
exponentialBackoffWithJitter
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
andfirstBackoff * (2 ** n)
where n is the next iteration number. IfmaxBackoff
is not null, the maximum backoff applied will be limited tomaxBackoff
.- Parameters:
firstBackoff
- the delay for the first backoff, which is also used as the coefficient for subsequent backoffsmaxBackoff
- the maximum backoff delay before a retry- Returns:
- retry function with full jitter backoff strategy
-
randomBackoff
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 offirstBackoff
. IfmaxBackoff
is not null, the maximum backoff applied will be limited tomaxBackoff
.- Parameters:
firstBackoff
- the delay for the first backoff, also used as minimum backoffmaxBackoff
- the maximum backoff delay before a retry- Returns:
- retry function with de-correlated jitter backoff strategy
-
apply
Transforms the source into a retryingFlux
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
-
toReactorRetry
Converts this retry function to a reactor retry function for compatibility.- Returns:
- a converted retry function.
-