为 Http.outboundGateway spring dsl 配置错误处理和重试

Configure error handling and retry for Http.outboundGateway spring dsl

我有一个要求,当它失败时我需要进行休息呼叫我必须重试 3 次并且根据收到的状态代码我需要执行不同的操作,我找不到合适的 spring 集成 dsl 示例。如何配置错误处理程序并重试

@Bean
public IntegrationFlow performCreate() {
    return IntegrationFlows.from("createFlow")
            .handle(Http.outboundGateway("http://localhost:8080/create")
                    .httpMethod(HttpMethod.GET)
                    .expectedResponseType(String.class)
                    .requestFactory(simpleClientHttpRequestFactory())
                    .errorHandler(??)
            )

            .log(LoggingHandler.Level.DEBUG, "response", m -> m.getPayload())
            .log(LoggingHandler.Level.DEBUG, "response", m -> m.getHeaders())
            .get();
}

private SimpleClientHttpRequestFactory simpleClientHttpRequestFactory() {
    SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
    simpleClientHttpRequestFactory.setReadTimeout(5000);
    simpleClientHttpRequestFactory.setConnectTimeout(5000);
    return simpleClientHttpRequestFactory;
}

Java DSL .handle() 有第二个参数 - Consumer<GenericEndpointSpec<?>> 并且可以配置为:

/**
 * Configure a list of {@link Advice} objects to be applied, in nested order, to the
 * endpoint's handler. The advice objects are applied only to the handler.
 * @param advice the advice chain.
 * @return the endpoint spec.
 */
public S advice(Advice... advice) {

其中一个建议在 Spring 集成框中 - RequestHandlerRetryAdvicehttps://docs.spring.io/spring-integration/docs/5.0.4.RELEASE/reference/html/messaging-endpoints-chapter.html#retry-advice

https://docs.spring.io/spring-integration/docs/5.0.4.RELEASE/reference/html/java-dsl.html#java-dsl-endpoints

.handle(Http.outboundGateway("http://localhost:8080/create")
                .httpMethod(HttpMethod.GET)
                .expectedResponseType(String.class)
                .requestFactory(simpleClientHttpRequestFactory()),
           e -> e.advice(retryAdvice())

...

@Bean
public RequestHandlerRetryAdvice retryAdvice() {
    RequestHandlerRetryAdvice requestHandlerRetryAdvice = new RequestHandlerRetryAdvice();
    requestHandlerRetryAdvice.setRecoveryCallback(errorMessageSendingRecoverer());
    return requestHandlerRetryAdvice;
}

@Bean
public ErrorMessageSendingRecoverer errorMessageSendingRecoverer() {
    return new ErrorMessageSendingRecoverer(recoveryChannel())
}

@Bean
public MessageChannel recoveryChannel() {
    return new DirectChannel();
}

@Bean
public IntegrationFlow handleRecovery() { 
     return IntegrationFlows.from("recoveryChannel")
                   .log(LoggingHandler.Level.ERROR, "error", 
                        m -> m.getPayload())
                   .get(); 
}