如何使spring @retryable可配置? [英] How can I make spring @retryable configurable?
问题描述
我有这段代码
@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 {
//这里的一些实施
}
//some implementation here }
有吗我可以使用@Value配置maxAttempts,延迟和乘数的方法吗?
或者是否有任何其他方法可以使注释中的这些字段可配置?
Is there a way I can make the maxAttempts , delay and multiplier configurable using @Value? Or is there any other approach to make such fields inside annotations configurable?
推荐答案
目前不可能;要连接属性,必须更改注释以获取字符串值,并且注释bean后处理器必须解析占位符和/或SpEL表达式。
It's not currently possible; to wire in properties, the annotation would have to be changed to take String values and the annotation bean post-processor would have to resolve placeholders and/or SpEL expressions.
请参阅此答案替代方案,但目前无法通过注释完成。
See this answer for an alternative, but it can't currently be done via the annotation.
编辑
<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
是你的方法想要重试。
Where EchoService.test
is the method you want to apply retries to.
这篇关于如何使spring @retryable可配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!