Over a million developers have joined DZone.

Camel Exception Handling Overview for a Java DSL

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

Here are some notes on adding Camel (from v2.3+) exception handling to a JavaDSL route.  There are various approaches/options available.  These notes cover the important distinctions between approaches...

Default handling
The default mode uses the DefaultErrorHandler strategy which simply propagates any exception back to the caller and ends the route immediately.  This is rarely the desired behavior, at the very least, you should define a generic/global exception handler to log the errors and put them on a queue for further analysis (during development, testing, etc).

onException(Exception)
    .to("log:GeneralError?level=ERROR")
    .to("activemq:queue:GeneralErrorQueue");


Try-catch-finally
This approach mimics the Java for exception handling and is designed to be very readable and easy to implement.  It inlines the try/catch/finally blocks directly in the route and is useful for route specific error handling.

from("direct:start")
    .doTry()
        .process(new MyProcessor())
    .doCatch(Exception.class)
        .to("mock:error");
    .doFinally()
        .to("mock:end");

onException
This approach defines the exception clause separately from the route.  This makes the route and exception handling code more readable and reusable.  Also, the exception handling will apply to any routes defined in its CamelContext.

onException(Exception.class)
    .to("mock:error");

from("direct:start") .process(new MyProcessor()) .to("mock:end");

 

Handled/Continued
These APIs provide valuable control over the flow.   Adding handled(true) tells Camel to not propagate the error back to the caller (should almost always be used).  The continued(true) tells Camel to resume the route where it left off (rarely used, but powerful).  These can both be used to control the flow of the route in interesting ways, for example...

from("direct:start")
    .process(new MyProcessor())
    .to("mock:end");

//send the exception back to the client (rarely used, clients need a meaningful response)
onException(ClientException.class)
    .handled(false)    //default
    .log("error sent back to the client");

//send a readable error message back to the client and handle the error internally
onException(HandledException.class)
    .handled(true)
    .setBody(constant("error"))
    .to("mock:error");

//ignore the exception and continue the route (can be dangerous, use wisely)
onException(ContinuedException.class)
    .continued(true);


Using a processor for more control
If you need more control of the handler code, you can use an inline Processor to get a handle to the exception that was thrown and write your own handler code...

onException(Exception.class)
.handled(true) .process(new Processor() { public void process(Exchange exchange) throws Exception { Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT); //log, email, reroute, etc. } });


Summary
Overall, the exception handling is very flexible and can meet almost any scenario you can come up with.  For the sake of focusing on the basics, many advanced features haven't been covered here.
  
For more details, see these pages on Camel's site...

http://camel.apache.org/error-handling-in-camel.html
http://camel.apache.org/try-catch-finally.html
http://camel.apache.org/exception-clause.html

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

Published at DZone with permission of Ben O'Day, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}