To use Spring’s RetryTemplate, first identify which of the two APIs your project uses: Spring Framework’s core org.springframework.core.retry.RetryTemplate or the separate Spring Retry library’s org.springframework.retry.support.RetryTemplate. Their packages, callback types, and configuration differ, so keep examples for each API separate. Both run an operation again after selected failures, optionally waiting between attempts, then return its result or report exhaustion.
Choose the RetryTemplate API your project uses
Spring Framework and the separate Spring Retry library provide different classes with the same simple name. Check your dependency and imports before copying code.
| What to check | Spring Framework core | Spring Retry |
|---|---|---|
| Class package | org.springframework.core.retry.RetryTemplate (Spring Framework API documentation: RetryTemplate) |
org.springframework.retry.support.RetryTemplate (Spring Retry 2.0.13 reference: Spring Retry reference) |
| Operation callback | Retryable |
RetryCallback |
| Configuration approach | RetryPolicy, including its builder |
RetryTemplate.builder() or other Spring Retry configuration |
| Recovery and stateful options | Core API has its own execution and listener facilities; do not assume Spring Retry overloads apply. | Documents a RecoveryCallback overload and stateful overloads using RetryState. |
The examples below are intentionally divided by API family. Do not combine one family’s imports or callback syntax with the other’s configuration.
Use Spring Framework’s core RetryTemplate
For a simple operation, create the core template and pass it a retryable operation:
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstall#1 Best Overall
import org.springframework.core.retry.RetryTemplate;
var retryTemplate = new RetryTemplate();
String result = retryTemplate.execute(() -> client.call());
The no-argument core template allows three retries after the initial invocation, with a fixed one-second delay between attempts. That means the operation can run up to four times if every invocation fails in a retryable way. Spring’s 2025 guidance states the three-retry default; the current API documentation describes the same retry limit and one-second fixed backoff (Spring resilience reference; RetryTemplate API documentation).
For production calls, configure the failures and waiting behavior rather than relying on defaults:
Rank #2
import java.time.Duration;
import org.springframework.core.retry.RetryPolicy;
import org.springframework.core.retry.RetryTemplate;
var policy = RetryPolicy.builder()
.includes(TransientClientException.class)
.maxRetries(4)
.delay(Duration.ofMillis(200))
.multiplier(2)
.maxDelay(Duration.ofSeconds(5))
.build();
var retryTemplate = new RetryTemplate(policy);
var value = retryTemplate.execute(() -> client.call());
Here, maxRetries(4) means up to four retries in addition to the first call, for a maximum of five invocations. The core policy builder also provides excludes() and predicate() to make the retry decision more selective, and supports a timeout bound on total elapsed time, including waits. See the Spring Framework retry reference and RetryPolicy.Builder API for the current API details.
Use Spring Retry’s separate library API
If your project uses the separate Spring Retry library, its package and callback syntax are different. This example uses the Spring Retry 2.0.13 API:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
import org.springframework.retry.support.RetryTemplate;
import org.springframework.retry.RetryCallback;
RetryTemplate template = RetryTemplate.builder()
.maxAttempts(5)
.exponentialBackoff(100, 2.0, 5000)
.retryOn(TransientClientException.class)
.build();
String value = template.execute(
context -> client.call(),
context -> fallbackValue());
In Spring Retry’s builder, maxAttempts(5) includes the initial call: the callback can run at most five times. The optional recovery callback provides a fallback when retries are exhausted. If no recovery callback is supplied, Spring Retry rethrows the most recent failure. Spring Retry also documents stateful execution overloads using RetryState; consult its reference documentation for those cases.
Choose which failures to retry
Retry only exceptions that represent failures likely to clear on another attempt, such as a transient remote-service or network problem. Use the selected API’s policy or builder to include those exception types and exclude permanent errors. Validation failures, authorization errors, and malformed requests ordinarily need to reach the caller without another attempt because repeating the same request will not fix them.
Be deliberate about the operation inside the callback. A retry may invoke it more than once, so a write, payment, message send, or other side effect should be idempotent or protected by an idempotency key. Otherwise, a timeout after the server has completed an operation could lead a later attempt to repeat that effect.
Set backoff to fit the failure pattern
A fixed delay is predictable and can suit low-volume operations. Exponential backoff lengthens the pause after repeated failures, reducing pressure on a service that may be recovering. Adding jitter randomizes those pauses so many clients are less likely to retry in lockstep.
Best Value
With Spring Framework core, the builder controls include delay, multiplier, maxDelay, and jitter; a custom BackOff can be used instead of those scalar settings. Spring Retry offers builder backoff methods such as exponentialBackoff and policies with randomized delay. Choose both the retry limit and delay with the operation’s latency budget in mind: retries and waits consume time before a result or error reaches the caller.
Handle exhaustion and observe retries
Decide what the caller should receive if the operation never succeeds. In Spring Retry, provide a RecoveryCallback when an appropriate fallback exists; otherwise, exhaustion rethrows the last failure. For the core API, use its own execution and recovery facilities rather than assuming Spring Retry’s overloads are available.
Both API families provide listener hooks that can support logging, metrics, tracing, or audit events. The core API exposes setRetryListener and composite listeners. Spring Retry listeners can observe events before the first attempt, after unsuccessful attempts, and after the final attempt. Keep listener output useful but avoid logging credentials, personal data, or sensitive request payloads. See the core RetryTemplate API and Spring Retry reference.
Quick Recap
Implementation checklist
- Identify the dependency and import. Confirm whether the project uses Spring Framework core or the separate Spring Retry library.
- Define retryable exceptions. Include transient failures; let permanent failures propagate.
- Bound the work. Set a maximum retry count or a total timeout appropriate to the caller’s latency budget.
- Choose the wait policy. Use fixed delay for predictable pauses or exponential backoff with jitter where repeated, coordinated retries could worsen a disruption.
- Protect side effects. Make the operation idempotent or use an idempotency key.
- Choose exhaustion behavior. Recover with a suitable fallback or allow the failure to reach the caller.
- Add observability. Use listeners for retry events while keeping sensitive data out of logs.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

