Interface Repeat<T>

  • Type Parameters:
    T - Application context type
    All Superinterfaces:
    Function<Flux<Long>,​org.reactivestreams.Publisher<Long>>
    All Known Implementing Classes:
    DefaultRepeat

    public interface Repeat<T>
    extends Function<Flux<Long>,​org.reactivestreams.Publisher<Long>>
    Repeat function that may be used with 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 Detail

      • onlyIf

        static <T> Repeat<T> onlyIf​(Predicate<? super RepeatContext<T>> predicate)
        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

        static <T> Repeat<T> once()
        Repeat function that repeats once.
        Returns:
        Repeat function for one repeat
      • times

        static <T> Repeat<T> times​(long n)
        Repeat function that repeats n times.
        Parameters:
        n - number of repeats
        Returns:
        Repeat function for n repeats
      • create

        static <T> Repeat<T> create​(Predicate<? super RepeatContext<T>> predicate,
                                    long n)
        Repeat function 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:
        Repeat function with predicate and n repeats
      • withApplicationContext

        Repeat<T> withApplicationContext​(T applicationContext)
        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 predicate 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.
        Parameters:
        applicationContext - Application context
        Returns:
        repeat function with associated application context
      • doOnRepeat

        Repeat<T> doOnRepeat​(Consumer<? super RepeatContext<T>> onRepeat)
        Returns a repeat function that invokes the provided onRepeat callback before every repeat. The 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.
        Parameters:
        onRepeat - callback to invoke before repeats
        Returns:
        repeat function with callback
      • timeout

        Repeat<T> timeout​(Duration 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

        Repeat<T> repeatMax​(long maxRepeats)
        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

        Repeat<T> backoff​(Backoff 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

        Repeat<T> jitter​(Jitter 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

        Repeat<T> withBackoffScheduler​(Scheduler scheduler)
        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

        default Repeat<T> 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

        default Repeat<T> fixedBackoff​(Duration backoffInterval)
        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

        default Repeat<T> exponentialBackoff​(Duration firstBackoff,
                                             Duration maxBackoff)
        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. If maxBackoff is not null, the maximum backoff applied will be limited to maxBackoff.

        Parameters:
        firstBackoff - the delay for the first backoff, which is also used as the coefficient for subsequent backoffs
        maxBackoff - the maximum backoff delay before a repeat
        Returns:
        repeat function with exponential backoff delay
      • exponentialBackoffWithJitter

        default Repeat<T> exponentialBackoffWithJitter​(Duration firstBackoff,
                                                       Duration maxBackoff)
        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 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.

        Parameters:
        firstBackoff - the delay for the first backoff, which is also used as the coefficient for subsequent backoffs
        maxBackoff - the maximum backoff delay before a repeat
        Returns:
        repeat function with full jitter backoff strategy
      • randomBackoff

        default Repeat<T> randomBackoff​(Duration firstBackoff,
                                        Duration maxBackoff)
        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 of firstBackoff. If maxBackoff is not null, the maximum backoff applied will be limited to maxBackoff.

        Parameters:
        firstBackoff - the delay for the first backoff, also used as minimum backoff
        maxBackoff - the maximum backoff delay before a repeat
        Returns:
        repeat function with de-correlated jitter backoff strategy
      • apply

        default <S> Flux<S> apply​(org.reactivestreams.Publisher<S> source)
        Transforms the source into a repeating 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);
         
        Parameters:
        source - the source publisher
        Returns:
        Flux with the repeat properties of this repeat function