Package org.kiwiproject.retry
Class SimpleRetryer
- java.lang.Object
-
- org.kiwiproject.retry.SimpleRetryer
-
public class SimpleRetryer extends Object
A simple class to retry an operation up to a maximum number of attempts. Uses the same set of initial values for maximum number of attempts, delay between attempts, etc. Consider using this rather thanSimpleRetriesdirectly.You can construct a
SimpleRetryerusing the builder obtained viaSimpleRetryer.builder().Available configuration options for SimpleRetryer: Name Default Description environment a DefaultEnvironmentinstancemainly useful for testing, e.g. to supply a mock maxAttempts DEFAULT_MAX_ATTEMPTSthe maximum number of attempts to make before giving up retryDelayTime DEFAULT_RETRY_DELAY_TIMEthe time value to wait between attempts retryDelayUnit DEFAULT_RETRY_DELAY_UNITthe time unit for retryDelayTimecommonType DEFAULT_TYPEuse this to specify a common type/description that retryer will get (used in log messages) logLevelForSubsequentAttempts Level.TRACEthe log level at which retries will be logged (the first attempt is always logged at TRACE) - Implementation Note:
- This is basically an instance wrapper around
SimpleRetriesto allow for specification of common configuration, and thus avoid calling methods with many parameters. It also facilitates easy mocking in tests as opposed to the static methods inSimpleRetries.
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_MAX_ATTEMPTSDefault maximum attempts.static longDEFAULT_RETRY_DELAY_TIMEDefault retry delay time.static TimeUnitDEFAULT_RETRY_DELAY_UNITDefault retry delay time unit.static StringDEFAULT_TYPEDefault value to include in attempt log messages.
-
Constructor Summary
Constructors Constructor Description SimpleRetryer()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> Optional<T>tryGetObject(Class<T> type, Supplier<T> supplier)Try to get an object.<T> Optional<T>tryGetObject(String type, Supplier<T> supplier)Try to get an object.<T> Optional<T>tryGetObject(Supplier<T> supplier)Try to get an object.<T> RetryResult<T>tryGetObjectCollectingErrors(Class<T> type, Supplier<T> supplier)Try to get an object.<T> RetryResult<T>tryGetObjectCollectingErrors(String type, Supplier<T> supplier)Try to get an object.<T> RetryResult<T>tryGetObjectCollectingErrors(Supplier<T> supplier)Try to get an object.
-
-
-
Field Detail
-
DEFAULT_MAX_ATTEMPTS
public static final int DEFAULT_MAX_ATTEMPTS
Default maximum attempts.- See Also:
- Constant Field Values
-
DEFAULT_RETRY_DELAY_TIME
public static final long DEFAULT_RETRY_DELAY_TIME
Default retry delay time. This is a static value, i.e. there is no fancy exponential or linear backoff.- See Also:
- Constant Field Values
-
DEFAULT_RETRY_DELAY_UNIT
public static final TimeUnit DEFAULT_RETRY_DELAY_UNIT
Default retry delay time unit.
-
DEFAULT_TYPE
public static final String DEFAULT_TYPE
Default value to include in attempt log messages.- See Also:
- Constant Field Values
-
-
Method Detail
-
tryGetObject
public <T> Optional<T> tryGetObject(Supplier<T> supplier)
Try to get an object.- Type Parameters:
T- the type of object- Parameters:
supplier- 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 <T> Optional<T> tryGetObject(Class<T> type, Supplier<T> supplier)
Try to get an object.- Type Parameters:
T- the type of object- Parameters:
type- the type of object to return, used only in logging messagessupplier- 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 <T> Optional<T> tryGetObject(String type, Supplier<T> supplier)
Try to get an object.- Type Parameters:
T- the type of object- Parameters:
type- the type of object to return, used only in logging messagessupplier- 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 <T> RetryResult<T> tryGetObjectCollectingErrors(Supplier<T> supplier)
Try to get an object.- Type Parameters:
T- the type of object- Parameters:
supplier- on success return the object; returnnullor throw exception if attempt failed- Returns:
- a
RetryResult
-
tryGetObjectCollectingErrors
public <T> RetryResult<T> tryGetObjectCollectingErrors(Class<T> type, Supplier<T> supplier)
Try to get an object.- Type Parameters:
T- the type of object- Parameters:
type- the type of object to return, used only in logging messagessupplier- on success return the object; returnnullor throw exception if attempt failed- Returns:
- a
RetryResult
-
tryGetObjectCollectingErrors
public <T> RetryResult<T> tryGetObjectCollectingErrors(String type, Supplier<T> supplier)
Try to get an object.- Type Parameters:
T- the type of object- Parameters:
type- the type of object to return, used only in logging messagessupplier- on success return the object; returnnullor throw exception if attempt failed- Returns:
- a
RetryResult
-
-