<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.1.5.RELEASE</version>
</dependency>
@EnableRetry
@SpringBootApplication
@ComponentScan(basePackages="com.example")
@EnableAutoConfiguration
public class SampleApplication extends SpringBootServletInitializer{
///.....
}
@Service
public class MyService {
@Retryable(value = {
IndexOutOfBoundsException.class, Exception.class}, maxAttempts = 10, backoff = @Backoff(delay = 5000))
IndexOutOfBoundsException.class, Exception.class}, maxAttempts = 10, backoff = @Backoff(delay = 5000))
public void retryWithException() {
System.out.println("running......");
List<Integer> list = new ArrayList<>();
list.get(3);
}
@RequestMapping(value="/testRetry",method=RequestMethod.GET)
public @ResponseBody String retry(){
System.out.println("requestReceived::"+"/retry");
myservice.retryWithException();
return "done";
}
output:
requestReceived::/retry
running......
running......
running......
running......
running......
running......
running......
running......
running......
running......
2017-12-10 19:24:26.924 ERROR 30540 --- [io-8080-exec-17] o.s.boot.web.support.ErrorPageFilter : Forwarding to error page from request [/testRetry] due to exception [Index: 3, Size: 0]
java.lang.IndexOutOfBoundsException: Index: 3, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653) ~[na:1.8.0_144]
at java.util.ArrayList.get(ArrayList.java:429) ~[na:1.8.0_144]
//recover after retry
//add below in MyService class
output:
requestReceived::/retry
running......
running......
running......
running......
running......
running......
running......
running......
running......
running......
ah... catch
//recover after retry
//add below in MyService class
@Recover
public void recover(IndexOutOfBoundsException exception) {
System.out.println("ah... catch");
}
output:
requestReceived::/retry
running......
running......
running......
running......
running......
running......
running......
running......
running......
running......
ah... catch