java - Spring Retry 不起作用并收到 maxAttemptsExpression 值的异常

标签 java spring-mvc websphere websphere-portal spring-retry

我正在使用 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): Type conversion problem, cannot convert from 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

关于java - Spring Retry 不起作用并收到 maxAttemptsExpression 值的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48309090/

相关文章:

java - Android AsyncTask 的 onProgressUpdate 不工作

spring-mvc - 如何使@PreAuthorize 具有比@Valid 或@Validated 更高的优先级

java - 如何监控 Websphere 6 中的 HTTP 出站连接池使用情况?

websphere - 将共享库附加到 WebSphere 应用程序的模块

Java - 限制扫描仪仅接受 double

java - 如何获取特定周内特定日期的日期

java - 从 Java 使用 groovy.util.AntBuilder

java - 我怎样才能转到 :text in Thymleaf? 内的下一行

spring - bean 名称 'login' 的 BindingResult 和普通目标对象都不能用作请求属性

java - 非法参数异常 : Unsupported ciphersuite