Configuring exceptions to exclude from retry (using annotations)

In the previous post under Spring Retry, I showed with example how to specify the exceptions through annotation, for which a annotated operation has to be retried.

In this post, I will show with example how to mention the exceptions through annotation, that when thrown by a annotated operation, Spring retry shouldn’t retry that operation.

In other words, how to exclude the exceptions for which Spring retry shouldn’t retry the failed operation.

For this post, I will use the below service class

Service Class


1  import org.springframework.retry.annotation.Retryable;
2  import org.springframework.stereotype.Service;
3  
4  @Service
5  public class Service9 {
6      private int i = 0;
7  
8      @Retryable(exclude = {NullPointerException.class, IllegalArgumentException.class})
9      public void executeWithException() {
10         i = i + 1;
11         System.out.println("Executing method 'executeWithException' : " + i);
12         if (i == 1) {
13             throw new IndexOutOfBoundsException();
14         } else if (i == 2) {
15             throw new IllegalArgumentException();
16         } else {
17             throw new NullPointerException();
18         }
19     }
20 }

In the above code, at line 8, I annotate the “executeWithException” with “@Retryable” annotation.

We also set the annotations “exclude” attribute with list of exceptions that the Spring Retry has to ignore, when the annotated method throws those exceptions.

In our code, we are telling Spring retry to ignore NullPointerException and IllegalArgumentException.

Next I will show the main code for your reference.

Main Class


import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.annotation.EnableRetry;

@Configuration
@EnableRetry
public class Example30 {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(Example30.class);
        Service9 service9 = (Service9) context.getBean("service9");

        try {
            service9.executeWithException();
        } catch(Exception excep) {
            excep.printStackTrace();
        }
    }

    @Bean(name="service9")
    public Service9 getService() {
        return new Service9();
    }
}

Below is the output

Output


[INFO] AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Sat Jun 11 09:39:24 IST 2022]; root of context hierarchy
Executing method 'executeWithException' : 1
Executing method 'executeWithException' : 2
java.lang.IllegalArgumentException
    at Service9.executeWithException(Service9.java:15)
    at Service9$$FastClassBySpringCGLIB$$560f7444.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 Service9$$EnhancerBySpringCGLIB$$e6685250.executeWithException(<generated>)
    at Example30.main(Example30.java:15)

As you can see from the output, when the first exception was thrown it was “IndexOutOfBoundsException” and the Spring Retry retried the operation. Whereas when the second exception “IllegalArgumentException” was thrown, Spring Retry didn’t retried the operation.

In this way we configure the exceptions that has to be excluded when thrown.

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