Retry Mechanism in Mule Applications

(4 reviews)

home

This application shows a simple use case for how to implement the retry mechanism a Mule application. The application receives the HTTP request and simulates the failure in a flow. After the flow fails, the retry mechanism proceeds with a series of retries based on the configuration.

0e157c90-MuleRetryMechanism.png

Prerequisites

This document assumes that you are familiar with HTTP, Mule, Anypoint connectors, Anypoint Studio, elements in a Mule flow, and global elements.

Example Use Case

The application consumes HTTP requests, and based on the query parameter fail, either successfully completes the flow or throws an exception. This is inside the Until Successful scope, which causes the retry mechanism to repeat the flow part inside the Until Successful scope.

Set Up and Run the Example

Complete the following procedure to create, then run this example in your own instance of Anypoint Studio.

  1. Open the Example project in Anypoint Studio from Anypoint Exchange.
  2. In your application in Studio, click the Global Elements tab. Double-click the HTTP Listener global element to open its Global Element Properties panel. Change the contents of the port field to required HTTP port for example 8081.
  3. Double-click to Until Successful scope and set the Max Retries and Milliseconds Between Retries to values of your choice.
  4. In the Package Explorer, right-click the retry-mechanism-example project name, then select Run As > Mule Application. Studio runs the application on the embedded server.
  5. In a REST client such as Postman, send the following request: GET localhost:{http.port}/trigger?fail=false
  6. In the HTTP response you can see the message Processing successfully completedand the application log contains the following log entries: INFO org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Attempt to trigger processingINFO org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Processing successfully completed.
  7. The flow did not fail, no retry was needed, and the processing was successful.
  8. Send another request: GET localhost:{http.port}/trigger?fail=true
  9. Setting the query parameter fail=true causes the flow to fail by throwing the following exception: throw new Exception("This is the reason why processing failed.");
  10. The HTTP status is 500 (Internal Server Error) now, and the response body contains the error message indicating the flow fails even after predefined number of retries: 'until-successful' retries exhausted. Last exception message was: java.lang.Exception: This is the reason why the processing has failed.
  11. The application log now contains entries like: INFO 2018-07-30 15:29:35,177 org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Attempt to trigger processingINFO 2018-07-30 15:29:36,604 org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Attempt to trigger processingINFO 2018-07-30 15:29:37,611 org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Attempt to trigger processingINFO 2018-07-30 15:29:38,617 org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Attempt to trigger processingINFO 2018-07-30 15:29:39,624 org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Attempt to trigger processingINFO 2018-07-30 15:29:40,631 org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Attempt to trigger processingERROR 2018-07-30 15:29:40,642 org.mule.runtime.core.internal.exception.OnErrorPropagateHandler:**Message : 'until-successful' retries exhausted. Last exception message was: java.lang.Exception: This is the reason why the processing has failed.
  12. As you see in the logs, the application triggered processing that failed, and proceeded with 5 retries each with 1000ms latency (depending on configuration), and in the end it propagated the error.

Documentation

Studio includes a feature that enables you to easily export all the documentation you have recorded for your project. Whenever you want to share your project with others outside the Studio environment, you can export the project's documentation to print, email or share online. Studio's auto-generated documentation includes:

  • A visual diagram of the flows in your application
  • The XML configuration which corresponds to each flow in your application
  • The text you entered in the Notes tab of any building block in your flow

Reviews

TypeExample
OrganizationMuleSoft
Published by
MuleSoft Organization
Published onSep 6, 2018
Asset overview

Asset versions for 2.1.x

Asset versions
VersionActions
2.1.4
2.1.3
2.1.2

Categories

Functions
Enterprise ITNo values left to add
Difficulty
AverageNo values left to add