Code Snippets

Example of Handling Errors in Mule with Collection Aggregator

Here is an example of fixing multiple errors in Mule ESB. For further information, check out this blog post.

This was the error handler:

<catch-exception-strategy name="catchUnexpectedError"> <parse-template location="templates/error-notification-email.txt"/> <smtp:outbound-endpoint name="unexpectedExceptionEmail" host="${email.host}" port="${email.port}" to="${error.to}" from="${error.from}" subject="${generic.error.subject}"/> </catch-exception-strategy>

The problem occurred when the testing of the application was conducted. The intended result was for only one email to be sent will all the error messages in it, however, it sent multiple error emails. To fix this, several steps were taken.

1. We set the MULE_CORRELATION_ID property in the initial flow that was being triggered.

<set-property propertyName="MULE_CORRELATION_ID" value="#[java.util.UUID.randomUUID().toString()]"/>

2. We set the MULE_CORRELATION_GROUP_SIZE property right after that.

<set-property propertyName="MULE_CORRELATION_GROUP_SIZE" value="#[java.lang.Integer.MAX_VALUE]"/>

3. Changed the global exception strategy to look like this:

<catch-exception-strategy name="catchUnexpectedError"> <processor-chain> <collection-aggregator failOnTimeout="false" timeout="${error.aggregator.timeout}"/> <transformer ref="unexpectedErrorTransformer"/> <parse-template location="templates/error-notification-email.txt"/> <smtp:outbound-endpoint name="unexpectedExceptionEmail" host="${email.host}" port="${email.port}" to="${error.to}" from="${error.from}" subject="${generic.error.subject}"/> </processor-chain> </catch-exception-strategy>
Resource Tags: