How does Apache Camel determine route termination in split with aggregation strategy upon error occurrence?

50 Views Asked by At

I've developed a Camel route to split a string and process it. When I don't use an aggregation strategy, even if an exception occurs during processing, the route proceeds to the next step defined by .to("direct:mock"). Here's the relevant snippet of my route:

    onException(RuntimeException.class)
        .log("in exception handler")
        .log(body().toString())
        .handled(true)
        .to("direct:dbError");
    
    from("direct:dbError")
        .log("logging in db 1");
    
    from("timer:my-timer?period=10000")
        .log("logging message")
        .setBody(constant("logx,logy,logz"))
        .split(body())
        .to("direct:process")
        .log("I Ain't coming here cause exception happened")
        .end()
        .to("direct:mock")
        .log("log4");
    
    from("direct:process")
        .log(body().toString())
        .process(exchange -> {
        throw new RuntimeException("exception");
        });
    
    from("direct:mock")
        .log("log2");
    

However, when I introduce an aggregation strategy to handle exceptions and propagate them, Camel doesn't invoke the line .to("direct:mock") that follows the end() block. How does Camel determine it should stop routing in this scenario? Any insights or suggestions would be greatly appreciated.

here's the snippet of the aggregation strategy used.

                .split(body(), (oldExchange, newExchange) -> {
                                if (newExchange.getException() != null) {
                                    if (oldExchange == null) {
                                        return newExchange;
                                    } else {
                                        oldExchange.setException(
                                            newExchange.getException());
                                        return oldExchange;
                                    }
                                }
                                if (oldExchange == null) {
                                    return newExchange;
                                }
                                String body = newExchange.getIn().getBody(String.class);
                                String existing = oldExchange.getIn().getBody(String.class);
                                oldExchange.getIn().setBody(existing + "+" + body);
                                return oldExchange;
                            })
0

There are 0 best solutions below