Two exception handling methods are provided for catching exceptions in Java DSL routes, as follows:
errorHandler()
clause — A simple catch-all mechanism that
re-routes the current exchange to a dead letter channel if an error occurs. This
mechanism is not able to discriminate between different exception
types.
onException()
clause — Enables you to discriminate between
different exception types, performing different kinds of processing for different
exception types.
This section provides a brief introduction to exception handling in FUSE Mediation Router. For full
details, see Dead Letter Channel in
The errorHandler()
clause is defined in a RouteBuilder
class
and applies to all of the routes in that RouteBuilder
class. It is triggered
whenever an exception of any kind occurs in one of the applicable
routes. For example, to define an error handler that routes all failed exchanges to the
ActiveMQ deadLetter
queue, you can define a RouteBuilder
as
follows:
public class MyRouteBuilder extends RouteBuilder { public void configure() { errorHandler(deadLetterChannel("activemq:deadLetter")); // The preceding error handler applies to all of the following routes: from("activemq:orderQueue").to("pop3://[email protected]"); from("file:src/data?noop=true").to("file:target/messages"); // ... } }
Redirection to the dead letter channel does not occur, however, until all attempts at redelivery have been exhausted (see Redelivery policy).
The onException(Class exceptionType)
clause is defined in a
RouteBuilder
class and applies to all of the routes in that
RouteBuilder
class. It is triggered whenever an exception of the specified
type, exceptionType
, occurs in one of the applicable routes. For example, you
can define onException
clauses for catching NullPointerException
,
IOException
, and Exception
exceptions as follows:
public class MyRouteBuilder extends RouteBuilder { public void configure() { onException(NullPointerException.class).to("activemq:ex.npex"); onException(IOException.class).to("activemq:ex.ioex"); onException(Exception.class).to("activemq:ex"); // The preceding onException() clauses apply to all of the following routes: from("activemq:orderQueue").to("pop3://[email protected]"); from("file:src/data?noop=true").to("file:target/messages"); // ... } }
When an exception occurs, FUSE Mediation Router selects the onException
clause that
matches the given exception type most closely. If no other clause matches the raised
exception, the onException(Exception.class)
clause (if present) matches by
default, because java.lang.Exception
is the base class of all Java exceptions.
The applicable onException
clause does not initiate processing, however, until
all attempts at redelivery have been exhausted (see Redelivery policy).
Both the errorHandler
clause and the onException
clause
support a redelivery policy that specifies how often FUSE Mediation Router
attempts to redeliver the failed exchange before giving up and triggering the actions
defined by the relevant error handler. The most important redelivery policy setting is the
maximum redeliveries value, which specifies how many times
redelivery is attempted. The default value is 6.
For full details about the redelivery policy and its associated settings, see Dead Letter Channel in