Java Spring重试复合策略不起作用
我正在使用具有以下RetryTemplate配置的模块:Java Spring重试复合策略不起作用,java,spring,soap,spring-retry,Java,Spring,Soap,Spring Retry,我正在使用具有以下RetryTemplate配置的模块: @EnableRetry @Configuration public class RetryConfig { @Bean public RetryTemplate retryTemplate() { final FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy(); backOffPolicy.setBackOffPerio
@EnableRetry
@Configuration
public class RetryConfig {
@Bean
public RetryTemplate retryTemplate() {
final FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(500);
final SimpleRetryPolicy attemptsPolicy = new SimpleRetryPolicy();
attemptsPolicy.setMaxAttempts(2);
final TimeoutRetryPolicy timeoutPolicy = new TimeoutRetryPolicy();
timeoutPolicy.setTimeout(2000);
final CompositeRetryPolicy retryPolicy = new CompositeRetryPolicy();
retryPolicy.setPolicies(new RetryPolicy[] {timeoutPolicy, attemptsPolicy});
final RetryTemplate template = new RetryTemplate();
template.setBackOffPolicy(backOffPolicy);
template.setRetryPolicy(retryPolicy);
return template;
}
}
但是TimeoutRetryPolicy(用于CompositeRetryPolicy实例)显然不起作用
我正在注入RetryTemplate以使用SOAP服务,在某些情况下,响应需要10秒以上。但是,通过配置,我认为不应超过4秒(2秒超时*2次尝试)。
提前非常感谢 我遇到了同样的问题,不幸的是无法使用spring重试解决。因此,我实现了自己的泛型类:(gist-link) 它的用途非常简单:
try {
final int attempts = 2;
final long timeout = 2000;
final String foo = new RetryTemplate<String>(attempts, timeout).execute(() -> {
// Your retryable logic here!
return "Lorem ipsum";
});
} catch (RetryException retryExpectedError) {
// Your logic if the re-attempts is exceeded.
// Note: RetryException is a simple inheritance of RuntimeException.
}
试试看{
最终int尝试=2;
最终长超时=2000;
最后一个字符串foo=newretrytemplate(尝试,超时)。执行(()->{
//你的逻辑在这里!
返回“Lorem ipsum”;
});
}捕获(RetryException retryExpectedError){
//如果超过重新尝试,则返回您的逻辑。
//注意:RetryException是RuntimeException的简单继承。
}
虽然我还没有实现控件回退,但这将是微不足道的。我希望有帮助 首先是测试服务
@Service
public class RetrySoapServiceImpl implements RetryCallback<YourClass, YourException>{
static private int i = 0;
@Retryable(
value = { YourException.class },
maxAttempts = 300 //Just an Example. Try it with 6.
@Override
public PartnerWertelistenResponse doWithRetry(RetryContext arg0) throws YourException {
System.out.println("Attempt "+i);
i++;
if (i<300) throw new YourException();
else {
System.out.println("No Exception");
return null;
}
}
}
作品非常感谢你!
RetryTemplate template = new RetryTemplate();
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(5000); //You'll become mesages every 5 Seconds.
TimeoutRetryPolicy policy = new TimeoutRetryPolicy();
policy.setTimeout(50000L); //you'll be back in 50 Seconds, so you schould'nt wait 300*5 Sec.
template.setRetryPolicy(policy); //It's the last point
template.setBackOffPolicy(backOffPolicy); //It's the time to repeat an attempt
try {
YourClass result2 = template.execute(new RetrySoapServiceImpl());
} catch (YourException e) {
System.out.println("parent Catch Attempt "+e.getClass().getName());
}
System.out.println("ready to return");