Spring / Spring Retry Interview Questions
How does the CompositeRetryPolicy work in Spring Retry?
CompositeRetryPolicy combines multiple RetryPolicy instances into one. It supports two composition modes: optimistic and pessimistic. This is useful when you need retry behavior that satisfies multiple independent conditions simultaneously.
Optimistic mode (default): A retry is allowed if any of the composed policies permit it. This is a logical OR across policies.
Pessimistic mode: A retry is allowed only if all of the composed policies permit it. This is a logical AND across policies.
SimpleRetryPolicy byCount = new SimpleRetryPolicy(5);
TimeoutRetryPolicy byTime = new TimeoutRetryPolicy();
byTime.setTimeout(10000); // max 10 seconds total
CompositeRetryPolicy composite = new CompositeRetryPolicy();
composite.setPolicies(new RetryPolicy[]{ byCount, byTime });
composite.setOptimistic(false); // pessimistic: both must agree
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(composite);In this example (pessimistic), both the count limit (5 attempts) and the time limit (10 seconds) must permit the retry. The retry stops when either limit is reached first — whichever comes first wins. This is a clean way to add a time-bound safety net on top of a count-based policy for long-running retries.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
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...
