Class RetryTemplate
- All Implemented Interfaces:
RetryOperations
RetryOperations that executes and potentially
retries a Retryable operation based on a configured RetryPolicy.
By default, a retryable operation will be retried at most 3 times with a fixed backoff of 1 second.
A RetryListener can be registered to react to events published during key retry phases (before a
retry attempt, after a retry attempt, etc.).
All retry actions performed by this template are logged at debug level, using
"org.springframework.core.retry.RetryTemplate" as the log category.
- Since:
- 7.0
- Author:
- Mahmoud Ben Hassine, Sam Brannen, Juergen Hoeller
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a newRetryTemplatewith maximum 3 retry attempts and a fixed backoff of 1 second.RetryTemplate(RetryPolicy retryPolicy) Create a newRetryTemplatewith the suppliedRetryPolicy. -
Method Summary
Modifier and TypeMethodDescriptionExecute the suppliedRetryableoperation according to the configuredRetryPolicy.Return the currentRetryListenerthat is in use with this template.Return the currentRetryPolicythat is in use with this template.voidsetRetryListener(RetryListener retryListener) Set theRetryListenerto use.voidsetRetryPolicy(RetryPolicy retryPolicy) Set theRetryPolicyto use.
-
Constructor Details
-
RetryTemplate
public RetryTemplate()Create a newRetryTemplatewith maximum 3 retry attempts and a fixed backoff of 1 second.- See Also:
-
RetryTemplate
Create a newRetryTemplatewith the suppliedRetryPolicy.- Parameters:
retryPolicy- the retry policy to use
-
-
Method Details
-
setRetryPolicy
Set theRetryPolicyto use.Defaults to
RetryPolicy.withDefaults().- Parameters:
retryPolicy- the retry policy to use- See Also:
-
getRetryPolicy
Return the currentRetryPolicythat is in use with this template. -
setRetryListener
Set theRetryListenerto use.If multiple listeners are needed, use a
CompositeRetryListener.Defaults to a no-op implementation.
- Parameters:
retryListener- the retry listener to use
-
getRetryListener
Return the currentRetryListenerthat is in use with this template. -
execute
Execute the suppliedRetryableoperation according to the configuredRetryPolicy.If the
Retryablesucceeds, its result will be returned. Otherwise, aRetryExceptionwill be thrown to the caller. TheRetryExceptionwill contain the last exception thrown by theRetryableoperation as the cause and any exceptions from previous attempts as suppressed exceptions.- Specified by:
executein interfaceRetryOperations- Type Parameters:
R- the type of the result- Parameters:
retryable- theRetryableto execute and retry if needed- Returns:
- the result of the
Retryable, if any - Throws:
RetryException- if theRetryPolicyis exhausted
-