Setting up a global error handler for a camel?

I am trying to set up an exception handler for all exceptions thrown by all routes in my camel context. My approach is as follows, with no luck:

  • Default Camel Context Instant
  • Get RouteDefinition list from spring context
  • Add these definitions to the camel context by calling ctx.addRouteDefinitions ()
  • Add my java dsl exception handler route defined by RotueBuilder (ctx.addRoutes (new MyErrorHandlerRouteBuilder ())
  • Camel context start

At this point, the exceptions thrown inside the routes defined in spring are handled by the DefaultErrorHandler function, and not the one I'm trying to determine. Here is what my route determination processing error looks like

public class MyErrorHandlerRouteBuilder extends RouteBuilder { @Override public void configure() throws Exception { onException(Exception.class) .routeId("errorHandlerRoute") .handled(true) .process(new Processor() { @Override public void process(Exchange exchange) throws Exception { Throwable caused = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class); // do some custom processing of the exception } }) .stop(); } 

What else can I try or where am I mistaken?

+4
source share
3 answers

Create an abstract base class and define your global error handler in the configure method. And then in your route classes extend this base class, and in your configure method, call super.configure () first.

+3
source

Error Handlers is a Java DSL-specific route builder. You need to create one for each route builder. Or in your case, you need to define it in Spring DSL (since you seem to define your routes in Spring).

+1
source

For future readers, this is the solution I use for camel error handling (this is all a java config, but it will work like spring xml):

1) Install a DeadLetterChannelBuilder Bean

 @Bean public DeadLetterChannelBuilder myErrorHandler() { DeadLetterChannelBuilder deadLetterChannelBuilder = new DeadLetterChannelBuilder(); deadLetterChannelBuilder.setDeadLetterUri("direct:error"); deadLetterChannelBuilder.setRedeliveryPolicy(new RedeliveryPolicy().disableRedelivery()); deadLetterChannelBuilder.useOriginalMessage(); return deadLetterChannelBuilder; } 

Now this bean will be responsible for collecting errors and forwarding them to the direct:error URI.

2) Set up the error handling route

You will need a route to listen in the direct:error URI, because now your routes are routed.

 @Override public void configure() throws Exception { from("direct:error") .to("log:error") .end(); } 

As you now see, all your errors are redirected to the above route, you have the flexibility to implement complex error handling logic around the world.

3) Add your global error handler to your routes

You still need to introduce bean error handling into each route. I want the camel to handle this automatically, but I have not found a way. Although with this approach, code duplication / overhead is minimal.

 @Override public void configure() throws Exception { errorHandler(myErrorHandler) from("myStartingUri") .to("myEndDestination") .end(); } 

Looking at the documentation for the springs xml configuration, I think you will need to put <route errorHandlerRef="myErrorHandler"> in the route definition.

+1
source

All Articles