Apache Camel onException

I want to catch all Exception from the routes.

I add this to OnExeption:

onException(Exception.class).process(new MyFunctionFailureHandler()).stop(); 

Then I create the MyFunctionureHandler class.

 public class MyFunctionFailureHandler implements Processor { @Override public void process(Exchange exchange) throws Exception { Throwable caused; caused = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class); exchange.getContext().createProducerTemplate().send("mock:myerror", exchange); } } 

Unfortunately, it does not work, and I do not know why.

if there is an Exeption, the program should stop.

How can I find out why this code is not working?

thanks s.

+4
source share
2 answers

I used this on my route:

 public class MyCamelRoute extends RouteBuilder { @Override public void configure() throws Exception { from("jms:start") .process(testExcpProcessor) // -- Handle Exceptions .onException(Exception.class) .process(errorProcessor) .handled(true) .to("jms:start"); } } 

and in my errorProcessor

 public class ErrorProcessor implements Processor { @Override public void process(Exchange exchange) throws Exception { Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); if(cause != null){ log.error("Error has occurred: ", cause); // Sending Error message to client exchange.getOut().setBody("Error"); }else // Sending response message to client exchange.getOut().setBody("Good"); } } 

I hope this helps

+1
source

Keep in mind that if you have routes in several RouteBuilder classes, the presence of onExcpetion in this route affects only all routes under this RouteBuilder

view this answer

in addition, if an exception occurs inside the route and is handled inside this route, it will not extend to the original route of the caller, you need to use noErrorHandler (), i.e. from(direct:start).errorHandler(noErrorHandler()) to extend exception handling back to the caller

0
source

All Articles