Top Description Inners Fields Constructors Methods
reactor.util.retry

public abstract Class Retry

extends Object
Class Inheritance
Known Direct Subclasses
reactor.util.retry.RetryBackoffSpec, reactor.util.retry.RetrySpec
Imports
java.time.Duration, java.util.function.Function, org.reactivestreams.Publisher, reactor.core.publisher.Flux, reactor.core.scheduler.Schedulers, reactor.util.context.Context, .ContextView

Base abstract class for a strategy to decide when to retry given a companion Flux of RetrySignal, for use with Flux#retryWhen(Retry) and reactor.core.publisher.Mono#retryWhen(Retry). Also provides access to configurable built-in strategies via static factory methods:

Users are encouraged to provide either concrete custom Retry strategies or builders that produce such concrete Retry. The RetrySpec returned by e.g. max(long) is a good inspiration for a fluent approach that generates a Retry at each step and uses immutability/copy-on-write to enable sharing of intermediate steps (that can thus be considered templates).

Author
Simon Baslé

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
Retry.RetrySignal

State used in Flux#retryWhen(Retry) and reactor.core.publisher.Mono#retryWhen(Retry), providing the Throwable that caused the source to fail as well as counters keeping track of retries.

Field Summary

Modifier and TypeField and Description
public final ContextView

Constructor Summary

AccessConstructor and Description
public
protected
Retry(ContextView retryContext)

Method Summary

Modifier and TypeMethod and Description
public static RetryBackoffSpec

Returns:

the exponential backoff spec for further configuration
backoff
(long
the maximum number of retry attempts to allow
maxAttempts
,
Duration
the minimum Duration for the first backoff
minBackoff
)

A RetryBackoffSpec preconfigured for exponential backoff strategy with jitter, given a maximum number of retry attempts and a minimum Duration for the backoff.

public static RetryBackoffSpec

Returns:

the fixed delays spec for further configuration
fixedDelay
(long
the maximum number of retry attempts to allow
maxAttempts
,
Duration
the Duration of the fixed delays
fixedDelay
)

A RetryBackoffSpec preconfigured for fixed delays (min backoff equals max backoff, no jitter), given a maximum number of retry attempts and the fixed Duration for the backoff.

public static final Retry

Returns:

the Retry strategy adapted from the Function
from
(Function<Flux<Retry.RetrySignal>, ? extends Publisher<?>>
the Function representing the desired Retry strategy as a lambda
function
)

A wrapper around Function to provide Retry by using lambda expressions.

public abstract Publisher<?>

Returns:

the companion publisher responsible for reacting to incoming RetrySignal emissions, effectively deciding when to retry.
generateCompanion
(Flux<Retry.RetrySignal>
the errors from the outer publisher as RetrySignal objects, containing the Throwable causing the error as well as retry counter metadata.
retrySignals
)

Generates the companion publisher responsible for reacting to incoming RetrySignal emissions, effectively deciding when to retry.

public static RetrySpec

Returns:

the retry indefinitely spec for further configuration
indefinitely
()

