Configuring TimeoutRetryPolicy for RetryTemplate (using RetryTemplateBuilder)

In this post under Spring Retry, I will show with example how to configure TimeoutRetryPolicy using RetryTemplateBuilder.

Lets refresh what is TimeoutRetryPolicy. TimeoutRetryPolicy tells Spring retry to retry a failed operation as many times as possible before the timeout happens. Once timeout happens the Spring retry stops
retrying the failed operation.

Now lets see how to configure using RetryTemplateBuilder. Below is the xml code for your reference

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="service18" class="Service18"/>
</beans>

As you can see we only have bean definition for Service18 class. Below is java code for Service18 class for your reference.

Service1 class


public class Service18 {
    private int i = 0;

    public void executeWithException() throws InterruptedException {
        i = i + 1;
        System.out.println("Executing method 'executeWithException' : " + i);
        Thread.sleep(100);
        throw new NullPointerException();
    }
}

As you can see in the above code, we have method “executeWithException” that will throw an exception

Next we will see the main method.

Main class


1  import org.springframework.context.ApplicationContext;
2  import org.springframework.context.support.ClassPathXmlApplicationContext;
3  import org.springframework.retry.support.RetryTemplate;
4  
5  public class Example19 {
6      public static void main(String[] args) throws Exception {
7          ApplicationContext context = new ClassPathXmlApplicationContext("Example19.xml");
8          Service18 service18 = (Service18) context.getBean("service18");
9          RetryTemplate retryTemplate = RetryTemplate.builder().withinMillis(300).build();
10         
11         retryTemplate.execute(retryContext -> { service18.executeWithException(); return null; });
12     }
13 }

In the above java code, at line 9, we are creating an instance of RetryTemplateBuilder by calling static “builder” method on RetryTemplate.

Then in the same line, using method chaining, we are configuring TimeoutRetryPolicy by calling “withinMillis” on the instance of RetryTemplateBuilder and passing integer 300 and then we call “build” method to create an instance of RetryTemplate.

So this line will create an instance of RetryTemplate with TimeoutRetryPolicy configured. This configured policy will tell RetryTemplate instance to retry the failed operation as many times as possible before the timeout occurs.

In this way we can configure TimeoutRetryPolicy using RetryTemplateBuilder.

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