0

我正在通过 Http.outboundGateway 向 Web 服务发送请求,并且我希望在这三种情况下得到响应

1-响应成功[确定]

2-连接失败[需要重试]

3-带有错误代码的响应返回,例如。400 [保存它]

我在轮询器之后使用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;
} 
4

1 回答 1

0

我面临着几乎完全相同的挑战。对于连接超时,此链接提供了确切的解决方案:[Configure error handling and retry for Http.outboundGateway spring dsl] [1]: Configure error handling and retry for Http.outboundGateway spring dsl

.... .handle(Http.outboundGateway(parser().parseExpression("headers[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()))
于 2018-06-04T18:10:26.090 回答