The default behavior for Camel is to call your exception (or error) handler, and rethrow the exception to the caller. This recipe will show how to mark that exception as handled, which tells Camel that it should not consider this to be an error and nothing further needs to be done, that is, do not rethrow that particular exception to the consuming endpoint.
The Java code for this recipe is located in the org.camelcookbook.error.exception
package. The Spring XML files are located under src/main/resources/META-INF/spring
and prefixed with exception
.
Within the onException
DSL statement, specify a handled
statement with a predicate that will indicate if the exception has been handled (predicate evaluates to true
) or if it is not handled (predicate evaluates to false
, so exception should be re-thrown).
In the XML DSL, define an onException
element within either the camelContext
or route
element. Within the onException
element include one or more exception
elements with the full package name of the exception you want this handler to catch, follow that with one or more route steps (for example, <to uri="direct:handleError"/>
). This is common for all exception handlers.
Then, add a handled
element with a nested predicate (for example, <constant>true</constant>
):
<camelContext xmlns="http://camel.apache.org/schema/spring"> <onException> <exception> org.camelcookbook.error.shared.FlakyException </exception> <handled> <constant>true</constant> </handled> <to uri="mock:error"/> </onException> <!-- ... --> </camelContext>
In the Java DSL, within the RouteBuilder.configure()
method, define an onException
block and include a comma separated list of one or more class types for the Exception
types that you want to handle. Then using the onException
fluent API, define one or more route steps (for example, to("direct:handleError")
). This is common for all exception handlers.
Then, add the handled(…)
call with either a boolean
or a predicate:
public class ExceptionRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
onException(FlakyException.class)
.handled(true)
.to("mock:error");
from("direct:start") //...
}
}
The onException
block behaves like any exception handler as described in the Catching exceptions recipe, except that if the handled
expression evaluates to true
then Camel will not re-throw the exception.
No further processing of this message will occur within the route where the exception was thrown, and the current message state will be returned to the caller. The exception handler can alter the message before it is returned, so it can create a custom error response as part of its normal exception handling.
If you want to continue processing the message that caused the exception from the next step within the route, instead of handled
you can mark the exception as continued
. Marking an exception as continued
means that the exception handling steps will be run on the message, and then the message will pick up processing at the step after the one that threw the exception.
In the XML DSL, use the continued
element with a nested predicate. Refer to the following code for the same:
<onException> <exception> org.camelcookbook.error.shared.FlakyException </exception> <continued> <constant>true</constant> </continued> <to uri="mock:ignore"/> </onException>
In the Java DSL, call the continued(…)
statement with a boolean
or a predicate:
onException(FlakyException.class)
.continued(true)
.to("mock:ignore")
Camel exception handlers also support redelivery policies (see the Retrying an operation recipe), the onRedelivery
processor (see the Customizing each redelivery attempt recipe), and conditional processing (see the Conditional retry recipe) just like error handlers.
18.226.177.86