问题
@Retryable(value = Exception.class, maxAttempts = 3)
public Boolean sendMessageService(Request request){
...
}
maxAttempts argument in @Retryable
annotation is hard coded. Can i read that value from application.properties
file?
something like
@Retryable(value = Exception.class, maxAttempts = "${MAX_ATTEMPTS}")
回答1:
No; it's not possible to set via a property when using the annotation.
You can wire up the RetryOperationsInterceptor
bean manually and apply it to your method using Spring AOP...
EDIT
<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>
Where EchoService.test
is the method you want to apply retries to.
回答2:
Yes, you can by using maxAttemptsExpression:
@Retryable(value = {Exception.class}, maxAttemptsExpression = "#{${maxAttempts}}")
Add @EnableRetry above your class if you have not already done so.
来源:https://stackoverflow.com/questions/38003086/read-maxattempts-of-spring-retryable-from-application-properties-file