从application.properties文件中读取spring @Retryable的maxAttempts

时间:2016-06-23 22:43:05

标签: java spring spring-retry

@Retryable(value = Exception.class, maxAttempts = 3)
public Boolean sendMessageService(Request request){
   ...
}

@Retryable注释中的maxAttempts参数是硬编码的。我可以从application.properties文件中读取该值吗?

类似

@Retryable(value = Exception.class, maxAttempts = "${MAX_ATTEMPTS}")

2 个答案:

答案 0 :(得分:1)

没有;使用注释时无法通过属性进行设置。

您可以手动连接RetryOperationsInterceptor bean并使用Spring AOP将其应用于您的方法......

修改

<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是您要将重试应用于的方法。

答案 1 :(得分:0)

是的,您可以使用maxAttemptsExpression:

@Retryable(value = {Exception.class},         maxAttemptsExpression =“#{$ {maxAttempts}}”)

如果尚未在课程上方添加@EnableRetry。