I'm using Resilience4J in a Spring Boot project to make a call to a REST client like so:
@Retry(name = "customerService")
public Customer getCustomer(String customerNumber) {
restTemplate.exchange("https://customer-service.net", HttpMethod.GET, ..., customerNumber);
}
With this configuration:
resilience4j.retry:
instances:
customerService:
maxAttempts: 3
waitDuration: 10s
retryExceptions:
- org.springframework.web.client.HttpServerErrorException
My expectation is if restTemplate.exchange()
is invoked and the customer service responds with a HttpServerErrorException
, the getCustomer()
method will be called 3 more times after a wait of ten seconds.
However, this is not the case.
This method is never called again, and the exception is immediately thrown.
Seeing that a fallback method is included in the examples, I decided to add it, even though I don't really want to invoke a different method, I just want my original method called again.
Anyway, I specified a fall back:
@Retry(name = "customerService", fallback = "customerFb")
public Customer getCustomer(String customerNumber) {
return getCustomer();
}
private Customer customerFb(String customerNumber, HttpServerErrorException ex) {
log.error("Could not retrieve customer... retrying...");
return getCustomer();
}
private Customer getCustomer(String customerNumber) {
return restTemplate.exchange("https://customer-service.net", HttpMethod.GET, ..., customerNumber);
}
Now, I am seeing the fallback method being retried, however, the HttpServerErrorException is being thrown each time, meaning that consumers will receive an exception as a response to their calls.
My questions are:
Does a fallback method need to be implemented in order for retry functionality to work?
and
Is it expected behavior for the exceptions to be thrown? Am I doing something wrong? I don't want callers of this service to receive an exception until all retry attempts have been made.
Thanks