In this post under Spring Retry, I will show with example the purpose and how to use the TimeoutRetryPolicy.
Spring Retry provides RetryPolicy interface and many out of the box implementations of this interface. This interface and its implementations decide whether to retry an operation or not based on given criteria. TimeoutRetryPolicy is one such implementation of RetryPolicy interface.
TimeoutRetryPolicy states that retry an operation if timeout hasn’t happened otherwise don’t retry.
For example if TimeoutRetryPolicy is set to 300ms and if an operation takes more than 1000ms to complete its execution and throw exception. It has crossed the 300ms timeout as a result, the operation is not retried.
If the operation finishes in 1ms and throws exception, the operation is retried till 300ms has reached.
Now I will show how to configure TimeoutRetryPolicy using xml
XML configuration
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.TimeoutRetryPolicy">
6 <property name="timeout" value="300"/>
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="service18" class="Service18"/>
14 </beans>
In the above xml configuration, at line 5, we declare a bean named “retryPolicy” for TimeoutRetryPolicy class and set the timeout property to 300ms
At line 10, we refer the bean “retryPolicy” as a property to RetryTemplate class.
In this way, we configure any RetryPolicy implementations to RetryTemplate class.
Below is the complete main and service class code for your reference.
Service
public class Service18 {
private int i = 0;
public void executeWithException() throws InterruptedException {
i = i + 1;
System.out.println("Executing method 'executeWithException' : " + i);
Thread.sleep(1000);
throw new NullPointerException();
}
}
Main class
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.retry.support.RetryTemplate;
public class Example5 {
public static void main(String[] args) throws InterruptedException {
ApplicationContext context = new ClassPathXmlApplicationContext("Example5.xml");
Service18 service18 = (Service18)context.getBean("service18");
RetryTemplate retryTemplate = (RetryTemplate)context.getBean("retryTemplate");
retryTemplate.execute(retryContext -> { service18.executeWithException(); return null; });
}
}
In the Service18 class, we set the sleep duration for “executeWithException” to 1000ms which is more than timeout i.e., 300ms. As a result, retry of the method will not happen as shown in the below output
Output1
[INFO] ClassPathXmlApplicationContext – Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6576fe71: startup date [Sat Jan 22 09:24:40 IST 2022]; root of context hierarchy
[INFO] XmlBeanDefinitionReader – Loading XML bean definitions from class path resource [Example5.xml]
Executing method ‘executeWithException’ : 1
Exception in thread “main” java.lang.NullPointerException
at Service18.executeWithException(Service18.java:8)
at Example5.lambda$0(Example5.java:11)
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:329)
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:209)
at Example5.main(Example5.java:11)
If we change the sleep duration for “executeWithException” to 100ms, retry of the method will take place multiple times until 300ms has reached. Below is the output
Output
[INFO] ClassPathXmlApplicationContext – Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6576fe71: startup date [Sat Jan 22 10:39:50 IST 2022]; root of context hierarchy
[INFO] XmlBeanDefinitionReader – Loading XML bean definitions from class path resource [Example5.xml]
Executing method ‘executeWithException’ : 1
Executing method ‘executeWithException’ : 2
Executing method ‘executeWithException’ : 3
Exception in thread “main” java.lang.NullPointerException
at Service18.executeWithException(Service18.java:8)
at Example5.lambda$0(Example5.java:11)
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:329)
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:209)
at Example5.main(Example5.java:11)