FixedBackOffPolicy Simple Example (using xml configuration)

In this post under Spring Retry, I will explain backoff policy concept with example.

In Spring Retry, when an operation fails, it is immediately retried but if you want Spring Retry to wait for some time before retrying, you can take help of backoff policies.

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

For this, Spring Retry provides BackOffPolicy interface and many out of the box implmentations.

In this post I will give an example of FixedBackOffPolicy (one of the implmentations of BackOffPolicy). FixedBackOffPolicy tell Spring Retry to wait for fixed interval before retrying a failed operation.

Below is the xml configuration

XML Code


1  <?xml version="1.0" encoding="UTF-8"?>
2  <beans xmlns="http://www.springframework.org/schema/beans"
3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
5      <bean id="backOffPolicy" class="org.springframework.retry.backoff.FixedBackOffPolicy">
6          <property name="backOffPeriod" value="120000"/>
7      </bean>
8      
9      <bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
10         <property name="backOffPolicy" ref="backOffPolicy"/>
11     </bean>
12     
13     <bean id="service5" class="Service5"/>
14 </beans>

In the above xml code, at line 5, I have created a bean definition for “FixedBackOffPolicy” class named “backOffPolicy” and set the “backOffPeriod” to 120000 ms.

So now Spring Retry will wait for 120000 ms before retrying any failed operation.

Then at line 10, I set the “backOffPolicy” bean to a property “backOffPolicy” under RetryTemplate class.

In this way we create and set a BackOffPolicy in Spring Retry

Below is the Service and Main class for your reference.

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();
    }
}

Main Class


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.retry.support.RetryTemplate;

public class Example12 {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Example12.xml");
        Service5 service5 = (Service5)context.getBean("service5");
        RetryTemplate retryTemplate =  (RetryTemplate)context.getBean("retryTemplate");

        retryTemplate.execute(retryContext -> { service5.executeWithException(); return null; });
    }
}

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