我在用Spring-retry-1.2.0 https://github.com/spring-projects/spring-retry/blob/master/README.md,重试工作正常,但在我的方法中我想知道重试是否是最后一次重试,是否有任何方法可以在 spring-retry 中获取 retryCount 或最后一次重试?
重审.java
public class Offers extends SimpleRetryPolicy {
@Async
@Retryable(maxAttemptsExpression = "#{retrial.times}", backoff = @Backoff(delayExpression = "#{retrial.delay}"))
public void handleOfferes(Data data) {
AlwaysRetryPolicy policy = new AlwaysRetryPolicy();
RetryContext context = policy.open(null);
System.out.println("context.getRetryCount()::"+context.getRetryCount()); //Always logs 0 even if it retries 2 to 3 times...
System.out.println("Can retry::"+canRetry(context)); //Always true
//Business Logic
//Get the last count and update the DB ...retrial is ends with failure
}
@Override
public boolean canRetry(RetryContext context) {
System.out.println("context.getRetryCount()::"+context.getRetryCount());
System.out.println("getMaxAttempts::"+getMaxAttempts());
return context.getRetryCount() < getMaxAttempts();
}
}
Artem Bilan 发表评论后所做的更改
重试配置
@Configuration
public class RetryConfiguration {
@Value("${retrial.times}")
private Long delayExpression;
@Value("${retrial.delay}")
private int maxAttempts;
@Bean
@ConditionalOnMissingBean(name = "retryInterceptor")
public RetryOperationsInterceptor retryInterceptor() {
return RetryInterceptorBuilder
.stateless()
.backOffOptions(0L,
0.0D, delayExpression)
.maxAttempts(maxAttempts).build();
}
}
优惠.java
@Service
public class Offers {
@Async
@Retryable(interceptor = "retryInterceptor")
public void handleDeviceStatus(Data data) {
//Here how shall i get the retrial count...
}
任何帮助将不胜感激。
我认为你需要的是@Recover
(RecoveryCallback
如果没有注释):
@Retryable(RemoteAccessException.class)
public void service() {
// ... do something
}
@Recover
public void recover(RemoteAccessException e) {
// ... panic
}
看到同样的README https://github.com/spring-projects/spring-retry/blob/master/README.md and 测试用例 https://github.com/spring-projects/spring-retry/blob/master/src/test/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandlerTests.java就此事。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)