Package org.kiwiproject.retry
Class SimpleRetryer
java.lang.Object
org.kiwiproject.retry.SimpleRetryer
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 than
SimpleRetries
directly.
You can construct a SimpleRetryer using the builder obtained via SimpleRetryer.builder().
| Name | Default | Description |
|---|---|---|
| environment | a DefaultEnvironment instance |
mainly useful for testing, e.g. to supply a mock |
| maxAttempts | DEFAULT_MAX_ATTEMPTS |
the maximum number of attempts to make before giving up |
| retryDelayTime | DEFAULT_RETRY_DELAY_TIME |
the time value to wait between attempts |
| retryDelayUnit | DEFAULT_RETRY_DELAY_UNIT |
the time unit for retryDelayTime |
| commonType | DEFAULT_TYPE |
use this to specify a common type/description that retryer will get (used in log messages) |
| logLevelForSubsequentAttempts | DEFAULT_RETRY_LOG_LEVEL |
the 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.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault maximum attempts.static final longDefault retry delay time.static final TimeUnitDefault retry delay time unit.static final org.slf4j.event.LevelDefault value of the log level to use when logging retry attempts.static final StringDefault value to include in attempt log messages. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()<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 Details
-
DEFAULT_MAX_ATTEMPTS
public static final int DEFAULT_MAX_ATTEMPTSDefault maximum attempts.- See Also:
-
DEFAULT_RETRY_DELAY_TIME
public static final long DEFAULT_RETRY_DELAY_TIMEDefault retry delay time. This is a static value, i.e. there is no fancy exponential or linear backoff.- See Also:
-
DEFAULT_RETRY_DELAY_UNIT
Default retry delay time unit. -
DEFAULT_TYPE
Default value to include in attempt log messages.- See Also:
-
DEFAULT_RETRY_LOG_LEVEL
public static final org.slf4j.event.Level DEFAULT_RETRY_LOG_LEVELDefault value of the log level to use when logging retry attempts.
-
-
Method Details
-
tryGetObject
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
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
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
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
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
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
-
builder
-