I am trying to leverage both the retry and circuit breaker mechanism of spring-retry. I tried to use both annotations(@Retryable and @CircuitBreaker) in a particular function(like below), but Circuit Breaker was not working.
@Service
public class CommandAndRetry {
private static final Logger LOGGER = LoggerFactory.getLogger(SampleRetryService.class);
@CircuitBreaker(maxAttempts = 1, openTimeout = 10000)
@Retryable(
value = {TypeOneException.class},
maxAttempts = 3, backoff = @Backoff(2000))
public void retryWhenException() throws TypeOneException {
LOGGER.info("Retrying");
throw new TypeOneException();
}
@Recover
public void recover(Throwable t) throws Throwable {
LOGGER.info("SampleRetryService.recover");
throw t;
}
}
Then I tried dividing the functionality into two different functions, both having @Retryable and @CircuitBreaker respectively. In this case, retry mechanism was not working. Please find below code snippet.
PS: exec method(Circuit Breaker method) is invoked from a controller.
@Service
public class CommandAndRetry {
private static final Logger LOGGER = LoggerFactory.getLogger(SampleRetryService.class);
@CircuitBreaker(maxAttempts = 1, openTimeout = 10000)
public void exec() throws TypeOneException {
retryWhenException();
}
@Retryable(
value = {TypeOneException.class},
maxAttempts = 3, backoff = @Backoff(2000))
public void retryWhenException() throws TypeOneException {
LOGGER.info("Retrying");
throw new TypeOneException();
}
@Recover
public void recover(Throwable t) throws Throwable {
LOGGER.info("SampleRetryService.recover");
throw t;
}
}
Can anyone please tell why it's behaving like this.
Also please advise if there exists a better way to implement both retry and circuit-breaker. PS: I neither want to use resilience4j nor retryTemplate.