When adding onRetry event into method itself, retry log reprints ALL previous retries that are already retried.
binnie268 opened this issue · comments
binnie268 commented
Resilience4j version: 1.7.1
Java version: 8
Thanks for raising a Resilience4j issue.
We want to be able to log request specific information as part of the onRetry, therefore the onRetry is called inside the handlingRequest method, instead of inside the constructor:
Instead of:
public class RetryExample {
public RetryExample() {
RetryRegistry retryRegistry = RetryRegistry.ofDefaults();
this.retry = retryRegistry.retry("myRetry", RetryConfig.custom()
.maxAttempts(3)
.waitDuration(Duration.ofSeconds(1))
.build());
// Set up onRetry event listener
retry.getEventPublisher().onRetry(event -> {
System.out.println("Retry Event: " + event.getNumberOfAttempts());
});
}
}
We declare onRetry in like this:
public class RetryExample {
public RetryExample() {
RetryRegistry retryRegistry = RetryRegistry.ofDefaults();
this.retry = retryRegistry.retry("myRetry", RetryConfig.custom()
.maxAttempts(3)
.waitDuration(Duration.ofSeconds(1))
.build());
}
public void performOperation(RequestInfo requestInfo) {
// Set request-specific information for the current request
setCurrentRequestInfo(requestInfo);
// Set up onRetry event listener
retry.getEventPublisher().onRetry(event -> {
RequestInfo requestInfo = getRequestInfo();
System.out.println("Retry Event: " + event.getNumberOfAttempts());
System.out.println("Request Info: " + requestInfo);
// Your custom logic here with request-specific information
});
// set up retry and completion stage
downstreamService.publish(requestInfo);
}
}
setting onRetry inside the method allows us to have request specific information, but logs all previous retries. it should only log the current retry and no all previous retries. We should also have a way to remove the retries from the consumer map.