Class RetryTemplate
java.lang.Object
org.springframework.core.retry.RetryTemplate
- All Implemented Interfaces:
RetryOperations
A basic implementation of
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 TypeMethodDescription<R> @Nullable RExecute the suppliedRetryableaccording to the configured retry and backoff policies.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:
-
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
-
execute
Execute the suppliedRetryableaccording to the configured retry and backoff policies.If the
Retryablesucceeds, its result will be returned. Otherwise, aRetryExceptionwill be thrown to the caller.- 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; exceptions encountered during retry attempts are available as suppressed exceptions
-