T
- Application context typepublic interface Repeat<T> extends Function<reactor.core.publisher.Flux<Long>,org.reactivestreams.Publisher<Long>>
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);
repeatMax(long)
method to change that post-construction, for the case
where one wants to fine tune that aspect for specific use-cases. For instance, the
default configuration could be an unlimited amount of attempts (Long.MAX_VALUE
)
with a global timeout(Duration)
, but for some specific cases you might want
to change both the timeout and limit the attempts.Modifier and Type | Method and Description |
---|---|
default <S> reactor.core.publisher.Flux<S> |
apply(org.reactivestreams.Publisher<S> source)
Transforms the source into a repeating
Flux based on the properties
configured for this function. |
Repeat<T> |
backoff(Backoff backoff)
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.
|
Repeat<T> |
doOnRepeat(Consumer<? super RepeatContext<T>> onRepeat)
Returns a repeat function that invokes the provided onRepeat
callback before every repeat.
|
default Repeat<T> |
exponentialBackoff(Duration firstBackoff,
Duration maxBackoff)
Returns a repeat function with exponential backoff delay.
|
default Repeat<T> |
exponentialBackoffWithJitter(Duration firstBackoff,
Duration maxBackoff)
Returns a repeat function with full jitter backoff strategy.
|
default Repeat<T> |
fixedBackoff(Duration backoffInterval)
Returns a repeat function with fixed backoff delay.
|
Repeat<T> |
jitter(Jitter jitter)
Returns a repeat function that applies jitter to the backoff delay.
|
default Repeat<T> |
noBackoff()
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.
|
default Repeat<T> |
randomBackoff(Duration firstBackoff,
Duration maxBackoff)
Returns a repeat function with random de-correlated jitter backoff strategy.
|
Repeat<T> |
repeatMax(long maxRepeats)
Returns a repeat function that repeats at most n times.
|
Repeat<T> |
timeout(Duration timeout)
Returns a repeat function with timeout.
|
static <T> Repeat<T> |
times(long n)
Repeat function that repeats n times.
|
Repeat<T> |
withApplicationContext(T applicationContext)
Returns a repeat function with an application context that may be
used to perform any rollbacks before a repeat.
|
Repeat<T> |
withBackoffScheduler(reactor.core.scheduler.Scheduler scheduler)
Returns a repeat function that uses the scheduler provided for
backoff delays.
|
static <T> Repeat<T> onlyIf(Predicate<? super RepeatContext<T>> predicate)
predicate
- Predicate that determines if next repeat is performedstatic <T> Repeat<T> once()
static <T> Repeat<T> times(long n)
n
- number of repeatsstatic <T> Repeat<T> create(Predicate<? super RepeatContext<T>> predicate, long n)
predicate
- Predicate that determines if next repeat is performedn
- number of repeatsRepeat<T> withApplicationContext(T applicationContext)
onlyIf(Predicate)
,
custom backoff function backoff(Backoff)
and repeat
callback doOnRepeat(Consumer)
. All other properties of
this repeat function are retained in the returned instance.applicationContext
- Application contextRepeat<T> doOnRepeat(Consumer<? super RepeatContext<T>> onRepeat)
RepeatContext
provided
to the callback contains the iteration and the any application
context set using withApplicationContext(Object)
.
All other properties of this repeat function are retained in the
returned instance.onRepeat
- callback to invoke before repeatsRepeat<T> timeout(Duration timeout)
timeout
- timeout after which no new repeats are initiatedRepeat<T> repeatMax(long maxRepeats)
maxRepeats
- number of repeatsRepeat<T> backoff(Backoff backoff)
backoff
- the backoff function to determine backoff delayRepeat<T> jitter(Jitter jitter)
jitter
- Jitter function to randomize backoff delayRepeat<T> withBackoffScheduler(reactor.core.scheduler.Scheduler scheduler)
scheduler
- the scheduler for backoff delaysdefault Repeat<T> noBackoff()
default Repeat<T> fixedBackoff(Duration backoffInterval)
backoffInterval
- fixed backoff delay applied before every repeatdefault Repeat<T> exponentialBackoff(Duration firstBackoff, Duration maxBackoff)
Repeats 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 repeatdefault Repeat<T> exponentialBackoffWithJitter(Duration firstBackoff, Duration maxBackoff)
Repeats 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 repeatdefault Repeat<T> randomBackoff(Duration firstBackoff, Duration maxBackoff)
Repeats 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 repeatdefault <S> reactor.core.publisher.Flux<S> apply(org.reactivestreams.Publisher<S> source)
Flux
based on the properties
configured for this function.
Example usage:
repeat = Repeat.times(n)
.exponentialBackoff(Duration.ofMillis(100), Duration.ofSeconds(60));
flux.as(repeat);
source
- the source publisherFlux
with the repeat properties of this repeat function