Start and Stop Mule Flow at Runtime
Note: While copy/paste the code snippet from blog please covert quotes and double quotes.
MuleSoft provides options to make a flow stop and start at design time. However there are few cases where we might need to decide to start and stop flow dynamically/on request.
- Use Case # 1: Start Flow Only Once on project start up
- Use Case # 2: Enable and Disable flows on demand or from configuration file
In the above mentioned use cases if we follow the standard approach we need to generate JAR again and deploy it. However functionality wise there is no code change.
In this blog I will explore the options which can be opted in above mentioned use cases without regenerating the JAR file.
- Option # 1: Using Groovy Script with in Code
- Option # 2: Using CURL or Postman
- Option # 3: Using ARM REST Services from Mule Flow
Lets dig deeper to each of the option:
Option # 1: Using Groovy Script with in Code
You will need to perform the following steps:
- 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 flowName. I.e. if flow is stopped it will start and vice versa.
Option # 2: Using CURL or Postman
Mulesoft Anypoint Runtime Manager publishes an API to manage Anypoint Runtime Manager programmatically. One of the 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 Param:
- applicationId: Application ID of the Mule Application in deployed environment its not application name. To get the application id do GET operation on https://anypoint.mulesoft.com/hybrid/api/v1/applications
- flowId: Flow Id i.e. Flow Name give in Anypoint Studio
Body: example {‘desiredStatus’: ‘STARTED’}
ACCESS_TOKEN: Access Token to access MuleSoft Anypoint Runtime Manager API
To change the flow status run below mentioned CURL command
Or use Postman
The output of both command will be like below
Option # 3: 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 HTTP Request component.
Pro and Cons of above Mentioned approaches:
Pros
Less Time Consuming
Little bit of planning required which approach suits your use case
Cons
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: