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 SummaryConstructorsConstructorDescriptionCreate a newRetryTemplatewith maximum 3 retry attempts and a fixed backoff of 1 second.RetryTemplate(RetryPolicy retryPolicy) Create a newRetryTemplatewith the suppliedRetryPolicy.
- 
Method SummaryModifier 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- 
RetryTemplatepublic RetryTemplate()Create a newRetryTemplatewith maximum 3 retry attempts and a fixed backoff of 1 second.- See Also:
 
- 
RetryTemplateCreate a newRetryTemplatewith the suppliedRetryPolicy.- Parameters:
- retryPolicy- the retry policy to use
 
 
- 
- 
Method Details- 
setRetryPolicySet theRetryPolicyto use.Defaults to RetryPolicy.withDefaults().- Parameters:
- retryPolicy- the retry policy to use
- See Also:
 
- 
getRetryPolicyReturn the currentRetryPolicythat is in use with this template.
- 
setRetryListenerSet theRetryListenerto use.If multiple listeners are needed, use a CompositeRetryListener.Defaults to a no-op implementation. - Parameters:
- retryListener- the retry listener to use
 
- 
getRetryListenerReturn the currentRetryListenerthat is in use with this template.
- 
executeExecute 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 interface- RetryOperations
- Type Parameters:
- R- the type of the result
- Parameters:
- retryable- the- Retryableto execute and retry if needed
- Returns:
- the result of the Retryable, if any
- Throws:
- RetryException- if the- RetryPolicyis exhausted
 
 
-