Package org.springframework.retry.policy
Class SimpleRetryPolicy
java.lang.Object
org.springframework.retry.policy.SimpleRetryPolicy
- All Implemented Interfaces:
Serializable,RetryPolicy
- Direct Known Subclasses:
ExpressionRetryPolicy
Simple retry policy that retries a fixed number of times for a set of named exceptions
(and subclasses). The number of attempts includes the initial try, so e.g.
retryTemplate = new RetryTemplate(new SimpleRetryPolicy(3)); retryTemplate.execute(callback);will execute the callback at least once, and as many as 3 times.
Since version 1.3 it is not necessary to use this class. The same behaviour can be
achieved by constructing a CompositeRetryPolicy with
MaxAttemptsRetryPolicy and BinaryExceptionClassifierRetryPolicy inside,
that is actually performed by:
RetryTemplate.builder()
.maxAttempts(3)
.retryOn(Exception.class)
.build();
or by RetryTemplate.defaultInstance()- Author:
- Dave Syer, Rob Harrop, Gary Russell, Aleksandr Shamukov, Artem Bilan, Emanuele Ivaldi
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default limit to the number of attempts for a new policy.Fields inherited from interface org.springframework.retry.RetryPolicy
NO_MAXIMUM_ATTEMPTS_SET -
Constructor Summary
ConstructorsConstructorDescriptionCreate aSimpleRetryPolicywith the default number of retry attempts, retrying all exceptions.SimpleRetryPolicy(int maxAttempts) Create aSimpleRetryPolicywith the specified number of retry attempts, retrying all exceptions.SimpleRetryPolicy(int maxAttempts, Map<Class<? extends Throwable>, Boolean> retryableExceptions) Create aSimpleRetryPolicywith the specified number of retry attempts.SimpleRetryPolicy(int maxAttempts, Map<Class<? extends Throwable>, Boolean> retryableExceptions, boolean traverseCauses) Create aSimpleRetryPolicywith the specified number of retry attempts.SimpleRetryPolicy(int maxAttempts, Map<Class<? extends Throwable>, Boolean> retryableExceptions, boolean traverseCauses, boolean defaultValue) Create aSimpleRetryPolicywith the specified number of retry attempts.SimpleRetryPolicy(int maxAttempts, BinaryExceptionClassifier classifier) Create aSimpleRetryPolicywith the specified number of retry attempts and provided exception classifier. -
Method Summary
Modifier and TypeMethodDescriptionbooleancanRetry(RetryContext context) Test for retryable operation based on the status.voidclose(RetryContext status) intThe maximum number of attempts before failure.voidmaxAttemptsSupplier(Supplier<Integer> maxAttemptsSupplier) Set a supplier for the number of attempts before retries are exhausted.open(RetryContext parent) Get a status object that can be used to track the current operation according to this policy.voidregisterThrowable(RetryContext context, Throwable throwable) Update the status with another attempted retry and the latest exception.voidsetMaxAttempts(int maxAttempts) Set the number of attempts before retries are exhausted.voidsetNotRecoverable(Class<? extends Throwable>... noRecovery) Configure throwables that should not be passed to a recoverer (if present) but thrown immediately.toString()
-
Field Details
-
DEFAULT_MAX_ATTEMPTS
public static final int DEFAULT_MAX_ATTEMPTSThe default limit to the number of attempts for a new policy.- See Also:
-
-
Constructor Details
-
SimpleRetryPolicy
public SimpleRetryPolicy()Create aSimpleRetryPolicywith the default number of retry attempts, retrying all exceptions. -
SimpleRetryPolicy
public SimpleRetryPolicy(int maxAttempts) Create aSimpleRetryPolicywith the specified number of retry attempts, retrying all exceptions.- Parameters:
maxAttempts- the maximum number of attempts
-
SimpleRetryPolicy
public SimpleRetryPolicy(int maxAttempts, Map<Class<? extends Throwable>, Boolean> retryableExceptions) Create aSimpleRetryPolicywith the specified number of retry attempts.- Parameters:
maxAttempts- the maximum number of attemptsretryableExceptions- the map of exceptions that are retryable
-
SimpleRetryPolicy
public SimpleRetryPolicy(int maxAttempts, Map<Class<? extends Throwable>, Boolean> retryableExceptions, boolean traverseCauses) Create aSimpleRetryPolicywith the specified number of retry attempts. If traverseCauses is true, the exception causes will be traversed until a match or the root cause is found.- Parameters:
maxAttempts- the maximum number of attemptsretryableExceptions- the map of exceptions that are retryable based on the map value (true/false).traverseCauses- true to traverse the exception cause chain until a classified exception is found or the root cause is reached.
-
SimpleRetryPolicy
public SimpleRetryPolicy(int maxAttempts, Map<Class<? extends Throwable>, Boolean> retryableExceptions, boolean traverseCauses, boolean defaultValue) Create aSimpleRetryPolicywith the specified number of retry attempts. If traverseCauses is true, the exception causes will be traversed until a match or the root cause is found. The default value indicates whether to retry or not for exceptions (or super classes thereof) that are not found in the map.- Parameters:
maxAttempts- the maximum number of attemptsretryableExceptions- the map of exceptions that are retryable based on the map value (true/false).traverseCauses- true to traverse the exception cause chain until a classified exception is found or the root cause is reached.defaultValue- the default action.
-
SimpleRetryPolicy
Create aSimpleRetryPolicywith the specified number of retry attempts and provided exception classifier.- Parameters:
maxAttempts- the maximum number of attemptsclassifier- custom exception classifier
-
-
Method Details
-
setMaxAttempts
public void setMaxAttempts(int maxAttempts) Set the number of attempts before retries are exhausted. Includes the initial attempt before the retries begin so, generally, will be>= 1. For example setting this property to 3 means 3 attempts total (initial + 2 retries).- Parameters:
maxAttempts- the maximum number of attempts including the initial attempt.
-
setNotRecoverable
Configure throwables that should not be passed to a recoverer (if present) but thrown immediately.- Parameters:
noRecovery- the throwables.- Since:
- 3.0
-
maxAttemptsSupplier
Set a supplier for the number of attempts before retries are exhausted. Includes the initial attempt before the retries begin so, generally, will be>= 1. For example setting this property to 3 means 3 attempts total (initial + 2 retries). IMPORTANT: This policy cannot be serialized when a max attempts supplier is provided. Serialization might be used by a distributed cache when using this policy in aCircuitBreakercontext.- Parameters:
maxAttemptsSupplier- the maximum number of attempts including the initial attempt.- Since:
- 2.0
-
getMaxAttempts
public int getMaxAttempts()The maximum number of attempts before failure.- Specified by:
getMaxAttemptsin interfaceRetryPolicy- Returns:
- the maximum number of attempts
-
canRetry
Test for retryable operation based on the status.- Specified by:
canRetryin interfaceRetryPolicy- Parameters:
context- the current retry status- Returns:
- true if the last exception was retryable and the number of attempts so far is less than the limit.
- See Also:
-
close
- Specified by:
closein interfaceRetryPolicy- Parameters:
status- a retry status created by theRetryPolicy.open(RetryContext)method of this policy.- See Also:
-
registerThrowable
Update the status with another attempted retry and the latest exception.- Specified by:
registerThrowablein interfaceRetryPolicy- Parameters:
context- the current status object.throwable- the exception to throw- See Also:
-
open
Get a status object that can be used to track the current operation according to this policy. Has to be aware of the latest exception and the number of attempts.- Specified by:
openin interfaceRetryPolicy- Parameters:
parent- the parent context if we are in a nested retry.- Returns:
- a
RetryContextobject specific to this policy. - See Also:
-
toString
-