Configuring FixedBackOffPolicy for RetryTemplate (using RetryTemplateBuilder)

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

Before we start lets just recap what is BackOffPolicy and FixedBackOffPolicy.

Backoff policies help us define different policies which will tell Spring Retry how long to wait before retrying a failed operation.

FixedBackOffPolicy is one of the implmentations of BackOffPolicy provided out of the box by Spring framework. FixedBackOffPolicy tell Spring Retry to wait for fixed interval before retrying a failed operation.

For our example we will use Service class as shown below

Service class

import java.util.Date;

public class Service5 {
    private int i = 0;

    public void executeWithException() {
        i = i + 1;
        System.out.println("Executing method 'executeWithException' : " + i + " at " + new Date());
        throw new NullPointerException();

In the above java code, we have defined service class named “Service5” with one method “executeWithException” that throws NullPointerException;

Next we will see the xml configuration

XML Configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <bean id="service5" class="Service5"/>

As you see in the above xml code we have defined only one bean of type “Service5”.

Next we will see the main class code.

Main Class

1  import org.springframework.context.ApplicationContext;
2  import;
3  import;
5  public class Example20 {
6      public static void main(String[] args) {
7          ApplicationContext context = new ClassPathXmlApplicationContext("Example20.xml");
8          Service5 service5 = (Service5) context.getBean("service5");
9          RetryTemplate retryTemplate = RetryTemplate.builder().fixedBackoff(120000).build();
11         retryTemplate.execute(retryContext -> { service5.executeWithException(); return null; });
12     }
13 }

In the above java code, at line 8, we retrieve the bean instance of type “Service5”.

At line 9, We create an instance of RetryTemplateBuilder by calling


With method chaining we call “fixedBackoff” on RetryTemplateBuilder instance and pass 120000 in ms and then call “build”.

This will create a RetryTemplate instance with FixedBackOffPolicy configured.

So in the above code, before retrying failed operation, the Spring Retry will wait for 120000 ms.

In this way we configure FixedBackOffPolicy using RetryTemplateBuilder.

Leave a Reply