Package reactor.util.repeat
Class RepeatSpec
java.lang.Object
reactor.util.repeat.RepeatSpec
A repeat strategy that allows fine-grained control over repeating behavior in
Flux.repeatWhen(java.util.function.Function<reactor.core.publisher.Flux<java.lang.Long>, ? extends org.reactivestreams.Publisher<?>>) and
Mono.repeatWhen(java.util.function.Function<reactor.core.publisher.Flux<java.lang.Long>, ? extends org.reactivestreams.Publisher<?>>). This includes limiting the number of repeats, applying delays,
using custom predicates, attaching hooks, and applying jitter.
Use static factory methods such as times(long) to start building a
RepeatSpec.
The builder is immutable and supports a fluent copy-on-write style, so intermediate configurations can be safely reused.
Additional configuration includes:
- Conditional repetition via
onlyIf(Predicate) - Hook functions before and after each repeat trigger via
doBeforeRepeat(java.util.function.Consumer<reactor.util.repeat.RepeatSpec.RepeatSignal>)anddoAfterRepeat(java.util.function.Consumer<reactor.util.repeat.RepeatSpec.RepeatSignal>) - Custom delay strategies via
withFixedDelay(Duration),jitter(double), andwithScheduler(Scheduler) - Context propagation via
withRepeatContext(ContextView)
The companion Flux<Long> represents repeat signals, and each value corresponds to a repeat attempt.
This class transforms the repeat signals into a Publisher that determines whether to trigger a repeat.
- Author:
- Daeho Kwon
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceState information associated with each repeat signal, used in repeat strategies. -
Method Summary
Modifier and TypeMethodDescriptionPublisher<?>static RepeatSpeccreate(Predicate<RepeatSpec.RepeatSignal> predicate, long n) Creates aRepeatSpecthat repeats n times, only if the predicate returns true.doAfterRepeat(Consumer<RepeatSpec.RepeatSignal> doAfterRepeat) Adds a synchronous doAfterRepeat to be executed after the repeat trigger has completed.doBeforeRepeat(Consumer<RepeatSpec.RepeatSignal> doBeforeRepeat) Adds a synchronous doBeforeRepeat to be executed before the repeat trigger.generateCompanion(Flux<Long> signals) Generates the companion publisher responsible for reacting to incoming repeat signals, effectively deciding whether to trigger another repeat cycle.jitter(double jitter) Applies jitter to the configured fixed delay.static RepeatSpeconce()Creates aRepeatSpecthat repeats once.onlyIf(Predicate<RepeatSpec.RepeatSignal> predicate) Sets a predicate that determines whether a repeat should occur based on the currentRepeatSpec.RepeatSignal.static RepeatSpectimes(long n) Creates aRepeatSpecthat repeats n times.withFixedDelay(Duration delay) Applies a fixed delay between repeat iterations.withRepeatContext(ContextView repeatContext) Set the user providedcontextViewthat can be used to manipulate state on retries.withScheduler(Scheduler scheduler) Sets aSchedulerto use for delaying repeat attempts.
-
Method Details
-
once
Creates aRepeatSpecthat repeats once.- Returns:
- a new
RepeatSpecinstance
-
times
Creates aRepeatSpecthat repeats n times.- Parameters:
n- number of repeats- Returns:
- a new
RepeatSpecinstance
-
create
Creates aRepeatSpecthat repeats n times, only if the predicate returns true.- Parameters:
predicate- Predicate that determines if next repeat is performedn- number of repeats- Returns:
- a new
RepeatSpecinstance
-
withRepeatContext
Set the user providedcontextViewthat can be used to manipulate state on retries.- Parameters:
repeatContext- a new snapshot of user provided data- Returns:
- a new copy of the
RepeatSpecwhich can either be further configured.
-
onlyIf
Sets a predicate that determines whether a repeat should occur based on the currentRepeatSpec.RepeatSignal. This allows conditional repetition based on iteration count, companion value, delay, or context.- Parameters:
predicate- the condition to test for eachRepeatSpec.RepeatSignal- Returns:
- a new copy of this
RepeatSpecwith the updated predicate
-
doBeforeRepeat
Adds a synchronous doBeforeRepeat to be executed before the repeat trigger. This is commonly used for side effects like logging or state updates right before each repetition.- Parameters:
doBeforeRepeat- theConsumerto invoke with the currentRepeatSpec.RepeatSignal- Returns:
- a new copy of this
RepeatSpecwith the doBeforeRepeat applied
-
doAfterRepeat
Adds a synchronous doAfterRepeat to be executed after the repeat trigger has completed. This is useful for tracking completion, updating metrics, or performing cleanup work after each repeat cycle.- Parameters:
doAfterRepeat- theConsumerto invoke with the currentRepeatSpec.RepeatSignal- Returns:
- a new copy of this
RepeatSpecwith the doAfterRepeat applied
-
withFixedDelay
Applies a fixed delay between repeat iterations.- Parameters:
delay- theDurationof delay to apply between repeat attempts- Returns:
- a new copy of this
RepeatSpecwith the delay applied
-
jitter
Applies jitter to the configured fixed delay. This randomizes the delay duration within a bounded range for each repeat.- Parameters:
jitter- jitter factor as a percentage of the base delay (e.g. 0.1 = ±10%)- Returns:
- a new copy of this
RepeatSpecwith jitter configured
-
withScheduler
Sets aSchedulerto use for delaying repeat attempts.- Parameters:
scheduler- the scheduler to apply- Returns:
- a new copy of this
RepeatSpecwith the scheduler applied
-
apply
-
generateCompanion
Generates the companion publisher responsible for reacting to incoming repeat signals, effectively deciding whether to trigger another repeat cycle.- Parameters:
signals- the incoming repeat signals, where eachLongvalue indicates the iteration index- Returns:
- the companion publisher that determines if a repeat should occur
-