Search code examples
javaspringspring-integration

Spring Integration and global default error channel


Java 11 and Spring Integration 5.x here. Currently I am specifying the error channel on each and every IntegrationFlow like so:

@Bean
public IntegrationFlow flow1(MessageChannel channel1) {
    return IntegrationFlows.from(channel1)
        .enrichHeaders(h -> h.header("errorChannel", ERRORS_CHANNEL_NAME))
        // do stuff
        .get();
}

@Bean
public IntegrationFlow flow2(MessageChannel channel2) {
    return IntegrationFlows.from(channel2)
        .enrichHeaders(h -> h.header("errorChannel", ERRORS_CHANNEL_NAME))
        // do stuff
        .get();
}

@Bean
public IntegrationFlow flow3(MessageChannel channel3) {
    return IntegrationFlows.from(channel3)
        .enrichHeaders(h -> h.header("errorChannel", ERRORS_CHANNEL_NAME))
        // do stuff
        .get();
}

Is there a way to set a "global" error channel and use it by default, so I don't have to keep redefining which error channel to use in each and every flow?


Solution

  • There is one for you in the framework: https://docs.spring.io/spring-integration/docs/current/reference/html/core.html#channel-special-channels.

    However, please, follow that link about Error Handling. It is not always the fact that any errors in the message processing goes to the error channel.

    There is also a HeaderEnricherSpec.errorChannel() API for convenience: not sure why you stick with that header("errorChannel")...