Class RetryPolicies
A collection of useful implementations of RetryPolicy.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classGiven pairs of number of retries and sleep time (n0, t0), (n1, t1), ..., the first n0 retries sleep t0 milliseconds on average, the following n1 retries sleep t1 milliseconds on average, and so on. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final org.slf4j.Loggerstatic final RetryPolicyKeep trying forever.static final RetryPolicyTry once, and fail by re-throwing the exception. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final RetryPolicyexponentialBackoffRetry(int maxRetries, long sleepTime, TimeUnit timeUnit) Keep trying a limited number of times, waiting a growing amount of time between attempts, and then fail by re-throwing the exception.static final RetryPolicyfailoverOnNetworkException(int maxFailovers) static final RetryPolicyfailoverOnNetworkException(RetryPolicy fallbackPolicy, int maxFailovers) static final RetryPolicyfailoverOnNetworkException(RetryPolicy fallbackPolicy, int maxFailovers, int maxRetries, long delayMillis, long maxDelayBase) static final RetryPolicyfailoverOnNetworkException(RetryPolicy fallbackPolicy, int maxFailovers, long delayMillis, long maxDelayBase) static final RetryPolicyretryByException(RetryPolicy defaultPolicy, Map<Class<? extends Exception>, RetryPolicy> exceptionToPolicyMap) Set a default policy with some explicit handlers for specific exceptions.static final RetryPolicyretryByRemoteException(RetryPolicy defaultPolicy, Map<Class<? extends Exception>, RetryPolicy> exceptionToPolicyMap) A retry policy for RemoteException Set a default policy with some explicit handlers for specific exceptions.static final RetryPolicyretryForeverWithFixedSleep(long sleepTime, TimeUnit timeUnit) Keep trying forever with a fixed time between attempts.static final RetryPolicyretryOtherThanRemoteAndSaslException(RetryPolicy defaultPolicy, Map<Class<? extends Exception>, RetryPolicy> exceptionToPolicyMap) A retry policy where RemoteException and SaslException are not retried, other individual exception types can have RetryPolicy overrides, and any other exception type without an override is not retried.static final RetryPolicyretryUpToMaximumCountWithFixedSleep(int maxRetries, long sleepTime, TimeUnit timeUnit) Keep trying a limited number of times, waiting a fixed time between attempts, and then fail by re-throwing the exception.static final RetryPolicyretryUpToMaximumCountWithProportionalSleep(int maxRetries, long sleepTime, TimeUnit timeUnit) Keep trying a limited number of times, waiting a growing amount of time between attempts, and then fail by re-throwing the exception.static final RetryPolicyretryUpToMaximumTimeWithFixedSleep(long maxTime, long sleepTime, TimeUnit timeUnit) Keep trying for a maximum time, waiting a fixed time between attempts, and then fail by re-throwing the exception.
-
Field Details
-
LOG
public static final org.slf4j.Logger LOG -
TRY_ONCE_THEN_FAIL
Try once, and fail by re-throwing the exception. This corresponds to having no retry mechanism in place.
-
RETRY_FOREVER
Keep trying forever.
-
-
Constructor Details
-
RetryPolicies
public RetryPolicies()
-
-
Method Details
-
retryForeverWithFixedSleep
Keep trying forever with a fixed time between attempts.
- Parameters:
sleepTime- sleepTime.timeUnit- timeUnit.- Returns:
- RetryPolicy.
-
retryUpToMaximumCountWithFixedSleep
public static final RetryPolicy retryUpToMaximumCountWithFixedSleep(int maxRetries, long sleepTime, TimeUnit timeUnit) Keep trying a limited number of times, waiting a fixed time between attempts, and then fail by re-throwing the exception.
- Parameters:
maxRetries- maxRetries.sleepTime- sleepTime.timeUnit- timeUnit.- Returns:
- RetryPolicy.
-
retryUpToMaximumTimeWithFixedSleep
public static final RetryPolicy retryUpToMaximumTimeWithFixedSleep(long maxTime, long sleepTime, TimeUnit timeUnit) Keep trying for a maximum time, waiting a fixed time between attempts, and then fail by re-throwing the exception.
- Parameters:
timeUnit- timeUnit.sleepTime- sleepTime.maxTime- maxTime.- Returns:
- RetryPolicy.
-
retryUpToMaximumCountWithProportionalSleep
public static final RetryPolicy retryUpToMaximumCountWithProportionalSleep(int maxRetries, long sleepTime, TimeUnit timeUnit) Keep trying a limited number of times, waiting a growing amount of time between attempts, and then fail by re-throwing the exception. The time between attempts is
sleepTimemutliplied by the number of tries so far.- Parameters:
sleepTime- sleepTime.maxRetries- maxRetries.timeUnit- timeUnit.- Returns:
- RetryPolicy.
-
exponentialBackoffRetry
public static final RetryPolicy exponentialBackoffRetry(int maxRetries, long sleepTime, TimeUnit timeUnit) Keep trying a limited number of times, waiting a growing amount of time between attempts, and then fail by re-throwing the exception. The time between attempts is
sleepTimemutliplied by a random number in the range of [0, 2 to the number of retries)- Parameters:
timeUnit- timeUnit.maxRetries- maxRetries.sleepTime- sleepTime.- Returns:
- RetryPolicy.
-
retryByException
public static final RetryPolicy retryByException(RetryPolicy defaultPolicy, Map<Class<? extends Exception>, RetryPolicy> exceptionToPolicyMap) Set a default policy with some explicit handlers for specific exceptions.
- Parameters:
exceptionToPolicyMap- exceptionToPolicyMap.defaultPolicy- defaultPolicy.- Returns:
- RetryPolicy.
-
retryByRemoteException
public static final RetryPolicy retryByRemoteException(RetryPolicy defaultPolicy, Map<Class<? extends Exception>, RetryPolicy> exceptionToPolicyMap) A retry policy for RemoteException Set a default policy with some explicit handlers for specific exceptions.
- Parameters:
defaultPolicy- defaultPolicy.exceptionToPolicyMap- exceptionToPolicyMap.- Returns:
- RetryPolicy.
-
retryOtherThanRemoteAndSaslException
public static final RetryPolicy retryOtherThanRemoteAndSaslException(RetryPolicy defaultPolicy, Map<Class<? extends Exception>, RetryPolicy> exceptionToPolicyMap) A retry policy where RemoteException and SaslException are not retried, other individual exception types can have RetryPolicy overrides, and any other exception type without an override is not retried.
- Parameters:
defaultPolicy- defaultPolicy.exceptionToPolicyMap- exceptionToPolicyMap.- Returns:
- RetryPolicy.
-
failoverOnNetworkException
-
failoverOnNetworkException
public static final RetryPolicy failoverOnNetworkException(RetryPolicy fallbackPolicy, int maxFailovers) -
failoverOnNetworkException
public static final RetryPolicy failoverOnNetworkException(RetryPolicy fallbackPolicy, int maxFailovers, long delayMillis, long maxDelayBase) -
failoverOnNetworkException
public static final RetryPolicy failoverOnNetworkException(RetryPolicy fallbackPolicy, int maxFailovers, int maxRetries, long delayMillis, long maxDelayBase)
-