Error handling is one big the major advantage of Camel. It's basically a mimic of the regular try catch finally in the Java language but with more power.The Try Catch Finally as DSL you can use directly on your route.
Sysntax:
<route> <from uri="direct:start"/> <!-- here the try starts. its a try .. catch .. finally just as regular java code --> <doTry> <process ref="processorFail"/> <to uri="mock:result"/> <doCatch> <!-- catch multiple exceptions --> <exception>java.lang.exception</exception> <exception>java.lang.IllegalStateException</exception> <to uri="mock:catch"/> </doCatch> <doFinally> <to uri="mock:finally"/> </doFinally> </doTry></route>
Example :
Above camel route calling one HTTP end point.While returning a response if any error it will fall into catch block and execute the catch endpoint.To simulate rethrowing an exception from a doCatch you should use the handled predicate. If it is evaluated to false Camel will reattach the exception on the Exchange.
<route id="BLOG_TIMEOUT" > <from uri="direct:start"/> <!-- here the try starts. its a try .. catch .. finally just as regular java code --> <doTry> <to uri="{{HTTPURL}}?httpClient.soTimeout=50(Timout in milliseconds)" /> <to uri="direct:catch"/> <!-- catch multiple exceptions --> <doCatch> <exception>java.net.SocketTimeoutException</exception> <handled><constant>true<constant></handled> <to uri="direct:catch"/> </doCatch>
<doCatch>
<exception>java.lang.Exception</exception>
<handled><constant>true<constant></handled>
<to uri="direct:catch"/>
</doCatch>
<doFinally> <to uri="direct:finally"/> </doFinally> </doTry></route>Above camel route calling one HTTP end point.While returning a response if any error it will fall into catch block and execute the catch endpoint.To simulate rethrowing an exception from a doCatch you should use the handled predicate. If it is evaluated to false Camel will reattach the exception on the Exchange.
No comments:
Post a Comment