将自定义 BackOffPolicy 添加到 spring 时出错-重试
Error in adding a custom BackOffPolicy to spring-retry
我在将自定义 BackOffPolicy 添加到 spring 时遇到错误-重试我遵循了 中的方法。
我的 Retryable 方法是
@Autowired
private RetryOperationsInterceptor retryOperationsInterceptor;
@Override
@Retryable(
interceptor = "retryOperationsInterceptor",
maxAttemptsExpression = "#{${customer.maxRetries:8}}",
value=Exception.class
)
public customer getcustomer(String abc) {
LOGGER.info("Trying to fetch customer details.");
}
@Recover
public customer recover(RuntimeException e, String abc){
LOGGER.info("Recovering - returning safe value");
return null;
}
我的配置文件是
@EnableRetry
public class RetryConfigurations {
@Autowired
private LinearBackoffPolicy linearBackoffPolicy;
@Bean(name = "retryOperationsInterceptor")
RetryOperationsInterceptor retryOperationsInterceptor() {
return RetryInterceptorBuilder.stateless().backOffPolicy(linearBackoffPolicy).build();
}
我定义了一个简单的 linearBackOffPolicy 为
package com.ncr.digitalbanking.prism.connector.configuration;
import org.springframework.retry.backoff.BackOffInterruptedException;
import org.springframework.retry.backoff.Sleeper;
import org.springframework.retry.backoff.SleepingBackOffPolicy;
import org.springframework.retry.backoff.StatelessBackOffPolicy;
import org.springframework.retry.backoff.ThreadWaitSleeper;
import org.springframework.stereotype.Component;
@Component
public class LinearBackoffPolicy extends StatelessBackOffPolicy implements SleepingBackOffPolicy<LinearBackoffPolicy>{
private static final long DEFAULT_BACK_OFF_PERIOD = 1000L;
private volatile long backOffPeriod = DEFAULT_BACK_OFF_PERIOD;
private volatile long backOffDelta = DEFAULT_BACK_OFF_PERIOD;
private Sleeper sleeper = new ThreadWaitSleeper();
public LinearBackoffPolicy withSleeper(Sleeper sleeper) {
LinearBackoffPolicy linearBackoffPolicy = new LinearBackoffPolicy();
linearBackoffPolicy.setBackOffPeriod(backOffPeriod);
linearBackoffPolicy.setSleeper(sleeper);
return linearBackoffPolicy;
}
public long getBackOffPeriod() {
return backOffPeriod;
}
public void setBackOffPeriod(long backOffPeriod) {
this.backOffPeriod = (backOffPeriod > 0? backOffPeriod : 500);
}
public void setSleeper(Sleeper sleeper) {
this.sleeper = sleeper;
}
protected void doBackOff() throws BackOffInterruptedException {
try {
sleeper.sleep(backOffPeriod);
backOffPeriod = backOffPeriod + backOffDelta;
}
catch (InterruptedException e) {
throw new BackOffInterruptedException("Thread interrupted while sleeping", e);
}
}
public String toString() {
return "LinearBackoffPolicy [backOffPeriod=" + backOffPeriod + "]";
}
}
问题
无法在剩余调用中使用线性退避策略。
在所有重试尝试失败后无法调用 Recover 方法。
备注
我的项目使用此配置工作正常
@Retryable(
interceptor = "retryOperationsInterceptor",
maxAttemptsExpression = "#{${prism.cas.maxRetries:8}}",
value=Exception.class
backoff = @Backoff(
delayExpression = "#{${prism.cas.delayExpression:10000}}",
multiplier = 2,
maxDelay = 12000
)
)
我是不是搞砸了什么?
@Recover
不适用于手动配置的拦截器;它仅在框架构建拦截器时有效。
您需要将 .recoverer(...)
添加到您的拦截器构建器。
还有
/**
* Retry interceptor bean name to be applied for retryable method. Is mutually
* exclusive with other attributes.
* @return the retry interceptor bean name
*/
String interceptor() default "";
你的maxAttemptsExpression
被忽略了;最大尝试次数也必须在生成器上进行。
我在将自定义 BackOffPolicy 添加到 spring 时遇到错误-重试我遵循了
我的 Retryable 方法是
@Autowired
private RetryOperationsInterceptor retryOperationsInterceptor;
@Override
@Retryable(
interceptor = "retryOperationsInterceptor",
maxAttemptsExpression = "#{${customer.maxRetries:8}}",
value=Exception.class
)
public customer getcustomer(String abc) {
LOGGER.info("Trying to fetch customer details.");
}
@Recover
public customer recover(RuntimeException e, String abc){
LOGGER.info("Recovering - returning safe value");
return null;
}
我的配置文件是
@EnableRetry
public class RetryConfigurations {
@Autowired
private LinearBackoffPolicy linearBackoffPolicy;
@Bean(name = "retryOperationsInterceptor")
RetryOperationsInterceptor retryOperationsInterceptor() {
return RetryInterceptorBuilder.stateless().backOffPolicy(linearBackoffPolicy).build();
}
我定义了一个简单的 linearBackOffPolicy 为
package com.ncr.digitalbanking.prism.connector.configuration;
import org.springframework.retry.backoff.BackOffInterruptedException;
import org.springframework.retry.backoff.Sleeper;
import org.springframework.retry.backoff.SleepingBackOffPolicy;
import org.springframework.retry.backoff.StatelessBackOffPolicy;
import org.springframework.retry.backoff.ThreadWaitSleeper;
import org.springframework.stereotype.Component;
@Component
public class LinearBackoffPolicy extends StatelessBackOffPolicy implements SleepingBackOffPolicy<LinearBackoffPolicy>{
private static final long DEFAULT_BACK_OFF_PERIOD = 1000L;
private volatile long backOffPeriod = DEFAULT_BACK_OFF_PERIOD;
private volatile long backOffDelta = DEFAULT_BACK_OFF_PERIOD;
private Sleeper sleeper = new ThreadWaitSleeper();
public LinearBackoffPolicy withSleeper(Sleeper sleeper) {
LinearBackoffPolicy linearBackoffPolicy = new LinearBackoffPolicy();
linearBackoffPolicy.setBackOffPeriod(backOffPeriod);
linearBackoffPolicy.setSleeper(sleeper);
return linearBackoffPolicy;
}
public long getBackOffPeriod() {
return backOffPeriod;
}
public void setBackOffPeriod(long backOffPeriod) {
this.backOffPeriod = (backOffPeriod > 0? backOffPeriod : 500);
}
public void setSleeper(Sleeper sleeper) {
this.sleeper = sleeper;
}
protected void doBackOff() throws BackOffInterruptedException {
try {
sleeper.sleep(backOffPeriod);
backOffPeriod = backOffPeriod + backOffDelta;
}
catch (InterruptedException e) {
throw new BackOffInterruptedException("Thread interrupted while sleeping", e);
}
}
public String toString() {
return "LinearBackoffPolicy [backOffPeriod=" + backOffPeriod + "]";
}
}
问题 无法在剩余调用中使用线性退避策略。 在所有重试尝试失败后无法调用 Recover 方法。 备注 我的项目使用此配置工作正常
@Retryable(
interceptor = "retryOperationsInterceptor",
maxAttemptsExpression = "#{${prism.cas.maxRetries:8}}",
value=Exception.class
backoff = @Backoff(
delayExpression = "#{${prism.cas.delayExpression:10000}}",
multiplier = 2,
maxDelay = 12000
)
)
我是不是搞砸了什么?
@Recover
不适用于手动配置的拦截器;它仅在框架构建拦截器时有效。
您需要将 .recoverer(...)
添加到您的拦截器构建器。
还有
/**
* Retry interceptor bean name to be applied for retryable method. Is mutually
* exclusive with other attributes.
* @return the retry interceptor bean name
*/
String interceptor() default "";
你的maxAttemptsExpression
被忽略了;最大尝试次数也必须在生成器上进行。