Spring 重试不起作用,并得到 maxTrysExpression 值的异常



我在服务方法中使用spring-retry-1.2.0.RELEASE.jar并使用下面的Retryable注释

@Retryable(value = {CustomException.class}, 
            maxAttemptsExpression = "#{'${max.retry.attempts}'}", 
            backoff = @Backoff(delayExpression = "#{'${retry.delay}'}"))

在日志中看到以下异常maxAttemptsExpression由于值和相同的错误,我们也使用Interger.ParseInt/Interger.ValueOf

org.springframework.expression.spel.SpelEvaluationException: EL1001E:(pos 0(:类型转换问题,无法从 java.lang.String to java.lang.Integer

我只在少数@Retryable服务方法上看到此异常,其余@Retryable方法工作正常。我不知道这里发生了什么,我们在点击注释之前也看到了价值

删除#{''}(包括单引号(。

@Retryable(maxAttemptsExpression = "${max.retry.attempts}")

这里不需要#{...}

您还应该升级到 1.2.1.RELEASE。

编辑

一定还有其他事情发生;这两种形式对我来说都很好用......

@SpringBootApplication
@EnableRetry
public class So48309090Application {
    public static void main(String[] args) {
        SpringApplication.run(So48309090Application.class, args);
    }
    @Bean
    public ApplicationRunner runner(Foo foo) {
        return args -> {
            try {
                foo.foo();
            }
            catch (RuntimeException e) {
            }
            try {
                foo.bar();
            }
            catch (RuntimeException e) {
            }
        };
    }
    @Component
    public static class Foo {
        @Retryable(maxAttemptsExpression = "${max.attempts}")
        public void foo() {
            System.out.println("foo");
            throw new RuntimeException("c");
        }
        @Retryable(maxAttemptsExpression = "#{'${max.attempts}'}")
        public void bar() {
            System.out.println("bar");
            throw new RuntimeException("c");
        }
    }
}

应用程序属性

max.attempts=5

foo
foo
foo
foo
foo
bar
bar
bar
bar
bar

EL1001E也是找不到属性时引发的错误。尝试使用 @Value 将属性绑定到实例变量,并检查值是否确实是从属性中正确读取的。

相关内容

  • 没有找到相关文章

最新更新