In this post under Spring Retry, I will show with example how to use SimpleRetryPolicy (another implementation of RetryPolicy interface) in place of MaxAttemptsRetryPolicy.
SimpleRetryPolicy retries a failed operation for fixed number of times for a set of named exceptions (and subclasses).
In this post I will configure SimpleRetryPolicy to retry failed operation for fixed number of times for any exception. Configuring the SimpleRetryPolicy to work like this is similar to MaxAttemptsRetryPolicy purpose.
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 </bean>
8
9 <bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
10 <property name="retryPolicy" ref="retryPolicy"/>
11 </bean>
12
13 <bean id="service1" class="Service1"/>
14 </beans>
In the above xml code, at line 5 I have created a bean named “retryPolicy” for SimpleRetryPolicy class and set its “maxAttempts” property to 4.
At line 10, we are referring to the bean “retryPolicy” when setting the property “retryPolicy” of class RetryTemplate.
In this way we are telling SimpleRetryPolicy to retry a failed operation 4 times for any exception. This is similar to MaxAttemptsRetryPolicy.
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;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.retry.support.RetryTemplate;
public class Example7 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Example7.xml");
Service1 service1 = (Service1)context.getBean("service1");
RetryTemplate retryTemplate = (RetryTemplate)context.getBean("retryTemplate");
try {
retryTemplate.execute(retryContext -> { service1.executeWithException(); return null; });
} catch(NullPointerException excep) {
excep.printStackTrace();
}
}
}
Below is the output
Output
[INFO] ClassPathXmlApplicationContext – Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6576fe71: startup date [Sun Jan 23 09:26:48 IST 2022]; root of context hierarchy
[INFO] XmlBeanDefinitionReader – Loading XML bean definitions from class path resource [Example7.xml]
Executing method ‘executeWithException’ : 1
Executing method ‘executeWithException’ : 2
Executing method ‘executeWithException’ : 3
Executing method ‘executeWithException’ : 4
java.lang.NullPointerException
at Service1.executeWithException(Service1.java:8)
at Example7.lambda$0(Example7.java:12)
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:329)
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:209)
at Example7.main(Example7.java:12)
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.