问题描述
我遇到的情况是必须点击一个API并发布对该API的响应ID。
如果该API已关闭,我需要每5分钟重试一次,以检查API是否已启动并发送响应ID。
虽然API保持低速状态,但是响应ID可能会增加,因此我必须将所有响应合并为一个响应,并在API启动时发送。
我可以使用@Retryable重试,但不允许我提供业务逻辑以合并数据。
请向我建议一种达到目标的方法。
RetryableConfig:
@Configuration
@EnableRetry
@Slf4j
public class CircuitBreaker {
@Bean
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(2000l);
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(2);
retryTemplate.setRetryPolicy(retryPolicy);
retryTemplate.registerListener(new DefaultListenerSupport());
return retryTemplate;
}
private static class DefaultListenerSupport extends RetryListenerSupport {
@Override
public <T,E extends Throwable> void onError(RetryContext context,RetryCallback<T,E> callback,Throwable throwable) {
log.info("Remote call failed,Will retry (count = " + context.getRetryCount()+" ) exception :" +throwable.getClass().getSimpleName());
super.onError(context,callback,throwable);
}
@Override
public <T,E extends Throwable> void close(final RetryContext context,final RetryCallback<T,final Throwable throwable) {
if (throwable != null) {
log.info("Final retry attempt failed,",throwable);
}
}
}
}
服务:
@Retryable( value = Exception.class,maxAttempts = 2,backoff = @Backoff(delay = 2000))
public String notify(String responseId)
{
Boolean flag = true;
SupplierQuoteIDResponseBO supplierQuoteIDResponseBO = new SupplierQuoteIDResponseBO();
supplierQuoteIDResponseBO.getSupplierQuoteResponse().setResponseId(responseId);
//retryTemplate.execute(retryContext->fuzzyClientHandler.getTestData(supplierQuoteIDResponseBO));
retryTemplate.execute(new RetryCallback<Object,RuntimeException>() {
@Override
public Object doWithRetry(RetryContext context) {
log.info("retry COunt print {}",context.getRetryCount());
return fuzzyClientHandler.getTestData(supplierQuoteIDResponseBO);
}
});
return "response from flow : ";
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)