在所有 @Retryables 上执行的 RetryListenerSupport 处理程序

问题描述

我查看了文档,似乎(并且很直观)您必须在 @Retryable 注释侦听器的 arg 中注册一个 RetryListenerSupport。

但出于某种原因,RetryListenerSupport 会在项目中的所有 @Retryables 上执行,而不会将其添加到任何侦听器参数中 - 这是预期的行为吗?

如果是,那么听众的论点是什么?

解决方法

抱歉回复晚了,但我遇到了完全相同的问题。

我认为注释已更新,并且侦听器字段已删除,因为它没有出现在当前文档中。

我为我的项目实现了一个新的拦截器,为了记录重试,我实现了一个 RetryListener,它在拦截器内部使用的新 RetryTemplate 中注册。

举个例子胜过冗长的解释,它看起来像这样:

@Configuration
public class MyRetryInterceptor{
    @Bean
    public RetryOperationsInterceptor retryInterceptorCustom() {
        UniformRandomBackOffPolicy backOffPolicy = new BackOffPolicy(); //Configure it
        RetryPolicy retryPolicyCustom = new RetryPolicy(); //Configure it

        RetryTemplate template = RetryTemplate.builder()
                                    .customBackoff(backOffPolicy)
                                    .customPolicy(retryPolicyCustom )
                                    .withListener(new MyRetryListener())
                                    .build();
        RetryOperationsInterceptor interceptor = new RetryOperationsInterceptor();
        interceptor.setRetryOperations(template);
        return interceptor;
    }
}

然后您可以在 @Retryable 注释中使用它,如下所示:

@Retryable(interceptor = "retryInterceptorCustom")

我知道这并不完美,因此请自行承担风险。