Reputation: 503
Is possible to implement some switch before error handler of apache-camel?
Something like: If it is MyException.class, then use default error handler otherwise use dead letter channel for handling an error.
I have try to use but seems this cannot be set globaly so easy as it has to be in method configure() of each route.
Upvotes: 2
Views: 7310
Reputation: 507
Use try-catch in camel route
.doTry()
.to("bean:<beanName>?method=<method>")
.endDoTry()
.doCatch(MyException.class)
.to("bean:<beanName>?method=<method1>")
.doCatch(Exception.class)
.to("bean:<beanName>?method=<method2>")
Upvotes: 1
Reputation: 503
Solution: I have used DeadLetterChannelBuilder as error handler with failureProcessor and deadLetterHandleNewException as false, that did the check what I needed (rethrowing exception/hide exception).
Thanks for advice anyway, it led me to the right way.
Upvotes: 0
Reputation: 21
Global scope for the errorHandler is only per RouteBuilder instance. You will need to create a base RouteBuilder class that contains the error handling logic in its configure() method and then extend all of your other routes from it (not forgetting to call super.configure()).
You can use a combination of errorHandler as a catch-all for exceptions, with specific exceptions handled by onException()
errorHandler(deadLetterChannel("mock:generalException"));
onException(NullPointerException.class)
.handled(true)
.to("mock:specificException");
Any routes with these handlers will send exchanges that throw a NullPointerException to the endpoint "mock:specificException". Any other exceptions thrown will be handled by the errorHandler, and the exchange will be sent to "mock:generalException".
http://camel.apache.org/error-handler.html
http://camel.apache.org/exception-clause.html
Upvotes: 1
Reputation: 3193
Yes you can have a generic error handler.
In the configure method I have done like this:
public void configure() throws Exception {
ExceptionBuilder.setup(this);
...
}
The ExceptionBuilder class look like this:
public class ExceptionBuilder {
public static void setup(RouteBuilder routeBuilder) {
routeBuilder.onException(Exception.class).useOriginalMessage().handled(true).to("direct:errorHandler");
}
}
Finally in the error handler configure it to your requirements. That means, save the body and headers to log file or send them to a jms queue or stop the processing or anything else. That is up to you. You simply configure it once and refer to it from all your routeBuilder classes.
Upvotes: 2