Spring Retry only for specific exceptions (using xml configuration)

In this post under Spring Retry, I will show with example how to configure Spring Retry to retry failed operations for specific exceptions only. For this purpose Spring Retry provides out of the box SimpleRetryPolicy class.

Below is the xml configuration

XML Code


1  <?xml version="1.0" encoding="UTF-8"?>
2  <beans xmlns="http://www.springframework.org/schema/beans"
3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
5      <bean id="retryPolicy" class="org.springframework.retry.policy.SimpleRetryPolicy">
6          <constructor-arg name="maxAttempts" value="4"/>
7          <constructor-arg name="retryableExceptions">
8              <map key-type="java.lang.Class" value-type="java.lang.Boolean">
9                  <entry key="java.lang.NullPointerException" value="true"/>
10                 <entry key="java.lang.IllegalArgumentException" value="false"/>
11             </map>
12         </constructor-arg>
13     </bean>
14     
15     <bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
16         <property name="retryPolicy" ref="retryPolicy"/>
17     </bean>
18     
19     <bean id="service2" class="Service2"/>
20 </beans>

In the above xml code, at line 5 we created a bean named “retryPolicy” for SimpleRetryPolicy class.

In the defination we have set the “maxAttempts” property to 4.

Then we have created a Map where the key is Class object and value is a boolean.

Through this map, we are saying Spring Retry that if the exception is thrown when attempting any service operations and the exception is of type NullPointerException then retry (we indicate this by returning boolean value as map value). If the exception thrown is IllegalArgumentException then don’t retry (we indicate this by returning the corresponding map value which is false in this case).

Remaining everything is same.

Below is the Service and Main class for your reference.

Service class



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

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

    public void executeWithIllegalArgumentException() {
        j = j + 1;
        System.out.println("Executing 'executeWithIllegalArgumentException' : " + j);
        throw new IllegalArgumentException();
    }
}

Main class


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.retry.support.RetryTemplate;

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

        try {
            retryTemplate.execute(retryContext -> { service2.executeWithNullPointerException(); return null; });
        } catch(NullPointerException excep) {
            excep.printStackTrace();
        }

        try {
            retryTemplate.execute(retryContext -> { service2.executeWithIllegalArgumentException(); return null; });
        } catch(IllegalArgumentException excep) {
            excep.printStackTrace();
        }
    }
}

Below is the output

Output

[INFO] ClassPathXmlApplicationContext – Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6576fe71: startup date [Sun Jan 23 11:54:44 IST 2022]; root of context hierarchy
[INFO] XmlBeanDefinitionReader – Loading XML bean definitions from class path resource [Example8.xml]
Executing ‘executeWithNullPointerException’ : 1
Executing ‘executeWithNullPointerException’ : 2
Executing ‘executeWithNullPointerException’ : 3
Executing ‘executeWithNullPointerException’ : 4
java.lang.NullPointerException
at Service2.executeWithNullPointerException(Service2.java:9)
at Example8.lambda$0(Example8.java:12)
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:329)
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:209)
at Example8.main(Example8.java:12)
Executing ‘executeWithIllegalArgumentException’ : 1
java.lang.IllegalArgumentException
at Service2.executeWithIllegalArgumentException(Service2.java:15)
at Example8.lambda$1(Example8.java:18)
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:329)
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:209)
at Example8.main(Example8.java:18)

Note

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