Class KiwiRetryer<T>
- java.lang.Object
-
- org.kiwiproject.retry.KiwiRetryer<T>
-
public class KiwiRetryer<T> extends Object
This is a wrapper class forRetryer; it wraps methods so that theRetryExceptionandInterruptedExceptionthat are generated from theRetryer.call(Callable)method are converted toKiwiRetryerException. The Background Information section at the bottom gives some history on why this is even here in the first place. You might consider usingRetryerdirectly now that it is within org.kiwiproject as the standalone retrying-again library.It also provides some kiwi-flavored default values, an identifier that can be used to distinguish between retryer instances in logs, logging of retry attempts, and some factories for creating retryer instances with common behavior.
You can construct a
KiwiRetryerusing the builder obtained viaKiwiRetryer.builder().Available configuration options for KiwiRetryer: Name Default Description retryerId UUID generated by UUIDs.randomUUIDString()The identifier for the retryer initialSleepTimeAmount 100 The initial sleep amount for the default incrementing wait strategy. This value will be ignored if an explicit WaitStrategyis defined.initialSleepTimeUnit TimeUnit.MILLISECONDSThe initial sleep TimeUnitfor the default incrementing wait strategy. This value will be ignored if an explicitWaitStrategyis defined.retryIncrementTimeAmount 200 The subsequent retry increment amount for the default incrementing wait strategy. This value will be ignored if an explicit WaitStrategyis defined.retryIncrementTimeUnit TimeUnit.MILLISECONDSThe subsequent retry increment TimeUnitfor the default incrementing wait strategy. This value will be ignored if an explicitWaitStrategyis defined.maxAttempts 5 The maximum number of attempts to use for the default stop strategy. This value will be ignored if an explicit StopStrategyis defined.processingLogLevel Level.DEBUGThis log level controls the "happy path" messages that are logged by this retryer. exceptionLogLevel Level.WARNThis log level controls the "sad path" messages (i.e. exceptions) that are logged by this retryer. retryOnAllExceptions false Tells the retryer to retry on any exception. NOTE: This supersedes any exceptions added to the exceptionPredicateslist or ifretryOnAllRuntimeExceptionsis set totrueretryOnAllRuntimeExceptions false Tells the retryer to retry on all RuntimeExceptions. NOTE: This supersedes any exceptions added to theexceptionPredicateslist.exceptionPredicates empty list Defines the Exceptions that should cause KiwiRetryer to retry its specifiedCallableif encountered during processing.resultPredicates empty list Defines the Tobjects that should cause KiwiRetryer to retry its specifiedCallableif returned by theCallableduring processing.stopStrategy null An explicit stopStrategywhich will override the default stop after attempts stop strategy.waitStrategy null An explicit waitStrategywhich will override the default incrementing wait strategy.Background Information:
Originally this class was created to wrap the (now defunct) guava-retrying library and add various defaults and conveniences, as well as an easier way to handle RetryException and its causes. The guava-retrying library stopped being maintained circa 2016, and was forked into re-retrying, which then stopped active development circa 2018. So, in late 2020 we forked re-retrying as retrying-again in order to keep it up to date at a minimum, and hopefully add some additional value where it makes sense. It is possible we might simply move some of this functionality into retrying-again and then deprecate and remove this functionality from kiwi. That would also allow us to use kiwi from retrying-again, which we currently cannot do without introducing a circular dependency.
NOTE: The org.kiwiproject:retrying-again library must be available at runtime.
-
-
Constructor Summary
Constructors Constructor Description KiwiRetryer()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Tcall(String retryerId, Callable<T> callable)Invoke the retryer with the given ID andCallable.Tcall(Callable<T> callable)Invoke the retryer with the givenCallable.static <T> KiwiRetryer<T>newRetryerRetryingAllExceptions(String retryerId)Create a new instance that will retry on all exceptions.static <T> KiwiRetryer<T>newRetryerRetryingAllRuntimeExceptions(String retryerId)Create a new instance that will retry on all runtime exceptions.static <T> KiwiRetryer<T>newRetryerWithDefaultExceptions(String retryerId)Create a new instance with several common network-related exception predicates.static <T> KiwiRetryer<T>newRetryerWithDefaults()Create a new instance with only the default values.
-
-
-
Method Detail
-
newRetryerWithDefaults
public static <T> KiwiRetryer<T> newRetryerWithDefaults()
Create a new instance with only the default values.- Type Parameters:
T- type of object the retryer returns- Returns:
- a KiwiRetryer for type
T
-
newRetryerWithDefaultExceptions
public static <T> KiwiRetryer<T> newRetryerWithDefaultExceptions(String retryerId)
Create a new instance with several common network-related exception predicates.- Type Parameters:
T- type of object the retryer returns- Parameters:
retryerId- the retryer ID- Returns:
- a KiwiRetryer for type
T - See Also:
KiwiRetryerPredicates.CONNECTION_ERROR,KiwiRetryerPredicates.NO_ROUTE_TO_HOST,KiwiRetryerPredicates.SOCKET_TIMEOUT,KiwiRetryerPredicates.UNKNOWN_HOST
-
newRetryerRetryingAllExceptions
public static <T> KiwiRetryer<T> newRetryerRetryingAllExceptions(String retryerId)
Create a new instance that will retry on all exceptions.- Type Parameters:
T- type of object the retryer returns- Parameters:
retryerId- the retryer ID- Returns:
- a KiwiRetryer for type
T
-
newRetryerRetryingAllRuntimeExceptions
public static <T> KiwiRetryer<T> newRetryerRetryingAllRuntimeExceptions(String retryerId)
Create a new instance that will retry on all runtime exceptions.- Type Parameters:
T- type of object the retryer returns- Parameters:
retryerId- the retryer ID- Returns:
- a KiwiRetryer for type
T
-
call
public T call(Callable<T> callable)
Invoke the retryer with the givenCallable.- Parameters:
callable- the code that attempts to produce a result- Returns:
- the result of the
Callable - Throws:
KiwiRetryerException- if there was an unhandled exception during processing, or if the maximum number of attempts was reached without success. For further information about the cause, you can unwrap the exception.- See Also:
KiwiRetryerException.unwrapKiwiRetryerException(KiwiRetryerException),KiwiRetryerException.unwrapKiwiRetryerExceptionFully(KiwiRetryerException)
-
call
public T call(String retryerId, Callable<T> callable)
Invoke the retryer with the given ID andCallable.This method allows you to use different IDs with the same
KiwiRetryerinstance, for example if the same retryer is called in separate threads it will be useful to be able to distinguish between them in logs.- Parameters:
retryerId- the ID for this retryer call (overrides theretryerIdof this instance)callable- the code that attempts to produce a result- Returns:
- the result of the
Callable - Throws:
KiwiRetryerException- if there was an unhandled exception during processing, or if the maximum number of attempts was reached without success. For further information about the cause, you can unwrap the exception.- See Also:
KiwiRetryerException.unwrapKiwiRetryerException(KiwiRetryerException),KiwiRetryerException.unwrapKiwiRetryerExceptionFully(KiwiRetryerException)
-
-