Example: Error handling
This example illustrates the use of asynchronous reply router in the scenario where inbound HTTP caller waits for response from Mule can may receive either normal response or error message (in case one of the components fails to process the request and exception strategy is invoked).
- $USE_CASE refers to the root folder for this example, errorhandling-ee-supplemental/
- $MULE_HOME refers to the root folder for your Mule installation.
Adding the example and compiled classes to Mule standalone (no build required):
To install this example in Mule Standalone (without building), simply do the following:
1. Unzip the example in your $MULE_HOME/examples directory.
2. Copy the JAR file(s) from $USE_CASE/target directory into MULE_HOME/lib/user.
Building with Maven:
Before building the example, you must ensure that the relevant Mule libraries are in your maven2 repository. Therefore, it is recommended that you run the populate_m2_repo script provided in your Mule distribution. Open command prompt (Windows) or terminal (Linux) and cd into the root directory of the example. Run mvn install - Dmaven.test.skip.exec=true to build the example. This will also automatically copy the built jar file into $MULE_HOME/lib/user. However you must ensure your $MULE_HOME is set to the correct location.
Building with Ant:
If building with ant, simply run ant on the command line. The built jar file will automatically be copied into$MULE_HOME/lib/user.
Setup for Eclipse IDE (with Maven):
1. Open a command line terminal.
2. Go to $USE_CASE folder (with pom.xml).
3. Run maven command mvn eclipse:eclipse to prepare the project for Eclipse import.
4. Open Eclipse IDE.
5. Go to File → Import…
6. Expand the ‘General’ tree, select ‘Existing Projects into Workspace’, and click ‘Next’.
7. Browse and select folder $USE_CASE, and click ‘Finish.
Running the example:
1. At the command line, type runMule.bat (on Windows) or runMule (on UNIX).
2. After the server is up, open the URL
http:/localhost:8080/html/errorhandling.htmlin your browser.
Optionally, you can run 'mvn test' from the command line to launch the functional test case.
How it works:
The synchronous HTTP endpoint receives the request and waits until response is returned to the endpoint, specified in the async-reply router. Please note that even though the entire flow is synchronous, there's async router presents in the config. If the "normal" request is sent, the "NormalProcessing" service is invoked and the response sent to the sendReply endpoint. Otherwise request causes TestException to be thrown, the default exception strategy would catch it and reroute it to the ErrorHandler service. The error handler can transform this exception into the human-readable form or simply return an error message. The message then is forwarded to the sendReply endpoint and returned back to the calling HTTP client.