In this post under Spring Retry, I will show with example how to configure RetryListener using RetryTemplateBuilder class.
First we will create a custom listener class which implements RetryListener interface as shown below
CustomRetryListener1
import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.RetryListener;
public class CustomRetryListener1 implements RetryListener {
@Override
public <T, E extends Throwable> void close(RetryContext arg0, RetryCallback<T, E> arg1, Throwable arg2) {
System.out.println("Calling 'close' method");
}
@Override
public <T, E extends Throwable> void onError(RetryContext arg0, RetryCallback<T, E> arg1, Throwable arg2) {
System.out.println("Calling 'onError' method");
}
@Override
public <T, E extends Throwable> boolean open(RetryContext arg0, RetryCallback<T, E> arg1) {
System.out.println("Calling 'open' method");
return true;
}
}
As you can see from the above code “CustomRetryListener1” class provides implementation for “close”, “onError”, and “open” method obtained from “RetryListener” interface.
Next we will create the Service class as shown below
Service1
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";
}
}
As you can see in the above code, I have added three methods, one that throws NullPointerException, another that doesn’t throw any exception, and third one is a recovery method used to recover when Spring Retry fails at all retry attempts.
Below is the xml configuration
XML Code
<?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="customRetryListener" class="CustomRetryListener1"/>
<bean id="service1" class="Service1"/>
</beans>
As you can see in the above xml code, I have created two beans one for RetryListener and another for Service1 class.
Now lets see the main class.
Main class
1 import org.springframework.context.ApplicationContext;
2 import org.springframework.context.support.ClassPathXmlApplicationContext;
3 import org.springframework.retry.RetryListener;
4 import org.springframework.retry.support.RetryTemplate;
5
6 public class Example17 {
7 public static void main(String[] args) {
8 ApplicationContext context = new ClassPathXmlApplicationContext("Example17.xml");
9 Service1 service1 = (Service1) context.getBean("service1");
10 RetryListener retryListener = (RetryListener) context.getBean("customRetryListener");
11 RetryTemplate retryTemplate = RetryTemplate.builder().withListener(retryListener).build();
12
13 retryTemplate.execute(retryContext -> { service1.executeWithException(); return null; });
14 }
15 }
In the above java code, at line 9 and 10 I get the instance for “Service1” and “CustomRetryListener1” class.
At line 11, with the help of method chaining and “RetryTemplateBuilder” class, we create an RetryTemplate instance with our CustomRetryListener1 instance configured in it.
We get an instance of RetryTemplateBuilder by calling RetryTemplate.builder() method.
Then we tell RetryTemplateBuilder instance to use CustomRetryListener1 by calling “withListener” method.
Then we will finally call “build” method on instance of “RetryTemplateBuilder” to create an instance of “RetryTemplate” with “CustomRetryListener1” instance configured in it.
That’s all we have to do configure an instance of RetryListener using RetryTemplateBuilder.
Below is the output
Output
[INFO] ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6576fe71: startup date [Sat Feb 26 08:52:03 IST 2022]; root of context hierarchy
[INFO] XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [Example17.xml]
Calling 'open' method
Exception in thread "main" Executing method 'executeWithException' : 1
Calling 'onError' method
Executing method 'executeWithException' : 2
Calling 'onError' method
Executing method 'executeWithException' : 3
Calling 'onError' method
Calling 'close' method
java.lang.NullPointerException
at Service1.executeWithException(Service1.java:8)
at Example17.lambda$0(Example17.java:13)
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:329)
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:209)
at Example17.main(Example17.java:13)
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.