在Spring bean的基类中定义的方法中使用@Retryable不会重试 [英] Using @Retryable in methods define in spring bean's base class are not retried

查看:374
本文介绍了在Spring bean的基类中定义的方法中使用@Retryable不会重试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类型B的spring托管bean.我在@Configuration类中有@EnableREtry.当我在doStuff()上使用@Retryable时,该方法将按预期在失败时重试.

I have a spring managed bean of type B. I have @EnableREtry in a @Configuration class. When I use @Retryable on doStuff(), the method gets retried on failure as expected.

但是,我真正想重试的方法是在基类A中定义的方法.A是一个具体的类,而不是spring托管的bean. doSomethingElse方法不会在引发异常时重试.

But, the method I really want to retry is a method defined in the base class, A. A is a concrete class and not a spring managed bean. the doSomethingElse method doesn't get retried on throwing an exception.

我真的想重试doSomethingElse,这是基类方法.但是,我不确定如何执行此操作.我猜这是因为A是一个具体类而不是bean,尽管它确实充当了基类.

I really want doSomethingElse to be retried, the base class method. However, I'm not sure how to do this. I'm guessing it's because A is a concrete class and not a bean, although it does serve as a base class.

我需要在A类中使用RetryableTemplate吗?

Do I need to use a RetryableTemplate in class A?

public class B extends A {

   public void doStuff() {
      super.doSomethingElse();
   }
}

public class A {
     // doesn't actually retry
    @Retryable
    public void doSomething() {
      throws new Exception();
    }
}

推荐答案

@Retryable使用仅可重试方法的外部调用通过代理(在RetryTemplate中调用该方法);类中的内部调用会绕过代理,因此不会重试.

Only external calls to retryable methods go through the proxy (which invokes the method within a RetryTemplate); internal calls within the class bypass the proxy and therefore are not retried.

您可以玩一些技巧从应用程序上下文中获取对代理的引用并进行调用,或者直接在您的doStuff()方法中直接使用RetryTemplate.

You can play some tricks to get a reference to the proxy from the application context and call that, or simply use a RetryTemplate directly within your doStuff() method.

这篇关于在Spring bean的基类中定义的方法中使用@Retryable不会重试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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