【问题标题】:Spring retryTemplate fires too early if no maxDelay is set如果没有设置 maxDelay,Spring retryTemplate 会过早触发
【发布时间】:2017-03-17 12:37:45
【问题描述】:

我希望我的 RetryTemplate 延迟 300000 毫秒 - 每次尝试 5 分钟乘以 1.1。但是,它只会将下一次尝试延迟 30 秒。这是一个简化的示例:

@Service
public class Foo {
    private static final Logger log = Logger.getLogger(Foo.class);

    @Retryable(value = Exception.class,
            backoff = @Backoff(delay = 300000, multiplier = 1.1))
    public void run() throws Exception {
        new Bar().run();
    }

    @Recover
    void recover(Exception e) {
        log.error("e", e);
    }
}

public class Bar {
    private static final Logger log = Logger.getLogger(Bar.class);

    public void run() throws Exception{
        log.info("hier");
        throw new Exception();
    }
}

@Bean
CommandLineRunner runner() {
    return (String[] a) -> {
            scheduler.schedule(() -> {
                try {
                    retryTemplate.execute(arg -> {
                        foo.run();
                        return null;
                    });
                } catch (Exception e) {

                }
            }, 1 , TimeUnit.SECONDS);
    };
}

日志是在这些时间制作的:

2017-03-17 13:25:08.439  INFO 6500 --- [ 
2017-03-17 13:25:38.439  INFO 6500 --- [ 
2017-03-17 13:26:08.440  INFO 6500 --- [ 
2017-03-17 13:26:08.444 ERROR 6500 --- [ 

无论我使用调度器还是不使用调度器都没有区别(因为原始代码需要初始延迟。)

现在,如果我将 maxDelay 添加到 @Backoff

@Backoff(delay = 300000, multiplier = 1.1, maxDelay = 1000000000)

它完全按照预期工作 - 5 分钟后触发,然后 5*1.1 分钟后触发,依此类推。但是阅读@Backoff - maxDelay的javadoc,它说它默认为0并且如果小于延迟则被忽略

/**
     * The maximimum wait (in milliseconds) between retries. If less than the
     * {@link #delay()} then ignored.
     * 
     * @return the maximum delay between retries (default 0 = ignored)
     */
    long maxDelay() default 0;

有什么我不明白的吗?似乎 maxDelay 默认为 30 秒,这与 javadoc 解释的完全不同。

使用的 spring-retry 版本是 1.1.3

【问题讨论】:

    标签: java spring spring-retry


    【解决方案1】:

    这是 javadocs 中的一个错误 - 请参阅代码 here

    policy.setMaxInterval(max > min ? max : ExponentialBackOffPolicy.DEFAULT_MAX_INTERVAL);
    

    public static final long DEFAULT_MAX_INTERVAL = 30000L;
    

    【讨论】:

      猜你喜欢
      • 2019-09-27
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多