18

我有这段代码

@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 、 delay 和 multiplier 可配置?或者有没有其他方法可以使注释中的这些字段可配置?

4

5 回答 5

28

随着 Spring-retry 1.2 版的发布,这是可能的。@Retryable 可以使用 SPEL 进行配置。

@Retryable(
    value = { SomeException.class,AnotherException.class },
    maxAttemptsExpression = "#{@myBean.getMyProperties('retryCount')}",
    backoff = @Backoff(delayExpression = "#{@myBean.getMyProperties('retryInitalInterval')}"))
public void doJob(){
    //your code here
}

有关更多详细信息,请参阅:https ://github.com/spring-projects/spring-retry/blob/master/README.md

于 2017-01-23T14:39:21.907 回答
9

如果您想提供默认值,然后可以选择在 application.properties 文件中覆盖它:

@Retryable(maxAttemptsExpression = "#{${my.max.attempts:10}}")
public void myRetryableMethod() {
    // ...
}
于 2018-10-09T16:01:01.033 回答
3

正如这里所解释的: https ://stackoverflow.com/a/43144064

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 {
于 2017-10-02T18:39:32.317 回答
2

目前不可能;要连接属性,必须将注释更改为采用字符串值,并且注释 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>

EchoService.test您要应用重试的方法在哪里。

于 2016-06-29T11:50:12.523 回答
2

您可以使用RetryTemplatebean 代替@Retryable注释,如下所示:

@Value("${retry.max-attempts}")
private int maxAttempts;
@Value("${retry.delay}")
private long delay;

@Bean
public RetryTemplate retryTemplate() {
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
    retryPolicy.setMaxAttempts(maxAttempts);

    FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
    backOffPolicy.setBackOffPeriod(delay);

    RetryTemplate template = new RetryTemplate();
    template.setRetryPolicy(retryPolicy);
    template.setBackOffPolicy(backOffPolicy);
    return template;
}

然后使用execute这个模板的方法:

@Autowired
private RetryTemplate retryTemplate;

public ResponseVo doSomething(final Object data) {
    RetryCallback<ResponseVo, SomeException> retryCallback = new RetryCallback<ResponseVo, SomeException>() {
        @Override
        public ResponseVo doWithRetry(RetryContext context) throws SomeException {
             // do the business
             return responseVo;
        }
    };
    return retryTemplate.execute(retryCallback);
}
于 2017-01-17T03:52:35.450 回答