Configuring StatisticsListener (using interceptor)

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

Below is the complete xml code which shows how to configure StatisticsListener using interceptor.

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 xmlns:aop = "http://www.springframework.org/schema/aop"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
6 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
7 <aop:config>
8 <aop:pointcut id="transactional" expression="execution(* defaultPackage.Service1.executeWithException(..))"/>
9 <aop:advisor pointcut-ref="transactional" advice-ref="retryAdvice"/>
10 </aop:config>
11
12 <bean id="retryAdvice" class="org.springframework.retry.interceptor.RetryOperationsInterceptor">
13 <property name="retryOperations" ref="retryTemplate"/>
14 <property name="label" value="Example30"/>
15 </bean>
16
17 <bean id="statisticsRepository" class="org.springframework.retry.stats.DefaultStatisticsRepository"/>
18
19 <bean id="statisticsListener" class="org.springframework.retry.stats.StatisticsListener">
20 <constructor-arg index="0" ref="statisticsRepository"/>
21 </bean>
22
23 <bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
24 <property name="listeners">
25 <list>
26 <ref bean="statisticsListener"/>
27 </list>
28 </property>
29 </bean>
30
31 <bean id="service1" class="defaultPackage.Service1"/>
32 </beans>

In the above xml code, at line 17 we are creating a bean of “DefaultStatisticsRepository” class.

At line 19, we are creating a bean of “StatisticsListener” class and passing “statisticsRepository” bean as a constructor argument.

At line 23, in bean definition of “RetryTemplate” we are referring to “statisticsListener” bean and adding to “listeners” property.

At line 24, we are defining a bean of “RetryOperationsInterceptor”. While defining the bean we are setting its “retryOperations” property to “retryTemplate” bean.

Next we will see the Java code where these beans are used.

Below is the main code for your reference

Main class

1  package defaultPackage;
2
3 import java.util.Iterator;
4
5 import org.springframework.context.ApplicationContext;
6 import org.springframework.context.support.ClassPathXmlApplicationContext;
7 import org.springframework.retry.RetryStatistics;
8 import org.springframework.retry.stats.StatisticsRepository;
9
10 public class Example41 {
11 public static void main(String[] args) {
12 ApplicationContext context = new ClassPathXmlApplicationContext("resources\\Example30.xml");
13 Service1 service1 = (Service1)context.getBean("service1");
14
15 try {
16 service1.executeWithException();
17 } catch(NullPointerException excep) {
18 excep.printStackTrace();
19 }
20
21 StatisticsRepository statisticsRepository = (StatisticsRepository) context.getBean("statisticsRepository");
22 Iterator<RetryStatistics> retryStatistics = statisticsRepository.findAll().iterator();
23
24 while(retryStatistics.hasNext()) {
25 RetryStatistics entry = retryStatistics.next();
26 System.out.println(entry);
27 }
28 }
29 }

In the above main code, at line 13 we are getting an instance of “Service1” class.

At line 16, we are calling “executeWithException” method of “Service1” class.

Then at line 21, we are getting the instance of “StatisticsRepository” class which stores the statistics.

In the while loop at line 24, we iterate through the statistics stored in “StatisticsRepository” instance and print it to console.

In this way we can configure StatisticsListener using Interceptor.

Leave a comment