Search code examples
spring-integrationspring-integration-dsl

How to use custom error channel and WebFlux.inboundGateway to reply with an error response?


Following the two links below I've created an IntegrationFlow which upon an error, calls into the custom errorFlow. However the behaviour I witness is that the application never replies to the client, it just hangs. How can I reply back to the request from the errorFlow? For reference I've hosted my sample on github.

    @Bean
    public IntegrationFlow mainFlow() {
        return IntegrationFlows.from(WebFlux.inboundGateway(URI)
                .errorChannel(customErrorChannel()))
                .channel(MessageChannels.flux()) //Work around: https://github.com/spring-projects/spring-integration/issues/3276
                .transform(p -> {
                    throw new RuntimeException("Error!");
                    //return "Ok Response"; //If we comment the throw and uncomment this, then the the code replies to the request ok.
                })
                .get();
    }

    @Bean
    public PublishSubscribeChannel customErrorChannel() {
        return MessageChannels.publishSubscribe().get();
    }

    @Bean
    public IntegrationFlow errorFlow() {
        return IntegrationFlows.from("customErrorChannel")
                .transform(p -> {
                    return "Error Response";
                })
                .get();
    }

Request...

GET http://localhost:8080/foo

Request screenshot


Solution

  • Turns out there is still a problem with that logic.

    We need to investigate it more and figure out the fix.

    Meanwhile you could use a workaround for your failing transformer with the ExpressionEvaluatingRequestHandlerAdvice and handle error similar way in the errorFlow(). See returnFailureExpressionResult as true. And your onFailureExpression should be kinda a gateway call to that customErrorChannel. Or you can use a MessagingTemplate.sendAndReceive() API from that expression instead of gateway.

    See more in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#message-handler-advice-chain