spring重试找到最后一次重试 [英] Spring retry find the last retry

查看:41
本文介绍了spring重试找到最后一次重试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring-retry-1.2.0,重试工作正常,但在我的方法中,我想知道重试是否是最后一次重试,是否有任何方法可以在 spring-retry 中获取 retrialCount 或最后一次重试?

I am using Spring-retry-1.2.0, Retry is working fine, but in my method i want to find it out whether the retrial is the last retrial or not, Is there any method available to get the retrialCount or last retrial in spring-retry?

重试.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();
}
}

Offer.java

@Service
public class Offers {
@Async
@Retryable(interceptor = "retryInterceptor")
public void handleDeviceStatus(Data data) {
//Here how shall i get the retrial count...

}

任何帮助都是可观的.

推荐答案

我觉得你需要的是@Recover(RecoveryCallback 如果没有注解):

I think what you need is called @Recover (RecoveryCallback if without annotations):

@Retryable(RemoteAccessException.class)
public void service() {
    // ... do something
}
@Recover
public void recover(RemoteAccessException e) {
   // ... panic
}

查看相同的READMEtest-案件.

这篇关于spring重试找到最后一次重试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