Java – Why Spring @Retryable does not provide retry

javaspringspring-retry

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.

Best Answer

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

@Configuration
@EnableRetry
public class Application {