Loading

How To Stop Or Start Flows In Mule 4.x Programmatically

Fecha de publicación: Mar 2, 2024
Tarea

GOAL

To be able to start and stop flows in Mule 4.x you need to acquire the Mule Registry and then get the flow. This KB provides a way to stop and start Mule flow using a Groovy script and Java SDK connector in Mule 4.x. 
Note: This cannot be applied in CloudHub application where the flow starts with schedulers as it needs to be managed by their own infrastructure. This article is still applicable for flow that starts with HTTP listener for CloudHub application.
Pasos
Option 1: Groovy script
You will need to perform the following steps:
  1. Add the scripting module to your project if you haven't previously added it.
  2. Add a scripting component like the following:
<scripting:execute engine="groovy" doc:name="Toggle flow" doc:id="2eb6f071-bdef-4d3d-926d-2565fcd62d33" >
    <scripting:code>
      flow = registry.lookupByName(&quot;flowName&quot;).get();
      if (flow.isStarted())
        flow.stop()
      else 
        flow.start()
    </scripting:code>
</scripting:execute>
That Script will start the flow with name "flowName" if it is not started and stop it otherwise. Please replace "flowName" with the actual name of the flow you wish to start/stop or with a variable.

Option 2: SDK Connector
Describing all details and content of the connector is beyond the scope of this article. Following is an overall procedure
  • You need to follow steps in the following document to create your Java SDK project and to learn more about its architecture and how it works:
https://docs.mulesoft.com/mule-sdk/v/1.1/
https://docs.mulesoft.com/mule-sdk/v/1.1/getting-started
https://docs.mulesoft.com/mule-sdk/v/1.1/module-structure
https://docs.mulesoft.com/mule-sdk/v/1.1/metadata
  • Please import the FlowController project in your Studio as "existing maven project" and then open FlowControllerOperations class. As you can see in the following section using @Inject annotation, spring will inject Mule registry into Instance Variable muleRegistry:
@Inject
private Registry muleRegistry;
  • Next, each method under this class represents/implements an Operation of the Connector. Please consider the following example Operation. with flow name as input parameter will lookup for given flow name instance in the registry and then try to start it:
public String startFlow(String flowName) {
    Flow flow = (Flow) muleRegistry.lookupByName(flowName).get();
    flow.start();
    Return "Success";
}
  • Install and test the connector using the following command from the terminal
mvn clean install
  • Now you can create a test project and add the following dependency to it in order to test the connector
<dependency>
	<groupId>com.mulesoft.support</groupId>
	<artifactId>FlowController</artifactId>
	<version>1.0.3</version>
	<classifier>mule-plugin</classifier>
</dependency>

Provided sample connector is for demonstration purpose only and is not supported nor considered as official Mulesoft Connector. However, you may use it as is in your project after testing it in your lower environments. 

Related links
How to start/stop a flow programmatically (Mule 3.x)

Disclaimer: This article involves products and technologies which do not form part of the MuleSoft product set. Technical assistance for such products is limited to this article.
Número del artículo de conocimiento

001114843

 
Cargando
Salesforce Help | Article