public final class Retrier
extends java.lang.Object
The Retrier executes an action and can be passed a predicate to determine if a retry should occur on failure.
Example usage to create an ActionRetrier with a default time generator to pause for 5 seconds and repeat 10 times (retries). The retrier will run an action and the result will be tested to see if it should be retried.
final ActionRetrier retrier =
Retrier.createSleepRetrier(Retrier.createConstantTimeGenerator(5000), 10);
final Predicate<Result> predicate =
Retrier.exceptionsFilterPredicate(IllegalArgumentException.class);
// always fails and will retry...
final Result result =
retrier.run(() -> Result.failure(() -> new Exception()), predicate);
// more code
The class is configured with a number of static members and methods to create default time generators and action retriers.
Example to create an ActionRetrier using the default capped incrementing time generator with 5 retry attempts:
final ActionRetrier retrier =
Retrier.createSleepRetrier(Retrier.DEFAULT_CAPPED_INCREMENTING_TIME_GENERATOR, 5);
// default - filter all
final Predicate<Result> predicate = Retrier.PREDICATE_EXCEPTIONS_FILTER_ALL;
// will not retry as predicate test fails...
final Result result = retrier.run(() -> Result.failure(() -> new Exception()), predicate);
// more code
| Modifier and Type | Field and Description |
|---|---|
static java.util.function.Supplier<java.util.Iterator<java.lang.Long>> |
DEFAULT_CAPPED_INCREMENTING_TIME_GENERATOR |
static java.util.function.Supplier<java.util.Iterator<java.lang.Long>> |
DEFAULT_CONSTANT_TIME_GENERATOR |
static long |
TIME_GENERATOR_DEFAULT_CAPPED_DELAY_IN_MILLISECONDS |
static long |
TIME_GENERATOR_DEFAULT_DELAY_IN_MILLISECONDS |
static int |
TIME_GENERATOR_DEFAULT_RETRIES_BETWEEN_STEPS |
static long |
TIME_GENERATOR_DEFAULT_STEP_MULTIPLIER |
| Modifier and Type | Method and Description |
|---|---|
static java.util.function.Supplier<java.util.Iterator<java.lang.Long>> |
createCappedIncrementingTimeGenerator(long initialDelayInMillis,
long cappedDelayInMillis,
long stepMultiplier,
int retriesBetweenSteps)
Method to create a capped time incrementing time generator (iterator) from the specified arguments.
|
static java.util.function.Supplier<java.util.Iterator<java.lang.Long>> |
createConstantTimeGenerator(long timeInMiliseconds)
Create a constant time generator.
|
static <T> ActionRetrier<T> |
createContinuousSleepRetrier(java.util.function.Supplier<java.util.Iterator<java.lang.Long>> timeGenerator)
Method to create an ActionRetrier that has no limit on the the number of retry attempts.
|
static <T> ActionRetrier<T> |
createDefaultCappedIncrementingRetrier()
Create a retrier that that will increment the time between retry attempts if a failure occurs.
|
static <T> ActionRetrier<T> |
createDefaultContinuousCappedIncrementingRetrier()
Create a retrier that will keep retrying the action until success and incrementing the time between attempts.
|
static <T> ActionRetrier<T> |
createDefaultContinuousConstantSleepRetrier()
Create a retrier that will continually retry with a constant time between retry attempts.
|
static <T> java.util.function.Predicate<Result<T>> |
createDefaultExceptionPredicate()
A predicate that returns true for any exceptions derived from
Exception. |
static <T> ActionRetrier<T> |
createDefaultSingleSleepRetrier()
Create a retrier that will retry an action once if the action fails.
|
static <T> ActionRetrier<T> |
createNonRetrier()
Create a retrier that executes the action once only and will never retry.
|
static <T> ActionRetrier<T> |
createSleepRetrier(java.util.function.Supplier<java.util.Iterator<java.lang.Long>> timeGenerator,
int numberOfRetries)
Method to return a ActionRetrier to execute an action.
|
static <T> java.util.function.Predicate<Result<T>> |
exceptionsFilterPredicate(java.lang.Class... recoverableTypes)
A predicate that matches against a set of exception types.
|
public static final long TIME_GENERATOR_DEFAULT_DELAY_IN_MILLISECONDS
public static final long TIME_GENERATOR_DEFAULT_CAPPED_DELAY_IN_MILLISECONDS
public static final long TIME_GENERATOR_DEFAULT_STEP_MULTIPLIER
public static final int TIME_GENERATOR_DEFAULT_RETRIES_BETWEEN_STEPS
public static final java.util.function.Supplier<java.util.Iterator<java.lang.Long>> DEFAULT_CAPPED_INCREMENTING_TIME_GENERATOR
public static final java.util.function.Supplier<java.util.Iterator<java.lang.Long>> DEFAULT_CONSTANT_TIME_GENERATOR
public static <T> java.util.function.Predicate<Result<T>> createDefaultExceptionPredicate()
Exception.T - the type of objectpublic static <T> java.util.function.Predicate<Result<T>> exceptionsFilterPredicate(java.lang.Class... recoverableTypes)
T - the object typerecoverableTypes - The recoverable exception typespublic static <T> ActionRetrier<T> createNonRetrier()
T - the object typeActionRetrierpublic static <T> ActionRetrier<T> createDefaultSingleSleepRetrier()
T - the object typeActionRetrierpublic static <T> ActionRetrier<T> createDefaultContinuousConstantSleepRetrier()
T - the object typeActionRetrierpublic static <T> ActionRetrier<T> createDefaultCappedIncrementingRetrier()
T - the object typeActionRetrierpublic static <T> ActionRetrier<T> createDefaultContinuousCappedIncrementingRetrier()
T - the object typeActionRetrierpublic static <T> ActionRetrier<T> createSleepRetrier(java.util.function.Supplier<java.util.Iterator<java.lang.Long>> timeGenerator, int numberOfRetries)
T - the object typetimeGenerator - the time generator used to derive sleep timesnumberOfRetries - the number of retry attemptspublic static <T> ActionRetrier<T> createContinuousSleepRetrier(java.util.function.Supplier<java.util.Iterator<java.lang.Long>> timeGenerator)
T - the object typetimeGenerator - the time generator used to derive sleep timespublic static java.util.function.Supplier<java.util.Iterator<java.lang.Long>> createConstantTimeGenerator(long timeInMiliseconds)
timeInMiliseconds - the time that the generator will always returnpublic static java.util.function.Supplier<java.util.Iterator<java.lang.Long>> createCappedIncrementingTimeGenerator(long initialDelayInMillis,
long cappedDelayInMillis,
long stepMultiplier,
int retriesBetweenSteps)
Once the number of retries has been exceeded, the delay is incremented by the step until the maximum delay has been reached. The delay will never exceed the capped delay.
initialDelayInMillis - the initial delay in milliseconds that should be used for the sleep timecappedDelayInMillis - the maximum delay in milliseconds that the time generator will returnstepMultiplier - the step multiplier that is applied to the current time delayretriesBetweenSteps - the number of retries that are attempted before the step multiplier is applied