Combining multiple Retry Policies (using xml)

In this post under Spring Retry, I will show with example how to combine multiple retry policies.

Below is the xml configuration

XML code

1  <?xml version="1.0" encoding="UTF-8"?>
2  <beans xmlns=""
3  xmlns:xsi=""
4  xsi:schemaLocation="">
5      <bean id="maxAttemptsRetryPolicy1" class="org.springframework.retry.policy.MaxAttemptsRetryPolicy">
6          <property name="maxAttempts" value="3"/>
7      </bean>
9      <bean id="maxAttemptsRetryPolicy2" class="org.springframework.retry.policy.MaxAttemptsRetryPolicy">
10         <property name="maxAttempts" value="6"/>
11     </bean>
13     <bean id="retryPolicy" class="org.springframework.retry.policy.CompositeRetryPolicy">
14         <property name="policies">
15             <list value-type="org.springframework.retry.RetryPolicy">
16                 <ref bean="maxAttemptsRetryPolicy1"/>
17                 <ref bean="maxAttemptsRetryPolicy2"/>
18             </list>
19         </property>
20     </bean>
22     <bean id="retryTemplate" class="">
23         <property name="retryPolicy" ref="retryPolicy"/>
24     </bean>
26     <bean id="service1" class="Service1"/>
27 </beans>

In the above xml code, at line 5, I create bean definition named “maxAttemptsRetryPolicy1” for “MaxAttemptsRetryPolicy” class with “maxAttempts” property value as 3.

At line 9, I create bean definition named “maxAttemptsRetryPolicy2” for “MaxAttemptsRetryPolicy” class with “maxAttempts” property value as 6.

Now i add both the retry policies in a list and set it as value to “policies” property of “CompositeRetryPolicy” class. Please refer to bean definition from line 13 to 20.

Whenever a failed operation is retried, CompositeRetryPolicy loops through the list and calls each retry policies (in the order they are added) to figure out whether to retry or not. If all the retry policies in the list return true, the failed operation is retried. If anyone of the retry policies return false, the failed operation will not be retried. This behaviour can be changed by setting the “optimistic” boolean property of “CompositeRetryPolicy” class to true. By default this property is false.

In our example also “optimistic” property is false.

Below is the output


[INFO] ClassPathXmlApplicationContext - Refreshing startup date [Sat Jan 29 09:40:15 IST 2022]; root of context hierarchy
[INFO] XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [Example11.xml]
Executing method 'executeWithException' : 1
Executing method 'executeWithException' : 2
Executing method 'executeWithException' : 3
    at Service1.executeWithException(
    at Example11.lambda$0(
    at Example11.main(

As you can see from the output the failed operations is retried only 3 times because “optimistic” property is false and “maxAttemptsRetryPolicy1” will return false on 4th attempt. Causing the failed operation to end throwing exception.

In this way we can combine multiple retry policies.

Below is the Service and main class for your reference.

Service class

public class Service1 {
    private int i = 0;
    private int j = 0;

    public void executeWithException() {
        i = i + 1;
        System.out.println("Executing method 'executeWithException' : " + i);
        throw new NullPointerException();

    public void executeWithoutException() {
        j = j + 1;
        System.out.println("Executing method 'executeWithoutException' : " + j);

    public String recovery() {
        return "Recovered";

Main class

import org.springframework.context.ApplicationContext;

public class Example11 {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Example11.xml");
        Service1 service1 = (Service1)context.getBean("service1");
        RetryTemplate retryTemplate =  (RetryTemplate)context.getBean("retryTemplate");

        try {
            retryTemplate.execute((retryContext) -> { service1.executeWithException(); return null; });
        } catch(NullPointerException excep) {


Press like button if you like the post. Also share your opinion regarding posts through comments. Also mention what other framework you want me to cover through comments.

Leave a Reply