I have a Bean thus:
import org.eclipse.microprofile.faulttolerance.Retry;
import jakarta.enterprise.context.ApplicationScoped;
import lombok.SneakyThrows;
@ApplicationScoped
public class MyService {
private int retryCount = 0;
@SneakyThrows
private void throwConnectException() {
throw new ConnectException("Test");
}
public void call_testRetryMethod() {
testRetryMethod();
}
@Retry(maxRetries = 5)
public void testRetryMethod() {
LOGGER.info("testRetryMethod: {}", retryCount++);
throwConnectException();
}
}
Now from outside, I inject variable of MyService in unit tests:
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
class MyServiceTest {
@Inject MyService myService;
@Test
public void testMyService() {
myService.testRetryMethod(); // This does retries for 6 times.
myService.call_testRetryMethod(); // This does not!
}
}
Based on documentation of https://github.com/microprofile/microprofile-fault-tolerance/tree/4.0, particularly following line, the behaviour above seems correct to me.
When an annotated method is called, the call is intercepted and the corresponding fault tolerance strategies are applied to the execution of that method.
Could you confirm ?
EDIT
Could you confirm that when annotated method is called from outside bean, retry
specification is applied but when it is called from inside bean it does not?
Thanks in advance!
I have gone through documentation: https://github.com/microprofile/microprofile-fault-tolerance/tree/4.0
The problem is that you're not calling the method from outside. So, the annotations are just ignored.