Sab*_*ish 12 java spring spring-retry
我有这段代码
@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 {
Run Code Online (Sandbox Code Playgroud)
//这里的一些实现}
有没有办法可以使用@Value配置maxAttempts,延迟和乘数?或者是否有任何其他方法可以使注释中的这些字段可配置?
Sat*_*ish 18
随着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
}
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请参阅:https://github.com/spring-projects/spring-retry/blob/master/README.md
如果要提供默认值,然后有选择地在application.properties文件中覆盖它:
@Retryable(maxAttemptsExpression = "#{${my.max.attempts:10}}")
public void myRetryableMethod() {
// ...
}
Run Code Online (Sandbox Code Playgroud)
小智 5
正如这里所解释的: /sf/answers/3020084511/
版本 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 {
Run Code Online (Sandbox Code Playgroud)
目前还不可能;要连接属性,必须将注释更改为采用字符串值,并且注释 bean 后处理器必须解析占位符和/或 SpEL 表达式。
请参阅此答案以获取替代方案,但目前无法通过注释完成。
编辑
<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>
Run Code Online (Sandbox Code Playgroud)
EchoService.test您要应用重试的方法在哪里。
| 归档时间: |
|
| 查看次数: |
17283 次 |
| 最近记录: |