Configuring Custom Retry Policy for RetryTemplate (using RetryTemplateBuilder)

In this post under Spring Retry, I will show with example how to configure your own custom Retry Policy using RetryTemplateBuilder class.

For this example, we will use modified version of Spring’s MaxAttemptsRetryPolicy, as shown below

CustomMaxAttemptsRetryPolicy


import org.springframework.retry.RetryContext;
import org.springframework.retry.policy.MaxAttemptsRetryPolicy;

public class CustomMaxAttemptsRetryPolicy extends MaxAttemptsRetryPolicy {
    private String name;

    public CustomMaxAttemptsRetryPolicy(String name) {
        this.name = name;
    }

    @Override
    public boolean canRetry(RetryContext context) {
        System.out.println(this.name + " and " + "maxAttempts= " + this.getMaxAttempts() + ": " +super.canRetry(context) + ":" + "currentRetryCount=" + context.getRetryCount());
        return super.canRetry(context);
    }
}

As you can see in the above code, we are not doing anything special, we are just printing the policy name and retry statistics.

Next I will show the Service class used for our example

Service 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 two methods one that throws exception and another that doesn’t throw any exception.

Next we will see the xml configuration.

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="customMaxAttemptsRetryPolicy" class="CustomMaxAttemptsRetryPolicy">
        <constructor-arg name="name" value="customMaxAttemptsRetryPolicy"/>
        <property name="maxAttempts" value="3"/>
    </bean>

    <bean id="service1" class="Service1"/>
</beans>

In the above xml code, we have created a bean for our CustomMaxAttemptsRetryPolicy and a bean for our service class.

Now we will see the main class where we create a RetryTemplate instance with our custom retry policy.

Main Class


1  import org.springframework.context.ApplicationContext;
2  import org.springframework.context.support.ClassPathXmlApplicationContext;
3  import org.springframework.retry.RetryPolicy;
4  import org.springframework.retry.support.RetryTemplate;
5  
6  public class Example23 {
7      public static void main(String[] args) {
8          ApplicationContext context = new ClassPathXmlApplicationContext("Example23.xml");
9          Service1 service1 = (Service1)context.getBean("service1");
10         RetryPolicy retryPolicy = (RetryPolicy) context.getBean("customMaxAttemptsRetryPolicy");
11         
12         RetryTemplate retryTemplate = RetryTemplate.builder().customPolicy(retryPolicy).build();
13         
14         try {
15             retryTemplate.execute((retryContext) -> { service1.executeWithException(); return null; });
16         } catch(NullPointerException excep) {
17             excep.printStackTrace();
18         }
19     }
20 }

In the above code, at line 9 and 10, we create an instance of Service class and custom retry policy.

Next at line 12, we first create an instance of RetryTemplateBuilder using “builder” static method available on RetryTemplate class. The “builder” method will return an instance of RetryTemplateBuilder. Then using method chaining, we call “customPolicy” method passing instance of our custom retry policy. Then finally we call build. The build method will create a RetryTemplate instance configured with our
custom retry policy.

In this way we can configure RetryTemplate to use our custom retry policy using RetryTemplateBuilder.

Leave a Reply