2. • Choice exception strategy can be defined to customize the
way Mule handles a message with an error based on the
message’s content at the moment it throws an exception.
• A choice exception strategy :
– catches all exceptions thrown within its parent flow
– examines message contents and exception type
– then routes messages to the appropriate exception strategy for
processing.
• Usually more than once exception strategy is defined within
the choice exception strategy.
• Each exception strategy – either catch or rollback – uses
a Mule expression to advise the choice exception strategy
which type of messages it accepts and processes.
Choice Exception Strategy
3. • How it works :
– when the choice exception strategy catches an exception:
• It evaluates the type of exception and the message contents at the
time the error occurred.
• Then it checks the expression attribute of each of its exception
strategies one by one, serially, to see which one of them should
handle the error.
• It then routes the message to the firstexception strategy that
evaluates to true (has an expression that matches the expression
of the message).
• If none of its exception strategies can handle the error, the choice
exception strategy routes the message to Mule’s default exception
strategy.
Choice Exception Strategy
4. • How it works :
– when the choice exception strategy catches an exception:
• It evaluates the type of exception and the message contents at the
time the error occurred.
• Then it checks the expression attribute of each of its exception
strategies one by one, serially, to see which one of them should
handle the error.
• It then routes the message to the firstexception strategy that
evaluates to true (has an expression that matches the expression
of the message).
• If none of its exception strategies can handle the error, the choice
exception strategy routes the message to Mule’s default exception
strategy.
Choice Exception Strategy
5. • You can put any number of catch or rollback
exception strategies into a choice exception strategy.
• You cannot put a choice exception strategy inside
another choice exception strategy.
• You can use any Mule expression evaluator as the
expression attribute of an exception strategy.
Choice Exception Strategy
6. • When to Use :
– Use a choice exception strategy to enable Mule to make
decisions about how to handle each error that occurs in a
flow.
– For example, in a flow that processes orders, a choice
exception strategy can be used to apply the following error
handling rules:
• Messages that throw an AlreadyProcessedException should be
discarded.
• Messages that throw a ValidationException should be sent to an
invalid order queue.
• All other messages which throw exceptions should be rolled back
to retry processing.
Choice Exception Strategy
7. – A choice exception strategy can evaluate the exception
type of each message that throws an exception in this flow
and route them to one of three exception strategies:
• a catch exception strategy to process and discard all
AlreadyProcessedExceptions.
• a second catch exception strategy to process all
ValidationExceptions and send them to an invalid.orders queue
• a rollback exception strategy to roll back the order transaction in
order to retry processing in the parent flow
Choice Exception Strategy
8. – The following lists some useful expressions which check for
specific exceptions:
• #[exception.causedBy(ExceptionType)]
– Checks that the exception is an instance of ExceptionType
– Example : #[exception.causedBy(java.lang.IllegalArgumentException)]
• #[exception.causedExactlyBy(ExceptionType)]
– Checks that the exception exactly matches the ExceptionType
– Example : #[exception.causedExactlyBy(java.net.SocketTimeoutException)]
• #[exception.causeMatches(String)]
– Checks that the exception type matches a particular regular expression
(Regex) String
– Example : #[exception.causeMatches('*BusinessException')]
Choice Exception Strategy
9. – Configuration:
• From the Error Handling palette group, drag and drop the choice
exception strategy icon into the footer bar of a flow.
• Click the title bar of the exception strategy to open its Properties Editor,
then enter a name for your choice exception strategy in the Display
Name field.
• From the Error Handling palette group, drag and drop one or more catch
or rollback exception strategy icons into the choice exception strategy box.
– Note :
• choice exception strategy checks the expression attribute of each of its
exception strategies one by one, serially, to see which one of them should
handle the error.
• It then routes the message to the first exception strategy that evaluates to
true.
• Therefore, organize your exception strategies keeping in mind that the
top-most will be evaluated first, then the one below it, and so on.
• You cannot rearrange the exception strategies once they have been
placed inside the choice exception strategy, but can use xml for
rearranging the strategies.
Choice Exception Strategy