如何在 Spring RequestHandlerRetryAdvice 中以指数方式增加重试间隔
How to exponentially increase the interval for retries in Spring RequestHandlerRetryAdvice
重试指定如下:
<bean id="retryAdvice"
class="org.springframework.integration.handler.advice.RequestHandlerRetryAdvice" >
<property name="retryTemplate">
<bean class="org.springframework.retry.support.RetryTemplate">
<property name="backOffPolicy">
<bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
<property name="initialInterval" value="5000"/>
<property name="multiplier" value="3.0"/>
</bean>
</property>
<property name="retryPolicy">
<bean class="org.springframework.retry.policy.SimpleRetryPolicy">
<property name="maxAttempts" value="6" />
</bean>
</property>
</bean>
</property>
</bean>
我预计每次尝试间隔都会增加 3 倍。实际上我得到了以下间隔:
5、15、30、30、30 秒。请参阅下面的确切时间和间隔。
我指定的错误是什么?
2016-05-10 10:07:49,714 - 0
2016-05-10 10:07:55,085 - 5.2
2016-05-10 10:08:10,457 - 15.4
2016-05-10 10:08:40,830 - 30.4
2016-05-10 10:09:11,230 - 30.4
2016-05-10 10:09:41,639 - 30.4
有maxInterval
要考虑:
/**
* Setter for maximum back off period. Default is 30000 (30 seconds). the
* value will be reset to 1 if this method is called with a value less than
* 1. Set this to avoid infinite waits if backing off a large number of
* times (or if the multiplier is set too high).
*
* @param maxInterval in milliseconds.
*/
public void setMaxInterval(long maxInterval) {
this.maxInterval = maxInterval > 0 ? maxInterval : 1;
}
因此,当您的 ExponentialBackOffPolicy
达到 30 secs
(默认)时,它会停止使用 multiplier
。
增加最大间隔以允许乘数生效。
<bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
<property name="retryPolicy">
<bean class="RetryPolicy"
p:skipRetryFaults="${retry.skipForFaults}"
p:maxAttempts="${retry.maxAttempts}"/>
</property>
<property name="backOffPolicy">
<bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
<property name="initialInterval" value="${backoff.initialInterval}" />
<property name="maxInterval" value="${backoff.maxInterval}" />
<property name="multiplier" value="${backoff.multiplier}" />
</bean>
</property>
</bean>
重试指定如下:
<bean id="retryAdvice"
class="org.springframework.integration.handler.advice.RequestHandlerRetryAdvice" >
<property name="retryTemplate">
<bean class="org.springframework.retry.support.RetryTemplate">
<property name="backOffPolicy">
<bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
<property name="initialInterval" value="5000"/>
<property name="multiplier" value="3.0"/>
</bean>
</property>
<property name="retryPolicy">
<bean class="org.springframework.retry.policy.SimpleRetryPolicy">
<property name="maxAttempts" value="6" />
</bean>
</property>
</bean>
</property>
</bean>
我预计每次尝试间隔都会增加 3 倍。实际上我得到了以下间隔: 5、15、30、30、30 秒。请参阅下面的确切时间和间隔。 我指定的错误是什么?
2016-05-10 10:07:49,714 - 0
2016-05-10 10:07:55,085 - 5.2
2016-05-10 10:08:10,457 - 15.4
2016-05-10 10:08:40,830 - 30.4
2016-05-10 10:09:11,230 - 30.4
2016-05-10 10:09:41,639 - 30.4
有maxInterval
要考虑:
/**
* Setter for maximum back off period. Default is 30000 (30 seconds). the
* value will be reset to 1 if this method is called with a value less than
* 1. Set this to avoid infinite waits if backing off a large number of
* times (or if the multiplier is set too high).
*
* @param maxInterval in milliseconds.
*/
public void setMaxInterval(long maxInterval) {
this.maxInterval = maxInterval > 0 ? maxInterval : 1;
}
因此,当您的 ExponentialBackOffPolicy
达到 30 secs
(默认)时,它会停止使用 multiplier
。
增加最大间隔以允许乘数生效。
<bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
<property name="retryPolicy">
<bean class="RetryPolicy"
p:skipRetryFaults="${retry.skipForFaults}"
p:maxAttempts="${retry.maxAttempts}"/>
</property>
<property name="backOffPolicy">
<bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
<property name="initialInterval" value="${backoff.initialInterval}" />
<property name="maxInterval" value="${backoff.maxInterval}" />
<property name="multiplier" value="${backoff.multiplier}" />
</bean>
</property>
</bean>