Configuring RetryTemplate (using Interceptor)

In the previous post under Spring Retry –> XML Configuration –> Spring AOP, I showed with example how to configure Spring Retry using interceptor.

In that post I used RetryOperationsInterceptor class as shown in the below snippet.


    <bean id="retryAdvice" class="org.springframework.retry.interceptor.RetryOperationsInterceptor"/>

In the above code snippet, I created a bean of RetryOperationsInterceptor which will create a default RetryTemplate instance internally.

This default RetryTemplate instance is configured to retry the failed method a fixed number of times, which we cannot change.

In this post under Spring Retry, I will explain how to configure Spring RetryTemplate using Spring AOP with simple example.

For our example we will use the below service class.

Service1


package defaultPackage;

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

To add Spring Retry functionality in AOP style we will take help of “org.springframework.retry.interceptor.RetryOperationsInterceptor” as shown in the below xml definition.

XML definition


<?xml version="1.0" encoding="UTF-8"?>
1 <beans xmlns="http://www.springframework.org/schema/beans"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns:aop = "http://www.springframework.org/schema/aop"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
5 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
6     <aop:config>
7         <aop:pointcut id="transactional" expression="execution(* defaultPackage.Service1.executeWithException(..))"/>
8         <aop:advisor pointcut-ref="transactional" advice-ref="retryAdvice"/>
9     </aop:config>
10    
11    <bean id="retryAdvice" class="org.springframework.retry.interceptor.RetryOperationsInterceptor">
12        <property name="retryOperations" ref="retryTemplate"/>
13    </bean>
14    
15    <bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
16        <property name="retryPolicy" ref="retryPolicy"/>
17    </bean>
18    
19    <bean id="retryPolicy" class="org.springframework.retry.policy.MaxAttemptsRetryPolicy">
20        <property name="maxAttempts" value="5"/>
21    </bean>
22    
23    <bean id="service1" class="defaultPackage.Service1"/>
24</beans>1

In the above xml code, at line 19, I created a bean by id “retryPolicy” according to which I have configured Spring Retry to retry failed methods 5 times.

At line 15, I created a bean definition by id “retryTemplate” of instance “RetryTemplate” class.

This bean takes another bean definition by id “retryPolicy” as a dependent bean.

At line 11, I created a bean definition by id “retryAdvice” of “RetryOperationsInterceptor” class same as in previous post but this time I am providing an instance of “RetryTemplate” by
id “retryTemplate” as a dependent bean.

In this way we can configure our own custom implementation of RetryTemplate.

Leave a Reply