我正在使用 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/