Class BackOffPolicyBuilder
java.lang.Object
org.springframework.retry.backoff.BackOffPolicyBuilder
Fluent API for creating a
BackOffPolicy
based on given attributes. The delay
values are expressed in milliseconds. If any provided value is less than one, the
resulting policy will set it to one. The default policy is a FixedBackOffPolicy
with a delay of 1000ms.
Examples:
// DefaultFixedBackOffPolicy
with 1000ms delay BackOffPolicyBuilder .newDefaultPolicy(); //FixedBackOffPolicy
BackOffPolicyBuilder .newBuilder() .delay(2000) .build(); //UniformRandomBackOffPolicy
BackOffPolicyBuilder .newBuilder() .delay(500) .maxDelay(1000) .build(); //ExponentialBackOffPolicy
BackOffPolicyBuilder .newBuilder() .delay(1000) .maxDelay(5000) .multiplier(2) .build(); //ExponentialRandomBackOffPolicy
with providedSleeper
BackOffPolicyBuilder .newBuilder() .delay(3000) .maxDelay(5000) .multiplier(1.5) .random(true) .sleeper(mySleeper) .build();
Not thread safe. Building should be performed in a single thread. The resulting
BackOffPolicy
however is expected to be thread-safe and designed for moderate
load concurrent access.
- Since:
- 1.3.3
- Author:
- Tomaz Fernandes, Aftab Shaikh
-
Method Summary
Modifier and TypeMethodDescriptionbuild()
Builds theBackOffPolicy
with the given parameters.delay
(long delay) A canonical backoff period.delaySupplier
(Supplier<Long> delaySupplier) Set a supplier for the delay.maxDelay
(long maxDelay) The maximum wait in milliseconds between retries.maxDelaySupplier
(Supplier<Long> maxDelaySupplier) Set a supplier for the max delay.multiplier
(double multiplier) If positive, then used as a multiplier for generating the next delay for backoff.multiplierSupplier
(Supplier<Double> multiplierSupplier) Set a supplier for the multiplier.static BackOffPolicyBuilder
Creates a newBackOffPolicyBuilder
instance.static BackOffPolicy
Creates a newFixedBackOffPolicy
instance with a delay of 1000ms.random
(boolean random) In the exponential case (multiplier
> 0) set this to true to have the backoff delays randomized, so that the maximum delay is multiplier times the previous delay and the distribution is uniform between the two values.randomSupplier
(Supplier<Boolean> randomSupplier) Set a supplier for the random.TheSleeper
instance to be used to back off.
-
Method Details
-
newBuilder
Creates a newBackOffPolicyBuilder
instance.- Returns:
- the builder instance
-
newDefaultPolicy
Creates a newFixedBackOffPolicy
instance with a delay of 1000ms.- Returns:
- the back off policy instance
-
delay
A canonical backoff period. Used as an initial value in the exponential case, and as a minimum value in the uniform case.- Parameters:
delay
- the initial or canonical backoff period in milliseconds- Returns:
- this
-
maxDelay
The maximum wait in milliseconds between retries. If less thandelay(long)
then a default value is applied depending on the resulting policy.- Parameters:
maxDelay
- the maximum wait between retries in milliseconds- Returns:
- this
-
multiplier
If positive, then used as a multiplier for generating the next delay for backoff.- Parameters:
multiplier
- a multiplier to use to calculate the next backoff delay- Returns:
- this
-
random
In the exponential case (multiplier
> 0) set this to true to have the backoff delays randomized, so that the maximum delay is multiplier times the previous delay and the distribution is uniform between the two values.- Parameters:
random
- the flag to signal randomization is required- Returns:
- this
-
sleeper
TheSleeper
instance to be used to back off. Policies default toThreadWaitSleeper
.- Parameters:
sleeper
- theSleeper
instance- Returns:
- this
-
delaySupplier
Set a supplier for the delay.- Parameters:
delaySupplier
- the supplier.- Returns:
- this
- Since:
- 2.0
-
maxDelaySupplier
Set a supplier for the max delay.- Parameters:
maxDelaySupplier
- the supplier.- Returns:
- this
- Since:
- 2.0
-
multiplierSupplier
Set a supplier for the multiplier.- Parameters:
multiplierSupplier
- the supplier.- Returns:
- this
- Since:
- 2.0
-
randomSupplier
Set a supplier for the random.- Parameters:
randomSupplier
- the supplier.- Returns:
- this
- Since:
- 2.0
-
build
Builds theBackOffPolicy
with the given parameters.- Returns:
- the
BackOffPolicy
instance
-