Configuring MaxAttemptsRetryPolicy for RetryTemplate (using RetryTemplateBuilder)

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

Lets refresh what is MaxAttemptsRetryPolicy. MaxAttemptsRetryPolicy tells Spring retry to retry a failed operation for a fixed number of times.

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="service1" class="Service1"/>
</beans>

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

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 "Recovered";
    }
}

As you can see in the above code, we have three methods, “executeWithException” will throw an exception, “executeWithoutException” will not throw an exception, and “recovery” will be used to recover from failed attempts.

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 Example18 {
6      public static void main(String[] args) {
7          ApplicationContext context = new ClassPathXmlApplicationContext("Example18.xml");
8          Service1 service1 = (Service1) context.getBean("service1");
9          RetryTemplate retryTemplate = RetryTemplate.builder().maxAttempts(5).build();
10         
11         retryTemplate.execute(retryContext -> { service1.executeWithException(); return null; });
12     }
13 }

In the above java code, at line 8, 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 MaxAttemptsRetryPolicy by calling “maxAttempts” on the instance of RetryTemplateBuilder and passing integer 5 and then we call “build” method to create an instance of RetryTemplate.

So this line will create an instance of RetryTemplate with MaxAttemptsRetryPolicy configured. This configured policy will tell RetryTemplate instance to retry the failed operation 5 times.

In this way we can configure MaxAttemptsRetryPolicy 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