场景:我需要在运行时修改@Retryable
的maxAttempts值,以便可以从数据库驱动重试次数
@Service
public class PropertyHolder {
private int retryCount= 2;
public int retryCount() { return retryCount; }
@Scheduled(fixedRate=3600000) //Query DB and update retryCount every 60mins
private void updateRetryCount(int in) {
this.retryCount = 0; //Fetch retryCount from DB and update retryCount
}
}
@Service
public class SimpleService{
@Retryable( value={ Throwable.class }, maxAttemptsExpression="#{@propertyHolder.retryCount()}")
private void performTask() {
// do some opertion that throws Exception
}
}
PropertyHolder
将每60分钟更新retryCount
一次
此PropertHolder#retryCount
需要连接到SimpleService#performTask
中的@Retryable
目前,它只取retryCount(2(的初始值
这是一个正确的方法吗?还是我犯了一些可怕的错误?
否;当前表达式仅在上下文初始化期间进行求值;存在添加运行时评估的开放特性请求。
https://github.com/spring-projects/spring-retry/issues/184
目前,您必须将自己的拦截器与可变重试策略连接起来,并通过@Retryable
中的interceptor
属性对其进行配置。