Why Spring @Retryable does not provide retry?

7k views Asked by At

I just setup the simplest Spring application with the @Retryable annotation.

@Service
public class MyRestService {
    @Autowired
    private RestTemplate restTemplate;

    @Retryable(Exception.class)
    public void runRest() {
        ResponseEntity<String> response = restTemplate.getForEntity(
            "https://dat.sparkfun.com/streams/dZ4EVmE8yGCRGx5XRX1W.json",
            String.class);
    }

    @Recover
    public void recover(Exception e) {
        System.out.println("Recover=" + e);
    }
}

By the Spring documentation (https://github.com/spring-projects/spring-retry) the method runRest should be run three times since it throws Exception (in particular org.springframework.web.client.ResourceAccessException). However, I do not observe any retry. Using as an argument ResourceAccessException to @Retryable does not help.

2

There are 2 answers

1
Alex On BEST ANSWER

Sorry, very easy answer. I needed to specify @EnableRetry in my class with the main method.

@Configuration
@EnableRetry
public class Application {
0
Den Roman On

You can use XML based config as well.

<aop:config>
<aop:pointcut id="retryable" expression="execution(* it..*class.method(..))" />
<aop:advisor pointcut-ref="retryable" advice-ref="retryAdvice" order="-1"/>