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 executed once and potentially 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 givenRetryableoperation according to theRetryPolicyconfigured at the implementation level.Return the currentRetryListenerthat is in use with this template.Return the currentRetryPolicythat is in use with this template.voidInvoke the givenRunnableaccording to theRetryPolicy, returning successfully or throwing the lastRunnableexception to the caller in case of retry policy exhaustion.Invoke the givenSupplieraccording to theRetryPolicy, returning a successful result or throwing the lastSupplierexception to the caller in case of retry policy exhaustion.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
Description copied from interface:RetryOperationsExecute the givenRetryableoperation according to theRetryPolicyconfigured at the implementation level.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 successful result of the
Retryable, if any - Throws:
RetryException- if theRetryPolicyis exhausted. Note that this exception represents a failure outcome and is not meant to be propagated; you will typically rather rethrow its cause (the last original exception thrown by theRetryablecallback) or throw a custom business exception instead.
-
invoke
Description copied from interface:RetryOperationsInvoke the givenSupplieraccording to theRetryPolicy, returning a successful result or throwing the lastSupplierexception to the caller in case of retry policy exhaustion.- Specified by:
invokein interfaceRetryOperations- Type Parameters:
R- the type of the result- Parameters:
retryable- theSupplierto invoke and retry if needed- Returns:
- the result of the
Supplier
-
invoke
Description copied from interface:RetryOperationsInvoke the givenRunnableaccording to theRetryPolicy, returning successfully or throwing the lastRunnableexception to the caller in case of retry policy exhaustion.- Specified by:
invokein interfaceRetryOperations- Parameters:
retryable- theRunnableto invoke and retry if needed
-