Spring Retryable annotation ClassNotFoundException
It worked for me after adding the spring-boot-starter-aop dependency like below:
compile ("org.springframework.boot:spring-boot-starter-aop:1.5.10.RELEASE")
I will answer my own question 'cause it seems to be working now. All dependencies that are necessary:
<dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> <version>1.2.1.RELEASE</version></dependency><dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.11.RELEASE</version></dependency><dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version></dependency>
check if there is no duplicate dependencies (f.e. by mvn dependency:tree
, use <exclusions>
where necessary). Be sure you decorated your config class with @EnableRetry
. For some reason you need to separate the caller from the method itself. F.e.this:
@Retryable(maxAttempts = 4, value = {ResourceAccessException.class}, backoff = @Backoff(delay = 5000))public ResponseEntity<String> tryToSendAndReturnResponseByRestTemplate(final RestTemplate restTemplate, final HttpEntity<MyObjDTO> request) { return restTemplate.exchange(resolveUrl(ENDPOINT_ADDRESS), HttpMethod.POST, request, String.class);}
goes to SenderManager
class marked as f.e. @Service
and in a different class f.e. PhoneDirector
class you call:
senderManager.tryToSendAndReturnResponseByRestTemplate(restTemplate, request);
It should work. (If you need to specify what supposed to be returned if after 4
(only here) attempts you're still getting an exception, you can create another method (in SenderManager
class) marked with @Recover
, like this:
@Recoverpublic ResponseEntity<String> recoverWhenSendingMessageFailed(final ResourceAccessException e) { return new ResponseEntity<>(e.getMessage(), HttpStatus.REQUEST_TIMEOUT);}
But If you needed args from @Retryable
(in @Recover
method) remeber that arguments are populated from the argument list of the failed method in the same order as the failed method, and with the same return type
spring-retry uses spring aop, make sure you have aop added in your pom and try clean building the application.
for spring app :
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.4.RELEASE</version></dependency>
for spring boot :
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> <version>2.0.3.RELEASE</version></dependency>