Search code examples
spring-integrationspring-integration-dsl

Spring Integration Java DSL: How to route the flow to the error channel if there occurs an error


How to route the flow from the channel myChannel to the error channel myErrorChannel if there occurs an error in the Http.outboundGateway call?

@Bean
private IntegrationFlow myChannel() {
    return f -> f
            .handle(Http.outboundGateway("http://localhost:8080/greeting")
                    ...
                    .expectedResponseType(String.class));
}

@Bean
private IntegrationFlow myErrorChannel() {
    return f -> f
            ...
}

In the error handler I will wrap the error message inside my custom JSON and I will send that as a part of the normal flow back to the source system.

Is this a good way to handle errors in the Spring Integration Java DSL?


Solution

  • You can use an ExpressionEvaluatingRequestHandlerAdvice with its returnFailureExpressionResult = true and use it in the second argument of the .handle(..., e -> e.advice(...)).

    You configure that advice for the onFailureExpression to be able to return something meaningful. If you still think that you need to send to the channel and get reply back, then you need to have a @MessagingGateway and use it in that onFailureExpression to send and receive. The normal failureChannel configuration in the ExpressionEvaluatingRequestHandlerAdvice doesn't expect reply.

    Another approach can be done using the same @MessagingGateway, but in front of that myChannel IntegrationFlow. Then you can configure that gateway for the errorChannel and here a reply from the error flow is expected.