void close(RetryContext context, RetryCallback callback, Throwable throwable) { if (throwable != null) { log.error("All retry attempts exhausted after {} tries", context.getRetryCount(), throwable); } } } Approach 2 — Micrometer metrics counter: @Override public void onError(RetryContext ctx, ...) { meterRegistry.counter("app.retry.attempt", "method", ctx.getAttribute("context.name").toString()).increment(); } Register the listener as a bean and it will be auto-detected by Spring Retry when annotation-based retry is enabled. For programmatic retry, pass it explicitly to retryTemplate.setListeners(). Dashboard alerts on high retry rates are an early indicator of upstream service degradation."> void close(RetryContext context, RetryCallback callback, Throwable throwable) { if (throwable != null) { log.error("All retry attempts exhausted after {} tries", context.getRetryCount(), throwable); } } } Approach 2 — Micrometer metrics counter: @Override public void onError(RetryContext ctx, ...) { meterRegistry.counter("app.retry.attempt", "method", ctx.getAttribute("context.name").toString()).increment(); } Register the listener as a bean and it will be auto-detected by Spring Retry when annotation-based retry is enabled. For programmatic retry, pass it explicitly to retryTemplate.setListeners(). Dashboard alerts on high retry rates are an early indicator of upstream service degradation." />

Prev Next

Spring / Spring Retry Interview Questions

How can you monitor and observe Spring Retry behavior in production?

Observing retry behavior in production is essential for detecting systemic issues with downstream dependencies. Spring Retry provides hooks through RetryListener, and for Spring Boot applications, you can wire this into Micrometer for metrics or into logging frameworks for audit trails.

Approach 1 — Logging RetryListener:

@Component
public class RetryLoggingListener implements RetryListener {

    private static final Logger log = LoggerFactory.getLogger(RetryLoggingListener.class);

    @Override
    public <T, E extends Throwable> void onError(RetryContext context,
        RetryCallback<T, E> callback, Throwable throwable) {
        log.warn("Retry attempt {} failed: {}",
            context.getRetryCount(), throwable.getMessage());
    }

    @Override
    public <T, E extends Throwable> void close(RetryContext context,
        RetryCallback<T, E> callback, Throwable throwable) {
        if (throwable != null) {
            log.error("All retry attempts exhausted after {} tries",
                context.getRetryCount(), throwable);
        }
    }
}

Approach 2 — Micrometer metrics counter:

@Override
public <T, E extends Throwable> void onError(RetryContext ctx, ...) {
    meterRegistry.counter("app.retry.attempt",
        "method", ctx.getAttribute("context.name").toString()).increment();
}

Register the listener as a bean and it will be auto-detected by Spring Retry when annotation-based retry is enabled. For programmatic retry, pass it explicitly to retryTemplate.setListeners(). Dashboard alerts on high retry rates are an early indicator of upstream service degradation.

Which Spring Retry interface is used to hook into retry lifecycle events for monitoring?
How can you pass a RetryListener to a programmatically created RetryTemplate?

Invest now in Acorns!!! 🚀 Join Acorns and get your $5 bonus!

Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!

Earn passively and while sleeping

Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.

Invest now!!! Get Free equity stock (US, UK only)!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.


Webull! Receive free stock by signing up using the link: Webull signup.

More Related questions...

What is Spring Retry and why is it used? How do you add Spring Retry to a Spring Boot project? What is the @Retryable annotation and what are its key attributes? What is the @Recover annotation and when is it invoked? What is the @Backoff annotation and how does it control retry delays? What is RetryTemplate and how does it differ from annotation-based retry? What are the different retry policies available in Spring Retry? What is a BackOffPolicy in Spring Retry and what implementations are available? What is a RetryContext and what information does it carry? How does the RetryListener interface work in Spring Retry? How does Spring Retry implement the circuit breaker pattern? What is stateful retry in Spring Retry and when should it be used? How do you configure Spring Retry using application properties in Spring Boot? What is the difference between include and exclude in @Retryable? How does Spring Retry integrate with Spring Batch? What common pitfalls should you avoid when using @Retryable? How do you write a unit test for a @Retryable method? How does Spring Retry handle exceptions that are not in the retryFor list? What is the RetryOperationsInterceptor and how is it used? How can you use Spring Retry with Spring WebClient or RestTemplate? How does the ExceptionClassifierRetryPolicy work? What is the RetryCallback interface in Spring Retry? How does Spring Retry differ from Resilience4j Retry? What is the RetryContextCache and when do you need a custom implementation? Can Spring Retry be used with Spring Cloud OpenFeign? How? How do you implement a custom RetryPolicy in Spring Retry? How do you use Spring Retry with Spring Kafka consumers? What is the proxyTargetClass attribute in @EnableRetry? How does the CompositeRetryPolicy work in Spring Retry? What is the difference between retry and idempotency? Why does it matter? How do you configure Spring Retry to use a fixed backoff programmatically? What is the maxAttemptsExpression attribute and how does it differ from maxAttempts? How does Spring Retry integrate with Spring's @Transactional? What is the use of the recover attribute in @Retryable? What are transient vs permanent failures and why does the distinction matter for retry? How do you implement retry with a custom exception condition using RetryPolicy? How does Spring Retry handle the case when @Recover returns void? What is exponential backoff with jitter and why is it preferred over plain exponential backoff? How can you monitor and observe Spring Retry behavior in production? What is the difference between RetryTemplate.execute() and RetryTemplate.executeWithLoadBalancer()?


Comments & Discussions