Spring重试中的@Retryable注释不会触发

时间:2016-08-03 20:27:19

标签: java spring-boot spring-retry

我有一个表更新导致死锁,并且当方法获得某种锁定异常时,我试图让Spring Retry重试。我已经尝试取出maxAttempts,值和退避但它似乎永远不会捕获任何异常。我错过了什么吗?我是否需要在Application文件中声明一个bean?任何帮助将不胜感激!

Application.Java

@SpringBootApplication
@EnableRetry
public class Application extends SpringBootServletInitializer {

DetailService

@Service
public class DetailService {

    @Retryable(maxAttempts = 5, value = { LockAcquisitionException.class, ConcurrencyFailureException.class }, backoff = @Backoff(delay = 500, multiplier = 2) )
    public void delete(final String detailCode) {
        try {
            this.delete(this.dao.findByDetailCode(detailCode));
        } catch (LockAcquisitionException | ConcurrencyFailureException e) {
            LOG.warn("Locking error! Going to retry", e.getMessage());
            throw e;
        }
    }

    public void delete(Details detail) {
           this.dao.delete(detail);            
    }

    @Retryable(maxAttempts = 5, value = { LockAcquisitionException.class, ConcurrencyFailureException.class }, backoff = @Backoff(delay = 500, multiplier = 2) )
    public void delete(final Integer id) {
        if (id != null) {
            try {
                this.delete(this.dao.findOne(id));
            } catch (LockAcquisitionException | ConcurrencyFailureException e) { 
                 LOG.warn("Locking error! Going to retry", e.getMessage());
                 throw e;
            }
        }
    }

修改

重新编写我的DetailService以提供更多细节并添加缺少的方法

1 个答案:

答案 0 :(得分:8)

如果从同一个类中调用delete()方法(在DetailService中),则会使Spring包装bean的代理短路。

具有注释的类必须是Spring管理的bean,并且必须从其他一些Spring管理的bean调用delete()方法,该bean可以通过自动装配,注入等访问可重试的bean。

修改

如果您需要从此课程中的其他方法调用delete()方法,则无法使用注释 - 请改为使用经过适当配置的RetryTemplate