我通过Http.outboundGateway向网络服务发送请求,我希望对这三种情况做出回复
1-回应成功[确定]
2-连接失败[需要重试]
3-响应返回错误代码,例如400 [保存]
我在poller之后使用advice()来重新尝试连接失败,但问题是在两种情况下都抛出了错误消息异常(连接失败,响应错误代码),因此两种情况都调用了重试
我如何区分它们并仅使用重试建议来解决连接失败问题
.handle(
Http.outboundGateway(propertiesConfig.getURL())
......
, endpoint -> endpoint
.poller(Pollers.fixedDelay(delayBetweenRequests)
.errorChannel("errorChannel")
.taskExecutor(executor)
.receiveTimeout(timeoutDelay)
)
.advice(retryAdvice)
)
重试建议创建bean
@Bean("retryAdvice")
public RequestHandlerRetryAdvice maspRetryAdvice() {
Request
HandlerRetryAdvice retryAdvice = new RequestHandlerRetryAdvice();
RetryTemplate retryTemplate = new RetryTemplate();
FixedBackOffPolicy policy = new FixedBackOffPolicy();
policy.setBackOffPeriod(interval);
retryTemplate.setBackOffPolicy(policy);
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(nRetry);
retryTemplate.setRetryPolicy(retryPolicy);
retryAdvice.setRetryTemplate(retryTemplate);
ErrorMessageSendingRecoverer recover = new
ErrorMessageSendingRecoverer(aggregatorChannel());
handlerRetryAdvice.setRecoveryCallback(recover);
return retryAdvice;
}
答案 0 :(得分:0)
我几乎面临同样的挑战。 对于连接超时,此链接提供了确切的解决方案:[为Http.outboundGateway spring dsl配置错误处理和重试 ] [1]:Configure error handling and retry for Http.outboundGateway spring dsl
.... .handle(Http.outboundGateway( 解析器()。parseExpression( “报头[URL]”))
.httpMethod(HttpMethod.POST)
.headerMapper(headerMapper())
.expectedResponseType(String.class)
.requestFactory(clientHttpRequestFactory())
// The inner writer method is doing nothing, just place holder for future usage,
// errorHandler is necessary to capture :
// e.g. org.springframework.web.client.HttpClientErrorException: 403 Forbidden
.errorHandler(responseErrorFileWriter())
, (Consumer<GenericEndpointSpec>)e -> e.advice(retryAdvice()))