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
Modifier and TypeFieldDescriptionstatic final int
The 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
ConstructorDescriptionCreate aMaxAttemptsRetryPolicy
with the default number of retry attempts (3), retrying all throwables.MaxAttemptsRetryPolicy
(int maxAttempts) Create aMaxAttemptsRetryPolicy
with the specified number of retry attempts, retrying all throwables. -
Method Summary
Modifier and TypeMethodDescriptionboolean
canRetry
(RetryContext context) Test for retryable operation based on the status.void
close
(RetryContext status) int
The 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.void
registerThrowable
(RetryContext context, Throwable throwable) Update the status with another attempted retry and the latest exception.void
setMaxAttempts
(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 aMaxAttemptsRetryPolicy
with the default number of retry attempts (3), retrying all throwables. -
MaxAttemptsRetryPolicy
public MaxAttemptsRetryPolicy(int maxAttempts) Create aMaxAttemptsRetryPolicy
with 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:
getMaxAttempts
in interfaceRetryPolicy
- Returns:
- the maximum number of attempts
-
canRetry
Test for retryable operation based on the status.- Specified by:
canRetry
in 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:
close
in 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:
registerThrowable
in 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:
open
in interfaceRetryPolicy
- Parameters:
parent
- the parent context if we are in a nested retry.- Returns:
- a
RetryContext
object specific to this policy. - See Also:
-