In this post under Spring Retry, I will show with example how to configure the exceptions for which retry has to happen.
For the example I will use the below service class.
Service8
1 import org.springframework.retry.annotation.Retryable;
2 import org.springframework.stereotype.Service;
3
4 @Service
5 public class Service8 {
6 private int i = 0;
7
8 @Retryable(value = {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 NullPointerException();
14 } else if (i == 2) {
15 throw new IllegalArgumentException();
16 } else {
17 throw new IndexOutOfBoundsException();
18 }
19 }
20 }
As you can see in the above code, the method to be retried is annotated with “Retryable” annotation. The annotation also list the exceptions for which the method has to be retried separated by comma. In this way we can set the exceptions for which the method has to be retried.
Next we will see the main class code for your reference.
Main
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 Example29 {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(Example29.class);
Service8 service8 = (Service8) context.getBean("service8");
try {
service8.executeWithException();
} catch(Exception excep) {
excep.printStackTrace();
}
}
@Bean(name="service8")
public Service8 getService() {
return new Service8();
}
}
Below is the output
Output
[INFO] AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Sat Jun 04 09:33:06 IST 2022]; root of context hierarchy
Executing method 'executeWithException' : 1
Executing method 'executeWithException' : 2
Executing method 'executeWithException' : 3
java.lang.IndexOutOfBoundsException
at Service8.executeWithException(Service8.java:17)
at Service8$$FastClassBySpringCGLIB$$560f7443.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 Service8$$EnhancerBySpringCGLIB$$f805f537.executeWithException(<generated>)
at Example29.main(Example29.java:15)
If you see the output, “executeWithException” is retried whenever “NullPointerException” and “IllegalArgumentException” is thrown
But when “IndexOutOfBoundsException” is thrown, the method is not retried.
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.