In this post under Spring Retry, I will explain how to configure Spring Retry framework into your application using builder pattern with simple example
At minimum, you need the below jars in your classpath
1) spring-retry-1.3.1.jar
2) commons-logging-1.2.jar
3) spring-core-4.3.22.RELEASE.jar
4) spring-aop-4.3.22.REELEASE.jar
5) spring-beans-4.3.22.RELEASE.jar
6) spring-context-4.3.22.RELEASE.jar
7) spring-expression-4.3.22.RELEASE.jar
8) aspectjweaver-1.8.9.jar
For the example I will the below service class
Service1 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 "Hello";
}
}
As you can see in the above code, we have two methods one which throws exception and another doesn’t. Ignore the “recovery” method for now.
Lets define bean definition of the above service class in the below xml file
XML configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="service1" class="Service1"/>
</beans>
In the above xml file, we have defined only one bean which is of class “Service1”.
Next lets see the main class code
Main class
1 import org.springframework.context.ApplicationContext;
2 import org.springframework.context.support.ClassPathXmlApplicationContext;
3 import org.springframework.retry.support.RetryTemplate;
4 import org.springframework.retry.support.RetryTemplateBuilder;
5
6 public class Example16 {
7 public static void main(String[] args) {
8 ApplicationContext context = new ClassPathXmlApplicationContext("Example16.xml");
9 Service1 service1 = (Service1)context.getBean("service1");
10 RetryTemplateBuilder builder = RetryTemplate.builder();
11 RetryTemplate retryTemplate = builder.build();
12
13 retryTemplate.execute(retryContext -> { service1.executeWithException(); return null; });
14 }
15 }
In the above code, as with any Spring standalone application, at first we are creating an instance of ApplicationContext using Example1.xml file at line 7.
At line 8, we retrieve an instance of Service1 from the application context.
At line 9, we create an instance of RetryTemplateBuilder class by calling static method “builder” on RetryTemplate class.
At line 10, we create an instance of RetryTemplate by calling “build” method on the “builder” instance.
This will create a RetryTemplate instance with default configurations.
At line 13, we call the “execute” method on RetryTemplate instance passing the operation to be retried as method argument.
Below is the output
Output
[INFO] ClassPathXmlApplicationContext – Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6576fe71: startup date [Sat Jun 05 12:04:30 IST 2021]; root of context hierarchy
[INFO] XmlBeanDefinitionReader – Loading XML bean definitions from class path resource [Example16.xml]
Executing method ‘executeWithException’ : 1
Executing method ‘executeWithException’ : 2
Executing method ‘executeWithException’ : 3
Exception in thread “main”
java.lang.NullPointerException
at Service1.executeWithException(Service1.java:8)
at Example16.lambda$0(Example16.java:13)
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:329)
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:209)
at Example16.main(Example16.java:13)