代码之家  ›  专栏  ›  技术社区  ›  USM kosa

Springboot@Retryable包含多个异常

  •  2
  • USM kosa  · 技术社区  · 6 年前

    我已经把 @可回收的 现在我需要包含多个异常以重试。

    代码:

     @Retryable(interceptor = "someRetryInterceptor",
            include = { SomeException.class, SomeOtherException.class })
    

    这是正确的方法吗?

    注:在 someRetryInterceptor 我已经定义了RetryPolicy。

    2 回复  |  直到 6 年前
        1
  •  4
  •   Bartosz Bilicki Ievgen Lukash    6 年前

    根据javadoc,拦截器与其他属性相互排斥。 所以您必须决定使用拦截器或include。

    但可以肯定的是: 只需进行单元测试! 使您的方法 @Retryable 注释抛出 SomeException SomeOtherException 看看吧。

    测试方法之一: 假设您正在重试方法

    void dummy() {
     someObject.someOperation();
    }
    

    嘲弄 someObject (使用Mockito或任何其他库)以便 someOperation 将抛出S omeException/SomeOtherException 。在测试中,验证someObject的调用次数。someOperation()

    若您有无限次重试(这很少有用),那个么测试将挂起,所以这样的测试必须有超时( @Test(timeout=1000) )你必须在以下情况下通过测试 TimeoutException 发生。

    如果重试次数有限,则应验证重试次数 someObject.someOperation() 在超时之前调用。

    注意:该测试必须是Spring测试(必须加载上下文 @EnableRetry 注释)。否则(如果将其编写为普通单元测试),则 @可回收的 注释将被完全忽略。

        2
  •  3
  •   Gary Russell    6 年前

    不当您指定 interceptor ,所有配置都必须在那里完成(包括 RetryPolicy )。

    请参阅javadocs:

    /**
     * Retry interceptor bean name to be applied for retryable method. Is mutually
     * exclusive with other attributes.
     * @return the retry interceptor bean name
     */
    String interceptor() default "";