【问题标题】:Spring Retry Not working春季重试不工作
【发布时间】:2015-12-17 19:58:33
【问题描述】:

我正在尝试设置一个非常简单的测试,看看我是否可以让 Springs Retry API 工作,但它似乎没有按预期工作。下面是我的代码/配置。我正在使用 Spring 3.0

POM.xml 中定义的 Spring 重试版本

<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
    <version>1.1.2.RELEASE</version>
</dependency>

被测类的接口

public interface RetryTest
{
     void test() throws Exception;
}

接口实现

@Component("retryTest")
@EnableRetry
public class RetryTestImpl implements RetryTest
{

   @Retryable(maxAttempts = 3)
   public void test() throws Exception
   {
      System.out.println("try!");
      throw new Exception();
   }
}

JUnit 类

public class TestIt
{

   @Before
   public void initSpringContext()
   {
      // Load the spring context 
      ApplicationContextUtil.initAppContext(MyConfig.class);
   }

   @Test
   public void test_retryLogic() throws Exception
   {

       RetryTest retryTest = (RetryTest)ApplicationContextUtil.getApplicationContext().getBean(
            "retryTest");
       retryTest.test();
   }

}

控制台输出

Bean loaded: retryTest
try!

我期待“尝试!”打印到控制台 3 次。而是抛出一个异常,JUnit 就在那里失败。重试不应该运行3次吗?我在这里错过了什么?

【问题讨论】:

    标签: junit spring-retry


    【解决方案1】:

    您需要展示您的MyConfig 课程。

    @EnableRetry 继续您的 @Configuration 类之一,而不是可重试目标。

    注释见java文档:

     * Global enabler for <code>@Retryable</code> annotations in Spring beans. If this is
     * declared on any <code>@Configuration</code> in the context then beans that have
     * retryable methods will be proxied and the retry handled according to the metadata in
     * the annotations.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-07
      • 2015-11-21
      • 2022-01-16
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多