Start and stop Mule Flow at runtime
- April 13, 2020
MuleSoft provides options to make a flow stop and start at a designated time, as well as to start and stop a flow dynamically/on request.
- Start flow only once, on project start up
- Enable and disable flows on demand or from the configuration file
In these cases, if you follow the standard approach, you’ll need to regenerate and redeploy the JAR file. Functionality-wise, there’s no code change.
This tech article explores the options for these cases without regenerating the JAR file:
- Using Groovy script within the code
- Using CURL or Postman
- Using ARM REST services from Mule Flow
Using Groovy script within the code
- Add the scripting module to your project if you haven’t previously added it
- Add a scripting component like the following:
This script can switch the status of the flow identified by flow name — that is, if the flow is stopped, it will start and vice versa.
Using CURL or Postman
MuleSoft Anypoint Runtime Manager publishes an API to manage Anypoint Runtime Manager programmatically. One API can also be used to start and stop the specific flow.
ARM API URL: https://anypoint.mulesoft.com/hybrid/api/v1/applications/{applicationId}/flows/{flowId}
Method Name: PATCH
URI Parameters:
- applicationId: Application ID of the Mule application in a deployed environment; this isn’t the application name. To get the application ID, perform the GET operation on https://anypoint.mulesoft.com/hybrid/api/v1/applications
- flowId: Flow ID — that is, the flow name given in Anypoint Studio
Body: example {‘desiredStatus’: ‘STARTED’}
ACCESS_TOKEN: The access token to access the MuleSoft Anypoint Runtime Manager API
To change the flow status, run the following CURL command:
Or use Postman:
The output of both commands will look like the following example:
Using ARM REST services from Mule Flow
MuleSoft asset list contain ARM REST Service (RAML) which can be downloaded from https://anypoint.mulesoft.com/exchange/portals/anypoint-platform/f1e97bc6-315a-4490-82a7-23abe036327a.anypoint-platform/arm-rest-services/
The REST service — https://anypoint.mulesoft.com/hybrid/api/v1/applications/{applicationId}/flows/{flowId} — can be triggered from Mule Flow using the HTTP Request component.
Pros and cons of these approaches
Pros | Cons |
Less Time Consuming | Little bit of planning required which approach suits your use case |
No Need to do code change and generate the jar | As Status change by the method are not persistent so need to change the status after restart |
References:
— By Mohammad Mazhar Ansari