OlliP
OlliP

Reputation: 1585

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");                               
        }
    };
}

Upvotes: 4

Views: 6380

Answers (2)

Mike Salzman
Mike Salzman

Reputation: 143

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')

Upvotes: 3

Petter Nordlander
Petter Nordlander

Reputation: 22279

try .handled(false) in doCatch. Read this.

Upvotes: 2

Related Questions