Interface Repeat<T>
- Type Parameters:
T
- Application context type
- All Known Implementing Classes:
DefaultRepeat
Flux.repeatWhen(Function)
,
Mono.repeatWhen(Function)
and Mono.repeatWhenEmpty(Function)
.
Each change in configuration returns a new instance (copy configuration), which
makes Repeat
suitable for creating configuration templates that can be fine
tuned for specific cases without impacting the original general use-case configuration.
Example usage:
repeat = Repeat.times(10)
.randomBackoff(Duration.ofMillis(100), Duration.ofSeconds(60))
.withApplicationContext(appContext)
.doOnRepeat(context -> context.applicationContext().rollback());
flux.repeatWhen(repeat);
-
Method Summary
Modifier and TypeMethodDescriptiondefault <S> Flux
<S> apply
(org.reactivestreams.Publisher<S> source) Transforms the source into a repeatingFlux
based on the properties configured for this function.Returns a repeat function with backoff delay.static <T> Repeat
<T> create
(Predicate<? super RepeatContext<T>> predicate, long n) Repeat function that repeats n times, only if the predicate returns true.doOnRepeat
(Consumer<? super RepeatContext<T>> onRepeat) Returns a repeat function that invokes the provided onRepeat callback before every repeat.exponentialBackoff
(Duration firstBackoff, Duration maxBackoff) Returns a repeat function with exponential backoff delay.exponentialBackoffWithJitter
(Duration firstBackoff, Duration maxBackoff) Returns a repeat function with full jitter backoff strategy.fixedBackoff
(Duration backoffInterval) Returns a repeat function with fixed backoff delay.Returns a repeat function that applies jitter to the backoff delay.Returns a repeat function with no backoff delay.static <T> Repeat
<T> once()
Repeat function that repeats once.static <T> Repeat
<T> onlyIf
(Predicate<? super RepeatContext<T>> predicate) Repeat function that repeats only if the predicate returns true.randomBackoff
(Duration firstBackoff, Duration maxBackoff) Returns a repeat function with random de-correlated jitter backoff strategy.repeatMax
(long maxRepeats) Returns a repeat function that repeats at most n times.Returns a repeat function with timeout.static <T> Repeat
<T> times
(long n) Repeat function that repeats n times.withApplicationContext
(T applicationContext) Returns a repeat function with an application context that may be used to perform any rollbacks before a repeat.withBackoffScheduler
(Scheduler scheduler) Returns a repeat function that uses the scheduler provided for backoff delays.
-
Method Details
-
onlyIf
Repeat function that repeats only if the predicate returns true.- Parameters:
predicate
- Predicate that determines if next repeat is performed- Returns:
- Repeat function with predicate
-
once
Repeat function that repeats once.- Returns:
- Repeat function for one repeat
-
times
Repeat function that repeats n times.- Parameters:
n
- number of repeats- Returns:
- Repeat function for n repeats
-
create
Repeat function that repeats n times, only if the predicate returns true.- Parameters:
predicate
- Predicate that determines if next repeat is performedn
- number of repeats- Returns:
- Repeat function with predicate and n repeats
-
withApplicationContext
Returns a repeat function with an application context that may be used to perform any rollbacks before a repeat. This application context is provided to any repeat predicateonlyIf(Predicate)
, custom backoff functionbackoff(Backoff)
and repeat callbackdoOnRepeat(Consumer)
. All other properties of this repeat function are retained in the returned instance.- Parameters:
applicationContext
- Application context- Returns:
- repeat function with associated application context
-
doOnRepeat
Returns a repeat function that invokes the provided onRepeat callback before every repeat. TheRepeatContext
provided to the callback contains the iteration and the any application context set usingwithApplicationContext(Object)
. All other properties of this repeat function are retained in the returned instance.- Parameters:
onRepeat
- callback to invoke before repeats- Returns:
- repeat function with callback
-
timeout
Returns a repeat function with timeout. The timeout starts from the instant that this function is applied and switches to unlimited number of attempts. All other properties of this repeat function are retained in the returned instance.- Parameters:
timeout
- timeout after which no new repeats are initiated- Returns:
- repeat function with timeout
-
repeatMax
Returns a repeat function that repeats at most n times. All other properties of this repeat function are retained in the returned instance.- Parameters:
maxRepeats
- number of repeats- Returns:
- Retry function for n repeats
-
backoff
Returns a repeat function with backoff delay. All other properties of this repeat function are retained in the returned instance.- Parameters:
backoff
- the backoff function to determine backoff delay- Returns:
- repeat function with backoff
-
jitter
Returns a repeat function that applies jitter to the backoff delay. All other properties of this repeat function are retained in the returned instance.- Parameters:
jitter
- Jitter function to randomize backoff delay- Returns:
- repeat function with jitter for backoff
-
withBackoffScheduler
Returns a repeat function that uses the scheduler provided for backoff delays. All other properties of this repeat function are retained in the returned instance.- Parameters:
scheduler
- the scheduler for backoff delays- Returns:
- repeat function with backoff scheduler
-
noBackoff
Returns a repeat function with no backoff delay. This is the default. All other properties of this repeat function are retained in the returned instance.- Returns:
- repeat function with no backoff delay
-
fixedBackoff
Returns a repeat function with fixed backoff delay. All other properties of this repeat function are retained in the returned instance.- Parameters:
backoffInterval
- fixed backoff delay applied before every repeat- Returns:
- repeat function with fixed backoff delay
-
exponentialBackoff
Returns a repeat function with exponential backoff delay. All other properties of this repeat function are retained in the returned instance.Repeats 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 repeat- Returns:
- repeat function with exponential backoff delay
-
exponentialBackoffWithJitter
Returns a repeat function with full jitter backoff strategy. All other properties of this repeat function are retained in the returned instance.Repeats 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 repeat- Returns:
- repeat function with full jitter backoff strategy
-
randomBackoff
Returns a repeat function with random de-correlated jitter backoff strategy. All other properties of this repeat function are retained in the returned instance.Repeats 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 repeat- Returns:
- repeat function with de-correlated jitter backoff strategy
-
apply
Transforms the source into a repeatingFlux
based on the properties configured for this function.Example usage:
repeat = Repeat.times(n) .exponentialBackoff(Duration.ofMillis(100), Duration.ofSeconds(60)); flux.as(repeat);
- Parameters:
source
- the source publisher- Returns:
Flux
with the repeat properties of this repeat function
-