1
votes

I'm working on a service which sends emails using the spring integration java dsl.

I have a batch message which is split into a collection of individual messages which will be turned into emails.

The issue I am experiencing is that if one of these individual messages throws an error, the other messages in the batch are not processed.

Is there a way to configure the flow so that when a message throws an exception, the exception is handled gracefully and the next message in the batch is processed?

The following code achieves the functionality I would like but I'm wondering if there is an easier / better way to achieve this, ideally in a single IntegrationFlow? :

    @Bean
    public MessageChannel individualFlowInputChannel() {
        return MessageChannels.direct().get();
    }

    @Bean
    public IntegrationFlow batchFlow() {
        return f -> f
            .split()
            .handle(message -> {
                try {
                    individualFlowInputChannel().send(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
    }

    @Bean
    public IntegrationFlow individualFlow() {
        return IntegrationFlows.from(individualFlowInputChannel())
            .handle((payload, headers) -> {
                throw new RuntimeException("BOOM!");
            }).get();
    }
1

1 Answers

0
votes

You can add ExpressionEvaluatingRequestHandlerAdvice to the last handle() definition with its trapException option:

/**
 * If true, any exception will be caught and null returned.
 * Default false.
 * @param trapException true to trap Exceptions.
 */
public void setTrapException(boolean trapException) {

On the other hand, if you are talking about "sends emails", wouldn't it be better to consider to do that in the separate thread for each splitted item? In this case the ExecutorChannel after .split() comes to the rescue!