Home > Back-end >  Spring Integration Exceptions handling with serviceactivators
Spring Integration Exceptions handling with serviceactivators

Time:12-21

I have the following flow

    public IntegrationFlow flow() {
        return IntegrationFlows.from(commonMonitoringMessageChannel())
                .route(Message.class,
                        message -> message.getHeaders().get("ERROR"),
                        mapping -> mapping.channelMapping(true, errorMessageChannel())
                                .subFlowMapping(false, sf -> sf.route(
                                        Message.class,
                                        message -> message.getHeaders().get("STAGE"),
                                        subMapping -> subMapping
                                                .channelMapping("NEW_REQUEST", newRequestChannel())
                                                .channelMapping("IN_PROGRESS", inProgressChannel())
                                )))
                .get();
    }

I use @ServcieSctivator to process messages from newRequestChannel() and inProgressChannel()

I need to handle exceptions coming from ServiceActivators beans, but exceptions not sent to errorChannel (i have separate flow for listening errorChannel and it work only if I push messages directly to errorChannel from these beans). Now I'm using try...catch block to set ERROR header to message, if something went wrong. But I think that handling exceptions in separate @Service is more correctly. All exceptions from channels handled in one place

CodePudding user response:

The behavior is correct. The framework does not do any assumptions for error handling automatically - it just re-throws exceptions to the caller. The errorChannel only takes an effect when our flow is async. For example, you use an ExecutorChannel, so when you send a message to this one, it is going to be processed on a different thread. Therefore throwing an exception from your subscriber on the other side won't give the producer any hints. Therefore we use a special executor wrapper to catch exceptions over there and produce an ErrorMessage to the configured or default errorChannel.

See more in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/error-handling.html#error-handling

If you really would like to handle errors on the specific service activator instead of propagating them back to the caller, then see an ExpressionEvaluatingRequestHandlerAdvice: https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#expression-advice

  • Related