Spring Retry simple example (using annotation)

In this post under Spring Retry, I will explain how to configure Spring Retry framework into your application using annotation with simple example

At minimum, you need the below jars in your classpath
1) spring-retry-1.3.1.jar
2) commons-logging-1.2.jar
3) spring-core-4.3.22.RELEASE.jar
4) spring-aop-4.3.22.REELEASE.jar
5) spring-beans-4.3.22.RELEASE.jar
6) spring-context-4.3.22.RELEASE.jar
7) spring-expression-4.3.22.RELEASE.jar
8) aspectjweaver-1.8.9.jar

For the example I will use the below service class

Service6


1  import org.springframework.retry.annotation.Retryable;
2  import org.springframework.stereotype.Service;
3  
4  @Service
5  public class Service6 {
6      private int i = 0;
7  
8      @Retryable
9      public void executeWithException() {
10         i = i + 1;
11         System.out.println("Executing method 'executeWithException' : " + i);
12         throw new NullPointerException();
13     }
14 }

In the above service class, the method which has to be retried is marked with “@Retryable” annotation.

Retryable annotation has many attribute which we can use to further configure the retry settings. But for this example we will use the default settings.

Next we see how to call this method. Below is the main class

Example27


1  import org.springframework.context.ApplicationContext;
2  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3  import org.springframework.context.annotation.Bean;
4  import org.springframework.context.annotation.Configuration;
5  import org.springframework.retry.annotation.EnableRetry;
6  
7  @Configuration
8  @EnableRetry
9  public class Example27 {
10     public static void main(String[] args) {
11         ApplicationContext context = new AnnotationConfigApplicationContext(Example27.class);
12         Service6 service6 = (Service6) context.getBean("service6");
13         
14         try {
15             service6.executeWithException();
16         } catch(Exception excep) {
17             excep.printStackTrace();
18         }
19     }
20     
21     @Bean(name="service6")
22     public Service6 getService() {
23         return new Service6();
24     }
25 }

We need to mark the main class with “@Configuration” annotation as show at line 7. This annotation tells the Spring container that the class contains bean definitions and bean creation logic. The Spring container will use this information to create beans with requested by the user.

This is similar to xml configuration of beans the only difference here is we are using Java class instead of an xml file.

Next we need to enable Spring retry, so for that we annotate the main class with “@EnableRetry” annotation. As shown at line 8.

At line 11, we build the context.

At line 15, we call the Service class method which will throw exception and which has to be retried.

Below is the output

Output

[INFO] AnnotationConfigApplicationContext – Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@50040f0c: startup date [Sat Jun 12 10:56:19 IST 2021]; root of context hierarchy
Executing method ‘executeWithException’ : 1
Executing method ‘executeWithException’ : 2
Executing method ‘executeWithException’ : 3
java.lang.NullPointerException

at Service6.executeWithException(Service6.java:12)
at Service6$$FastClassBySpringCGLIB$$560f7441.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:736)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.retry.interceptor.RetryOperationsInterceptor$1.doWithRetry(RetryOperationsInterceptor.java:93)
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:329)
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:209)
at org.springframework.retry.interceptor.RetryOperationsInterceptor.invoke(RetryOperationsInterceptor.java:119)
at org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor.invoke(AnnotationAwareRetryOperationsInterceptor.java:163)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:671)
at Service6$$EnhancerBySpringCGLIB$$5acea97e.executeWithException(<generated>)
at Example27.main(Example27.java:15)

Leave a Reply