我有这段代码
@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,延迟和乘数?
或者是否有任何其他方法可以使注释中的这些字段可配置?
解决方法
目前还不可能;要在属性中连接,必须更改注释以获取字符串值,并且注释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是您要应用重试的方法.