Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring@Retryable-调用时如何记录?_Spring_Spring Boot_Spring Retry - Fatal编程技术网

Spring@Retryable-调用时如何记录?

Spring@Retryable-调用时如何记录?,spring,spring-boot,spring-retry,Spring,Spring Boot,Spring Retry,我使用编译org.springframework.retry:spring retry:1.2.2.RELEASE'和spring Boot 1.5.9.RELEASE 已配置为重试我的方法,并且效果良好: @Retryable(value = { IOException.class }, maxAttempts = 5, backoff = @Backoff(delay = 500)) public void someMethod(){...} 如何在重试时输出某些特定消息?查看代码org.

我使用
编译org.springframework.retry:spring retry:1.2.2.RELEASE'
spring Boot 1.5.9.RELEASE

已配置为重试我的方法,并且效果良好:

@Retryable(value = { IOException.class }, maxAttempts = 5, backoff = @Backoff(delay = 500))
public void someMethod(){...}

如何在重试时输出某些特定消息?

查看代码
org.springframework.retry.support.RetryTemplate
执行重试操作的重试逻辑。此模板仅记录以下简单内容:

o.s.retry.support.RetryTemplate          : Retry: count=0
o.s.retry.support.RetryTemplate          : Checking for rethrow: count=1
o.s.retry.support.RetryTemplate          : Retry: count=1
o.s.retry.support.RetryTemplate          : Checking for rethrow: count=2
o.s.retry.support.RetryTemplate          : Retry: count=2
o.s.retry.support.RetryTemplate          : Checking for rethrow: count=3
o.s.retry.support.RetryTemplate          : Retry failed last attempt: count=3
如果您想记录特定的异常,您可以捕获异常并记录它并重试。不幸的是,据我所知,没有办法在框架内记录自定义消息


另一种方法是对负责调用您的方法的实际拦截器进行阴影处理,如
RetryOperationsInterceptor
,但不建议这样做。

您可以注册
RetryListener

@Bean
public List<RetryListener> retryListeners() {
    Logger log = LoggerFactory.getLogger(getClass());

    return Collections.singletonList(new RetryListener() {

        @Override
        public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
            // The 'context.name' attribute has not been set on the context yet. So we have to use reflection.
            Field labelField = ReflectionUtils.findField(callback.getClass(), "val$label");
            ReflectionUtils.makeAccessible(labelField);
            String label = (String) ReflectionUtils.getField(labelField, callback);
            log.trace("Starting retryable method {}", label);
            return true;
        }

        @Override
        public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
            log.warn("Retryable method {} threw {}th exception {}",
                    context.getAttribute("context.name"), context.getRetryCount(), throwable.toString());
        }

        @Override
        public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
            log.trace("Finished retryable method {}", context.getAttribute("context.name"));
        }
    });

是的,'org.springframework.retry.support.RetryTemplate:DEBUG'只输出抽象的重试消息。一旦我启用了实现RetryListenerSupport的bean,我的@Retryable注释方法就不再受到尊重。有什么想法吗?@bforcer您的项目中还有其他RetryListener bean吗?可能它们相互干扰。不,这是唯一实现RetryListener的bean
@Bean
public List<RetryListener> retryListeners() {
    Logger log = LoggerFactory.getLogger(getClass());

    return Collections.singletonList(new RetryListenerSupport() {

        @Override
        public <T, E extends Throwable> void onError(
                RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
            log.warn("Retryable method {} threw {}th exception {}",
                    context.getAttribute("context.name"), 
                    context.getRetryCount(), throwable.toString());
        }
    });
}