Class RepeatSpec

java.lang.Object
reactor.util.repeat.RepeatSpec
All Implemented Interfaces:
Function<Flux<Long>,Publisher<?>>

public final class RepeatSpec extends Object implements Function<Flux<Long>,Publisher<?>>
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:

This strategy does not retry based on error signals, but rather repeats a successful sequence (until termination condition is met).

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
  • Method Details

    • once

      public static RepeatSpec once()
      Creates a RepeatSpec that repeats once.
      Returns:
      a new RepeatSpec instance
    • times

      public static RepeatSpec times(long n)
      Creates a RepeatSpec that repeats n times.
      Parameters:
      n - number of repeats
      Returns:
      a new RepeatSpec instance
    • create

      public static RepeatSpec create(Predicate<RepeatSpec.RepeatSignal> predicate, long n)
      Creates a RepeatSpec that repeats n times, only if the predicate returns true.
      Parameters:
      predicate - Predicate that determines if next repeat is performed
      n - number of repeats
      Returns:
      a new RepeatSpec instance
    • withRepeatContext

      public RepeatSpec withRepeatContext(ContextView repeatContext)
      Set the user provided contextView that can be used to manipulate state on retries.
      Parameters:
      repeatContext - a new snapshot of user provided data
      Returns:
      a new copy of the RepeatSpec which can either be further configured.
    • onlyIf

      public RepeatSpec onlyIf(Predicate<RepeatSpec.RepeatSignal> predicate)
      Sets a predicate that determines whether a repeat should occur based on the current RepeatSpec.RepeatSignal. This allows conditional repetition based on iteration count, companion value, delay, or context.
      Parameters:
      predicate - the condition to test for each RepeatSpec.RepeatSignal
      Returns:
      a new copy of this RepeatSpec with the updated predicate
    • doBeforeRepeat

      public RepeatSpec doBeforeRepeat(Consumer<RepeatSpec.RepeatSignal> 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 - the Consumer to invoke with the current RepeatSpec.RepeatSignal
      Returns:
      a new copy of this RepeatSpec with the doBeforeRepeat applied
    • doAfterRepeat

      public RepeatSpec doAfterRepeat(Consumer<RepeatSpec.RepeatSignal> 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 - the Consumer to invoke with the current RepeatSpec.RepeatSignal
      Returns:
      a new copy of this RepeatSpec with the doAfterRepeat applied
    • withFixedDelay

      public RepeatSpec withFixedDelay(Duration delay)
      Applies a fixed delay between repeat iterations.
      Parameters:
      delay - the Duration of delay to apply between repeat attempts
      Returns:
      a new copy of this RepeatSpec with the delay applied
    • jitter

      public RepeatSpec jitter(double 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 RepeatSpec with jitter configured
    • withScheduler

      public RepeatSpec withScheduler(Scheduler scheduler)
      Sets a Scheduler to use for delaying repeat attempts.
      Parameters:
      scheduler - the scheduler to apply
      Returns:
      a new copy of this RepeatSpec with the scheduler applied
    • apply

      public Publisher<?> apply(Flux<Long> signals)
      Specified by:
      apply in interface Function<Flux<Long>,Publisher<?>>
    • generateCompanion

      public Publisher<Long> generateCompanion(Flux<Long> signals)
      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 each Long value indicates the iteration index
      Returns:
      the companion publisher that determines if a repeat should occur