A RetrySpec preconfigured for the most simplistic retry strategy: retry immediately and indefinitely (similar to Flux#retry()).

public static RetrySpec

Returns:

the max attempt spec for further configuration
max
(long
the maximum number of retry attempts to allow
max
)

A RetrySpec preconfigured for a simple strategy with maximum number of retry attempts.

public static RetrySpec

Returns:

the max in a row spec for further configuration
maxInARow
(long
the maximum number of retry attempts to allow in a row, reset by successful onNext
maxInARow
)

A RetrySpec preconfigured for a simple strategy with maximum number of retry attempts over subsequent transient errors.

public ContextView

Returns:

the user provided context that will be accessible via RetrySignal#retryContextView().
retryContext
()

Return the user provided context that was set at construction time.

public static final Retry

Returns:

the Retry strategy adapted from the Function
withThrowable
(Function<Flux<Throwable>, ? extends Publisher<?>>
the Function representing the desired Retry strategy as a lambda
function
)

An adapter for Flux of Throwable-based Function to provide Retry from a legacy retryWhen Function.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

retryContextback to summary
public final ContextView retryContext

Constructor Detail

Retryback to summary
public Retry()
Retryback to summary
protected Retry(ContextView retryContext)

Method Detail

backoffback to summary
public static RetryBackoffSpec backoff(long maxAttempts, Duration minBackoff)

A RetryBackoffSpec preconfigured for exponential backoff strategy with jitter, given a maximum number of retry attempts and a minimum Duration for the backoff.

Parameters
maxAttempts:long

the maximum number of retry attempts to allow

minBackoff:Duration

the minimum Duration for the first backoff

Returns:RetryBackoffSpec

the exponential backoff spec for further configuration

See Also
RetryBackoffSpec#maxAttempts(long), RetryBackoffSpec#minBackoff(Duration)
fixedDelayback to summary
public static RetryBackoffSpec fixedDelay(long maxAttempts, Duration fixedDelay)

A RetryBackoffSpec preconfigured for fixed delays (min backoff equals max backoff, no jitter), given a maximum number of retry attempts and the fixed Duration for the backoff.

Note that calling RetryBackoffSpec#minBackoff(Duration) or RetryBackoffSpec#maxBackoff(Duration) would switch back to an exponential backoff strategy.

Parameters
maxAttempts:long

the maximum number of retry attempts to allow

fixedDelay:Duration

the Duration of the fixed delays

Returns:RetryBackoffSpec

the fixed delays spec for further configuration

See Also
RetryBackoffSpec#maxAttempts(long), RetryBackoffSpec#minBackoff(Duration), RetryBackoffSpec#maxBackoff(Duration)
fromback to summary
public static final Retry from(Function<Flux<Retry.RetrySignal>, ? extends Publisher<?>> function)

A wrapper around Function to provide Retry by using lambda expressions.

Parameters
function:Function<Flux<Retry.RetrySignal>, ? extends Publisher<?>>

the Function representing the desired Retry strategy as a lambda

Returns:Retry

the Retry strategy adapted from the Function

generateCompanionback to summary
public abstract Publisher<?> generateCompanion(Flux<Retry.RetrySignal> retrySignals)

Generates the companion publisher responsible for reacting to incoming RetrySignal emissions, effectively deciding when to retry.

When the source signals an error, that onError signal will be suppressed. Its Throwable will instead be attached to a RetrySignal, immediately emitted on the retrySignals publisher. Right after that emission, request(1) is called on the companion publisher.

The response to that request decides if a retry should be made. Thus, the outer publisher will wait until a signal is emitted by the companion publisher, making it possible to delay retry attempts.

Any onNext emitted by the companion publisher triggers a retry, onError will fail the outer publisher and onComplete will complete the outer publisher (effectively suppressing the original error/Throwable).

As an example, the simplest form of retry companion would be to return the incoming Flux of RetrySignal without modification. This would render a retry strategy that immediately retries, forever.

Parameters
retrySignals:Flux<Retry.RetrySignal>

the errors from the outer publisher as RetrySignal objects, containing the Throwable causing the error as well as retry counter metadata.

Returns:Publisher<?>

the companion publisher responsible for reacting to incoming RetrySignal emissions, effectively deciding when to retry.

indefinitelyback to summary
public static RetrySpec indefinitely()

A RetrySpec preconfigured for the most simplistic retry strategy: retry immediately and indefinitely (similar to Flux#retry()).

Returns:RetrySpec

the retry indefinitely spec for further configuration

maxback to summary
public static RetrySpec max(long max)

A RetrySpec preconfigured for a simple strategy with maximum number of retry attempts.

Parameters
max:long

the maximum number of retry attempts to allow

Returns:RetrySpec

the max attempt spec for further configuration

See Also
RetrySpec#maxAttempts(long)
maxInARowback to summary
public static RetrySpec maxInARow(long maxInARow)

A RetrySpec preconfigured for a simple strategy with maximum number of retry attempts over subsequent transient errors. An org.reactivestreams.Subscriber#onNext(Object) between errors resets the counter (see RetrySpec#transientErrors(boolean)).

Parameters
maxInARow:long

the maximum number of retry attempts to allow in a row, reset by successful onNext

Returns:RetrySpec

the max in a row spec for further configuration

See Also
RetrySpec#maxAttempts(long), RetrySpec#transientErrors(boolean)
retryContextback to summary
public ContextView retryContext()

Return the user provided context that was set at construction time.

Returns:ContextView

the user provided context that will be accessible via RetrySignal#retryContextView().

withThrowableback to summary
public static final Retry withThrowable(Function<Flux<Throwable>, ? extends Publisher<?>> function)

An adapter for Flux of Throwable-based Function to provide Retry from a legacy retryWhen Function.

Parameters
function:Function<Flux<Throwable>, ? extends Publisher<?>>

the Function representing the desired Retry strategy as a lambda

Returns:Retry

the Retry strategy adapted from the Function

reactor.util.retry back to summary

public Interface Retry.RetrySignal

Known Direct Implementers
reactor.core.publisher.FluxRetryWhen.RetryWhenMainSubscriber, reactor.util.retry.ImmutableRetrySignal

State used in Flux#retryWhen(Retry) and reactor.core.publisher.Mono#retryWhen(Retry), providing the Throwable that caused the source to fail as well as counters keeping track of retries.

Method Summary

Modifier and TypeMethod and Description
public default Retry.RetrySignal

Returns:

an immutable copy of the current RetrySignal, always safe to use
copy
()

An immutable copy of this RetrySignal which is guaranteed to give a consistent view of the state at the time at which this method is invoked.

public Throwable

Returns:

the current failure.
failure
()

The Throwable that caused the current onError signal.

public default ContextView

Returns:

a read-only view of a user provided context.
retryContextView
()

Return a read-only view of the user provided context, which may be used to store objects to be reset/rolled-back or otherwise mutated before or after a retry.

public long

Returns:

the total number of retries since the source first was subscribed to.
totalRetries
()

The total number of retries since the source first was subscribed to (in other words the number of errors -1 since the source was first subscribed to).

public long

Returns:

the number of retries since the latest onNext, or the number of retries since the source first was subscribed to if there hasn't been any onNext signals (in which case RetrySignal#totalRetries() and RetrySignal#totalRetriesInARow() are equivalent).
totalRetriesInARow
()

Retry counter resetting after each onNext (in other words the number of errors -1 since the latest onNext).

Method Detail

copyback to summary
public default Retry.RetrySignal copy()

An immutable copy of this RetrySignal which is guaranteed to give a consistent view of the state at the time at which this method is invoked. This is especially useful if this RetrySignal is a transient view of the state of the underlying retry subscriber.

Returns:Retry.RetrySignal

an immutable copy of the current RetrySignal, always safe to use

failureback to summary
public Throwable failure()

The Throwable that caused the current onError signal.

Returns:Throwable

the current failure.

retryContextViewback to summary
public default ContextView retryContextView()

Return a read-only view of the user provided context, which may be used to store objects to be reset/rolled-back or otherwise mutated before or after a retry.

Returns:ContextView

a read-only view of a user provided context.

totalRetriesback to summary
public long totalRetries()

The total number of retries since the source first was subscribed to (in other words the number of errors -1 since the source was first subscribed to).

Returns:long

the total number of retries since the source first was subscribed to.

totalRetriesInARowback to summary
public long totalRetriesInARow()

Retry counter resetting after each onNext (in other words the number of errors -1 since the latest onNext).

Returns:long

the number of retries since the latest onNext, or the number of retries since the source first was subscribed to if there hasn't been any onNext signals (in which case RetrySignal#totalRetries() and RetrySignal#totalRetriesInARow() are equivalent).