concept onexception in category camel

This is an excerpt from Manning's book Camel in Action.
How can you implement such a solution? Luckily, as you’ll see in chapter 11 on error-handling, Camel allows you to handle exceptions by using
onExceptionin the DSL. You spend less than 10 minutes adding the error handling to theOrderRouteclass that contains the Rest DSL. The following listing shows the REST configuration, error handling, and REST services all together.
Listing 11.9 laid out how to do this by using
onException. The following listing shows how the failureProcessorcould be implemented.Listing 11.10 Using a processor to create a failure response to be returned to the caller
public class FailureResponseProcessor implements Processor { public void process(Exchange exchange) throws Exception { String body = exchange.getIn().getBody(String.class); Exception e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); #1 StringBuilder sb = new StringBuilder(); sb.append("ERROR: "); sb.append(e.getMessage()); sb.append("\nBODY: "); sb.append(body); exchange.getIn().setBody(sb.toString()); } } #1 Gets the exceptionFirst, you grab the information you need: the message body and the exception ❶. It may seem a bit odd that you get the exception as a property instead of
exchange.getException(). You do that because you’ve markedonExceptionto handle the exception; this was done at ❶ in listing 11.9. Consequently, Camel moves the exception from theExchangeto theExchange.EXCEPTION_CAUGHTproperty. The rest of the processor builds the custom failure message that’s to be returned to the caller.
You’ve been studying Camel in Action, and you’ve learned that Camel has extensive support for error handling and that you could use
onExceptionto provide this kind of feature. With great confidence, you fire up the editor and alter the route as shown in the following listing.Listing 11.14 Route using error handling with failover to FTP
errorHandler(defaultErrorHandler() .maximumRedeliveries(5).redeliveryDelay(10000)); onException(IOException.class).maximumRedeliveries(3) #1 .handled(true) .to("ftp://gear@ftp.rider.com?password=secret"); from("file:/rider/files/upload?delay=15m") .to("http://rider.com/upload?user=gear&password=secret"); #1 Exception policyThis listing adds
onException❶ to the route, telling Camel that in the case ofIOException, it should try redelivering up to three times, using a 10-second delay. If there’s still an error after the redelivery attempts, Camel will handle the exception and reroute the message to the FTP endpoint instead. The power and flexibility of the Camel routing engine shines here.onExceptionis just another route, and Camel will continue on this route instead of the original route.
Figure 11.9 The JPA consumer is bridged to Camel’s error handler so
onExceptionnow also triggers when an error happens internally in the JPA consumer, such as no connection to the PostgresSQL database.![]()

This is an excerpt from Manning's book Camel in Action.
The book’s source code has an example that demonstrates what you’ve just learned. Take a look at the OnExceptionTest class in chapter5/onexception. It has multiple test methods, each showing a scenario of how onException works.
Let’s make the example a bit more interesting and add a second onException definition:
onException(OrderFailedException.class).maximumRedeliveries(3);
onException(ConnectException.class).maximumRedeliveries(10);If the same exception hierarchy is thrown as in the previous example, Camel would select the second onException because it directly matches the ConnectionException. This allows you to define different strategies for different kinds of exceptions. In this example, it is configured to use more redelivery attempts for connection exceptions than for order failures.
You have been studying Camel in Action, and you’ve learned that Camel has extensive support for error handling and that you could leverage onException to provide this kind of feature. With great confidence, you fire up the editor and alter the route as shown in listing 5.8.
This listing adds an onException
to the route, telling Camel that in the case of an IOException, it should try redelivering up to 3 times using a 10-second delay. If there is still an error after the redelivery attempts, Camel will handle the exception and reroute the message to the FTP endpoint instead. The power and flexibility of the Camel routing engine shines here. The onException is just another route, and Camel will continue on this route instead of the original route.
Suppose you have a complex route that processes a message in multiple steps. Each step does some work on the message, but any step can throw an exception to indicate that the message can’t be processed and that it should be discarded. This is where handling exceptions with onException comes into the game.
Handling an exception with onException is similar to exception handling in Java itself. You can think of it as being like using a try ... catch block.
The doTry ... doCatch block has one limitation—it’s only route scoped. The blocks only work in the route in which they’re defined. OnException, on the other hand, works in both context and route scopes, so you can try revising listing 5.3 using onException. This is illustrated in listing 5.4.
A difference between doCatch and onException is that doCatch will handle the exception, whereas onException will, by default, not handle it. That’s why you use handled(true)
to instruct Camel to handle this exception. As a result, when a JmsException is thrown, the application acts as if the exception were caught in a catch block using the regular Java try ... catch mechanism.
to the route, telling Camel that in the case of an IOException, it should try redelivering up to 3 times using a 10-second delay. If there is still an error after the redelivery attempts, Camel will handle the exception and