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


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

public class CustomMaxAttemptsRetryPolicy extends MaxAttemptsRetryPolicy {
    private String name;

    public CustomMaxAttemptsRetryPolicy(String name) { = name;

    public boolean canRetry(RetryContext context) {
        System.out.println( + " 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=""
    <bean id="customMaxAttemptsRetryPolicy" class="CustomMaxAttemptsRetryPolicy">
        <constructor-arg name="name" value="customMaxAttemptsRetryPolicy"/>
        <property name="maxAttempts" value="3"/>

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

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;
3  import org.springframework.retry.RetryPolicy;
4  import;
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");
12         RetryTemplate retryTemplate = RetryTemplate.builder().customPolicy(retryPolicy).build();
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