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

nag*_*dra 3 spring-integration spring-integration-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;
}
Run Code Online (Sandbox Code Playgroud)

Art*_*lan 7

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) {
Run Code Online (Sandbox Code Playgroud)

其中一项建议位于 Spring Integration 框中 - 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(); 
}
Run Code Online (Sandbox Code Playgroud)