Package org.kiwiproject.retry
Class SimpleRetries
- java.lang.Object
-
- org.kiwiproject.retry.SimpleRetries
-
public class SimpleRetries extends Object
Static utilities for retrying an operation. TheSupplierpassed to each method must indicate success or failure. Success is indicated by returning a non-null value. Failure is indicated by returningnullor by throwing an exception. Each time a failure occurs, the code sleeps for the specified delay time, and then another attempt will be made unless the maximum number of attempts has been reached.While you can use this directly, consider using
SimpleRetryer, which is more flexible because (1) you can easily mock it in tests, and (2) it accepts common configuration options and makes the method calls simpler because there are many fewer arguments.
-
-
Constructor Summary
Constructors Constructor Description SimpleRetries()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Optional<T>tryGetObject(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, Class<T> type, Supplier<T> supplier)Try to get an object, making up tomaxAttemptsattempts.static <T> Optional<T>tryGetObject(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, Supplier<T> supplier)Try to get an object, making up tomaxAttemptsattempts.static <T> Optional<T>tryGetObject(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, KiwiEnvironment environment, Class<T> type, Supplier<T> supplier)Try to get an object, making up tomaxAttemptsattempts.static <T> Optional<T>tryGetObject(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, KiwiEnvironment environment, String type, org.slf4j.event.Level level, Supplier<T> supplier)Try to get an object, making up tomaxAttemptsattempts.static <T> Optional<T>tryGetObject(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, KiwiEnvironment environment, Supplier<T> supplier)Try to get an object, making up tomaxAttemptsattempts.static <T> RetryResult<T>tryGetObjectCollectingErrors(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, Class<T> type, Supplier<T> supplier)Try to get an object, making up tomaxAttemptsattempts.static <T> RetryResult<T>tryGetObjectCollectingErrors(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, Supplier<T> supplier)Try to get an object, making up tomaxAttemptsattempts.static <T> RetryResult<T>tryGetObjectCollectingErrors(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, KiwiEnvironment environment, Class<T> type, Supplier<T> supplier)Try to get an object, making up tomaxAttemptsattempts.static <T> RetryResult<T>tryGetObjectCollectingErrors(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, KiwiEnvironment environment, String type, Supplier<T> supplier)Try to get an object, making up tomaxAttemptsattempts.static <T> RetryResult<T>tryGetObjectCollectingErrors(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, KiwiEnvironment environment, String type, org.slf4j.event.Level level, Supplier<T> supplier)Try to get an object, making up tomaxAttemptsattempts.
-
-
-
Method Detail
-
tryGetObject
public static <T> Optional<T> tryGetObject(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, Supplier<T> supplier)
Try to get an object, making up tomaxAttemptsattempts. Logs first attempt and retries at TRACE level.- Type Parameters:
T- the type of object- Parameters:
maxAttempts- the maximum number of attempts to make before giving upretryDelay- constant delay time between attemptsretryDelayUnit- delay time unit between attemptssupplier- on success return the object; returnnullor throw exception if attempt failed- Returns:
- an Optional which either contains a value, or is empty if all attempts failed
-
tryGetObject
public static <T> Optional<T> tryGetObject(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, KiwiEnvironment environment, Supplier<T> supplier)
Try to get an object, making up tomaxAttemptsattempts. Logs first attempt and retries at TRACE level using "object" as the description.- Type Parameters:
T- the type of object- Parameters:
maxAttempts- the maximum number of attempts to make before giving upretryDelay- constant delay time between attemptsretryDelayUnit- delay time unit between attemptsenvironment- theKiwiEnvironmentto use when sleeping between attemptssupplier- on success return the object; returnnullor throw exception if attempt failed- Returns:
- an Optional which either contains a value, or is empty if all attempts failed
-
tryGetObject
public static <T> Optional<T> tryGetObject(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, Class<T> type, Supplier<T> supplier)
Try to get an object, making up tomaxAttemptsattempts. Logs first attempt and retries at TRACE level using the giventypeas the description.- Type Parameters:
T- the type of object- Parameters:
maxAttempts- the maximum number of attempts to make before giving upretryDelay- constant delay time between attemptsretryDelayUnit- delay time unit between attemptstype- the type of object we are attempting to return, used when logging attemptssupplier- on success return the object; returnnullor throw exception if attempt failed- Returns:
- an Optional which either contains a value, or is empty if all attempts failed
-
tryGetObject
public static <T> Optional<T> tryGetObject(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, KiwiEnvironment environment, Class<T> type, Supplier<T> supplier)
Try to get an object, making up tomaxAttemptsattempts. Logs first attempt and retries at TRACE level, using the giventypeas the description.- Type Parameters:
T- the type of object- Parameters:
maxAttempts- the maximum number of attempts to make before giving upretryDelay- constant delay time between attemptsretryDelayUnit- delay time unit between attemptsenvironment- theKiwiEnvironmentto use when sleeping between attemptstype- the type of object we are attempting to return, used when logging attemptssupplier- on success return the object; returnnullor throw exception if attempt failed- Returns:
- an Optional which either contains a value, or is empty if all attempts failed
-
tryGetObject
public static <T> Optional<T> tryGetObject(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, KiwiEnvironment environment, String type, org.slf4j.event.Level level, Supplier<T> supplier)
Try to get an object, making up tomaxAttemptsattempts. Logs first attempt at TRACE level, and logs retries at the givenlevel, always usingtypeas the description.- Type Parameters:
T- the type of object- Parameters:
maxAttempts- the maximum number of attempts to make before giving upretryDelay- constant delay time between attemptsretryDelayUnit- delay time unit between attemptsenvironment- theKiwiEnvironmentto use when sleeping between attemptstype- the type of object we are attempting to return, used when logging attemptslevel- the SLF4J logLevelat which to log retriessupplier- on success return the object; returnnullor throw exception if attempt failed- Returns:
- an Optional which either contains a value, or is empty if all attempts failed
-
tryGetObjectCollectingErrors
public static <T> RetryResult<T> tryGetObjectCollectingErrors(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, Supplier<T> supplier)
Try to get an object, making up tomaxAttemptsattempts. Logs first attempt and retries at TRACE level using "object" as the description.- Type Parameters:
T- the type of object- Parameters:
maxAttempts- the maximum number of attempts to make before giving upretryDelay- constant delay time between attemptsretryDelayUnit- delay time unit between attemptssupplier- on success return the object; returnnullor throw exception if attempt failed- Returns:
- a
RetryResult
-
tryGetObjectCollectingErrors
public static <T> RetryResult<T> tryGetObjectCollectingErrors(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, Class<T> type, Supplier<T> supplier)
Try to get an object, making up tomaxAttemptsattempts. Logs first attempt and retries at TRACE level using the giventypeas the description.- Type Parameters:
T- the type of object- Parameters:
maxAttempts- the maximum number of attempts to make before giving upretryDelay- constant delay time between attemptsretryDelayUnit- delay time unit between attemptstype- the type of object we are attempting to return, used when logging attemptssupplier- on success return the object; returnnullor throw exception if attempt failed- Returns:
- a
RetryResult
-
tryGetObjectCollectingErrors
public static <T> RetryResult<T> tryGetObjectCollectingErrors(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, KiwiEnvironment environment, Class<T> type, Supplier<T> supplier)
Try to get an object, making up tomaxAttemptsattempts. Logs first attempt and retries at TRACE level using the giventypeas the description.- Type Parameters:
T- the type of object- Parameters:
maxAttempts- the maximum number of attempts to make before giving upretryDelay- constant delay time between attemptsretryDelayUnit- delay time unit between attemptsenvironment- theKiwiEnvironmentto use when sleeping between attemptstype- the type of object we are attempting to return, used when logging attemptssupplier- on success return the object; returnnullor throw exception if attempt failed- Returns:
- a
RetryResult
-
tryGetObjectCollectingErrors
public static <T> RetryResult<T> tryGetObjectCollectingErrors(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, KiwiEnvironment environment, String type, Supplier<T> supplier)
Try to get an object, making up tomaxAttemptsattempts. Logs first attempt and retries at TRACE level using the giventypeas the description.- Type Parameters:
T- the type of object- Parameters:
maxAttempts- the maximum number of attempts to make before giving upretryDelay- constant delay time between attemptsretryDelayUnit- delay time unit between attemptsenvironment- theKiwiEnvironmentto use when sleeping between attemptstype- the type of object we are attempting to return, used when logging attemptssupplier- on success return the object; returnnullor throw exception if attempt failed- Returns:
- a
RetryResult
-
tryGetObjectCollectingErrors
public static <T> RetryResult<T> tryGetObjectCollectingErrors(int maxAttempts, long retryDelay, TimeUnit retryDelayUnit, KiwiEnvironment environment, String type, org.slf4j.event.Level level, Supplier<T> supplier)
Try to get an object, making up tomaxAttemptsattempts. Logs first attempt at TRACE level and logs retries at the givenlevel, always usingtypeas the description.- Type Parameters:
T- the type of object- Parameters:
maxAttempts- the maximum number of attempts to make before giving upretryDelay- constant delay time between attemptsretryDelayUnit- delay time unit between attemptsenvironment- theKiwiEnvironmentto use when sleeping between attemptstype- the type of object we are attempting to return, used when logging attemptslevel- the SLF4J logLevelat which to log retriessupplier- on success return the object; returnnullor throw exception if attempt failed- Returns:
- a
RetryResult
-
-