Package org.kiwiproject.retry
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 theRetryExceptionandExecutionExceptionthat are generated from theRetryer.call(Callable)method are converted toKiwiRetryerException.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 Throwables that should cause KiwiRetryer to retry its specifiedCallableif encountered during processing. Note these are GuavaPredicateobjects not JDKPredicateobjects; the reason is that the underlying guava-retrying library uses Guava's predicate class.resultPredicates empty list Defines the Tobjects that should cause KiwiRetryer to retry its specifiedCallableif returned by theCallableduring processing. Note these are GuavaPredicateobjects not JDKPredicateobjects; the reason is that the underlying guava-retrying library uses Guava's predicate class.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.NOTE: The guava-retrying 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)
-
-