Package org.springframework.retry.policy
Class MaxAttemptsRetryPolicy
java.lang.Object
org.springframework.retry.policy.MaxAttemptsRetryPolicy
- All Implemented Interfaces:
Serializable,RetryPolicy
Simple retry policy that is aware only about attempt count and retries a fixed number
of times. The number of attempts includes the initial try.
It is not recommended to use it directly, because usually exception classification is strongly recommended (to not retry on OutOfMemoryError, for example).
For daily usage see RetryTemplate.builder()
Volatility of maxAttempts allows concurrent modification and does not require safe publication of new instance after construction.
- 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 aMaxAttemptsRetryPolicywith the default number of retry attempts (3), retrying all throwables.MaxAttemptsRetryPolicy(int maxAttempts) Create aMaxAttemptsRetryPolicywith the specified number of retry attempts, retrying all throwables. -
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.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.
-
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
-
MaxAttemptsRetryPolicy
public MaxAttemptsRetryPolicy()Create aMaxAttemptsRetryPolicywith the default number of retry attempts (3), retrying all throwables. -
MaxAttemptsRetryPolicy
public MaxAttemptsRetryPolicy(int maxAttempts) Create aMaxAttemptsRetryPolicywith the specified number of retry attempts, retrying all throwables.- Parameters:
maxAttempts- the maximum number of attempts
-
-
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.
-
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.
-
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:
-