我有这段代码
@Retryable(maxAttempts = 3, stateful = true, include = ServiceUnavailableException.class,
exclude = URISyntaxException.class, backoff = @Backoff(delay = 1000, multiplier = 2) )
public void testThatService(String serviceAccountId)
throws ServiceUnavailableException, URISyntaxException {
//这里有一些实现 }
有没有办法可以使用@Value配置maxAttempts,延迟和乘数? 或者是否有其他方法可以使注释中的这些字段可配置?
答案 0 :(得分:15)
随着Spring-retry 1.2版的发布,它是可能的。可以使用SPEL配置@Retryable。
@Retryable(
value = { SomeException.class,AnotherException.class },
maxAttemptsExpression = "#{@myBean.getMyProperties('retryCount')}",
backoff = @Backoff(delayExpression = "#{@myBean.getMyProperties('retryInitalInterval')}"))
public void doJob(){
//your code here
}
有关详细信息,请参阅:https://github.com/spring-projects/spring-retry/blob/master/README.md
答案 1 :(得分:3)
如果您想提供一个默认值,然后有选择地在application.properties文件中覆盖它:
@Retryable(maxAttemptsExpression = "#{${my.max.attempts:10}}")
public void myRetryableMethod() {
// ...
}
答案 2 :(得分:2)
目前不可能;要在属性中连接,必须更改注释以获取字符串值,并且注释bean后处理器必须解析占位符和/或SpEL表达式。
有关替代方案,请参阅this answer,但目前无法通过注释完成。
修改强>
<bean id="retryAdvice" class="org.springframework.retry.interceptor.RetryOperationsInterceptor">
<property name="retryOperations">
<bean class="org.springframework.retry.support.RetryTemplate">
<property name="retryPolicy">
<bean class="org.springframework.retry.policy.SimpleRetryPolicy">
<property name="maxAttempts" value="${max.attempts}" />
</bean>
</property>
<property name="backOffPolicy">
<bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
<property name="initialInterval" value="${delay}" />
<property name="multiplier" value="${multiplier}" />
</bean>
</property>
</bean>
</property>
</bean>
<aop:config>
<aop:pointcut id="retries"
expression="execution(* org..EchoService.test(..))" />
<aop:advisor pointcut-ref="retries" advice-ref="retryAdvice"
order="-1" />
</aop:config>
EchoService.test
是您要将重试应用于的方法。
答案 3 :(得分:2)
您可以使用RetryTemplate
bean代替@Retryable
注释,如下所示:
@Value("${retry.max-attempts}")
private int maxAttempts;
@Value("${retry.delay}")
private long delay;
@Bean
public RetryTemplate retryTemplate() {
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(maxAttempts);
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(delay);
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(retryPolicy);
template.setBackOffPolicy(backOffPolicy);
return template;
}
然后使用此模板的execute
方法:
@Autowired
private RetryTemplate retryTemplate;
public ResponseVo doSomething(final Object data) {
RetryCallback<ResponseVo, SomeException> retryCallback = new RetryCallback<ResponseVo, SomeException>() {
@Override
public ResponseVo doWithRetry(RetryContext context) throws SomeException {
// do the business
return responseVo;
}
};
return retryTemplate.execute(retryCallback);
}
答案 4 :(得分:1)
正如这里解释的那样: https://stackoverflow.com/a/43144064
1.2版引入了为某些属性使用表达式的功能。
所以你需要这样的东西:
@Retryable(maxAttempts = 3, stateful = true, include = ServiceUnavailableException.class,
exclude = URISyntaxException.class, backoff = @Backoff(delayExpression = "#{${your.delay}}" , multiplier = 2) )
public void testThatService(String serviceAccountId)
throws ServiceUnavailableException, URISyntaxException {