I have the following code:
@Retryable(value = ObjectOptimisticLockingFailureException.class, maxAttempts = 10, backoff = @Backoff(delay = 50L))
public void foo(Request request, String status) {
request.setStatus();
// some additional logic
repository.save(request);
}
Method foo
is called from many places. However, I need to refresh request
from DB every time exception is happened. How can I configure annotation to refresh argument of the method?
CodePudding user response:
try to write @Retryable annotation like this
@Retryable(value = { RetryException.class }, maxAttempts = 4, label="retry API", backoff = @Backoff(delay = 5000))
CodePudding user response:
The best way to add custom logic for every retry is to register a RetryTemplate
@Bean
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(2000l);
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(2);
retryTemplate.setRetryPolicy(retryPolicy);
//add policy for wanted exception
return retryTemplate;
}
and to use this
public void foo(Request request, String status) {
retryTemplate.execute(t -> {
request.setStatus();
// some additional logic
repository.save(request);
});
}