...
Code Block | ||||
---|---|---|---|---|
| ||||
<bean id="myRedeliveryPolicy" class="org.apache.camel.processor.RedeliveryPolicy"> <property name="maximumRedeliveries" value="${myprop.max}"/> </bean> <onException> <!-- you can define multiple exceptions just adding more exception elements as show below --> <exception>com.mycompany.MyFirstException</exception> <exception>com.mycompany.MySecondException</exception> <!-- here we reference our redelivery policy defined above --> <redeliveryPolicy ref="myRedeliveryPolicy"/> </onException> |
Catching multiple exceptions
Available as of Camel 1.5
In Camel 1.5 the exception clauses has been renamed to onException and it also supports multiple exception classes:
Code Block |
---|
onException(MyBusinessException.class, MyOtherBusinessException.class)
.maximumRedeliveries(2)
.to("activemq:businessFailed");
|
And in Spring DSL you just add another exception element:
Code Block | ||||
---|---|---|---|---|
| ||||
<onException>
<exception>com.mycompany.MyBusinessException</exception>
<exception>com.mycompany.MyOtherBusinessException</exception>
<redeliveryPolicy maximumRedeliveries="2"/>
<to uri="activemq:businessFailed"/>
</onException>
|
Marking exceptions as being handled
Available as of Camel 1.5
...
Code Block |
---|
onException(ValidationException).handled(true); |
Example using handled
In this route below we want to do special handling of all OrderFailedException as we want to return a customized response to the caller. First we setup our routing as:
...
If the order could not be processed and thus an OrderFailedException was thrown the caller will not receive this exception (as opposed to in Camel 1.4, where the caller received the OrderFailedException) but our customized response that we have fabricated in the orderFailed
method in our OrderService
. So the caller receives an Exchange with the payload Order ERROR
and a orderid=failed
in a header.
Using handled with Spring DSL
The same route as above in Spring DSL:
...
Wiki Markup |
---|
{snippet:id=e3|lang=java|url=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionHandleAndTransformTest.java} |
Catching multiple exceptions
Available as of Camel 1.5
In Camel 1.5 the exception clauses has been renamed to onException and it also supports multiple exception classes:
Code Block |
---|
onException(MyBusinessException.class, MyOtherBusinessException.class)
.maximumRedeliveries(2)
.to("activemq:businessFailed");
|
And in Spring DSL you just add another exception element:
...
<onException>
<exception>com.mycompany.MyBusinessException</exception>
<exception>com.mycompany.MyOtherBusinessException</exception>
<redeliveryPolicy maximumRedeliveries="2"/>
<to uri="activemq:businessFailed"/>
</onException>
Advanced Usage of Exception Clause
Using per route exception clauses
TODO:
Using fine grained selection using onWhen predicate
...