Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception not propagated to error handler in Apache Camel

I have a route that defines a doTry-doCatch block. When the exception is handled in the doCatch block I want it to be propagated to the error handler to make sure the message is added to the dead letter queue after handling it locally. Problem is that I can't get the propagation to the error handler to work ("defaultErrorHandler called!" is not printed to the console). I also tried with onException, but also no luck.

Any hints greatly appreciated. Regards, Oliver

protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {

            errorHandler(deadLetterChannel("ref:myDLQ")
                .log("defaultErrorHandler called! ${body}"));

            final RouteDefinition route = from("seda:queue.inbox");

            route               
                .doTry()
                    .process(new Processor() {
                        public void process(Exchange exchange) throws Exception {
                            System.out.println("throwing ex");
                            throw new IllegalArgumentException("test");
                        }
                    })
                .doCatch(Exception.class)
                    .process(new Processor() {
                        public void process(Exchange exchange) throws Exception {
                            System.out.println("handling ex");
                            route.log(LoggingLevel.ERROR, "Exception in route: ${body}");
                            throw new IllegalArgumentException("rethrow");
                        }
                    })
             .log("Received order ${body}")
             .to("mock:queue.order");                               
        }
    };
}
like image 592
OlliP Avatar asked Sep 15 '25 10:09

OlliP


1 Answers

According to this

Camel error handling is disabled

When using doTry .. doCatch .. doFinally then the regular Camel Error Handler does not apply. That means any onException or the likes does not trigger. The reason is that doTry .. doCatch .. doFinally is in fact its own error handler and that it aims to mimic and work like how try/catch/finally works in Java.

From my own experiments, I can verify that anything that happens within a doTry is not bubbled up to the error handler or exception policies. If you want to send to a dead letter channel, you'll have to do it manually in the doCatch using a

.to('uri')
like image 189
Mike Salzman Avatar answered Sep 18 '25 11:09

Mike Salzman