0
votes

I keep one of my flow say 'flow A' in "stopped" as a initial state. Then in another flow say 'flow B', using groovy script or MEL expression; I am starting the same 'flow A'. At the end of the 'flow A', I am programmatically stopping the flow.

Now, If I again start the 'flow A' through 'flow B' it fails saying 'flow A' is already started; can not be restarted.

Any solution for this.

I want to start my flow any time I want, keeping initial state as stopped & again at the end stopping the flow using script.

Here is the code:

<flow name="FlowB">
    <poll doc:name="Poll">
        <logger level="INFO" doc:name="Logger"/>
    </poll>
    <logger level="INFO" doc:name="Logger" message="triggered"/>
    <scripting:component doc:name="Groovy">
        <scripting:script engine="Groovy"><![CDATA[muleContext.registry.lookupFlowConstruct('FlowB').start()]]></scripting:script>
    </scripting:component>
    <logger level="INFO" doc:name="Logger"/>
    <expression-component doc:name="Expression"><![CDATA[app.registry.FlowA.stop();]]></expression-component>
</flow>
<flow name="FlowA" initialState="stopped">
    <sqs:receive-messages config-ref="Amazon_SQS__Configuration" doc:name="Amazon SQS (Streaming)"/>
    <logger level="INFO" doc:name="Logger"/>
</flow> 

I am using poller to start the flow A. So, if I again run the flow B to start the flow A; Its throwing an exception.

2

2 Answers

0
votes

either stopping flowA did not work (for whatever reason) or you tried to start flowA immediately after you stopped it. AFAIK starting/stopping takes place asynchronously, which means flowA might be in started state even if stop() method already returned.

here is a working example exposing /stop and /start http-endpoints.

<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>

    <flow name="flowA">
        <logger message="foobar" level="INFO" doc:name="Logger"/>
    </flow>

    <flow name="flowB_start">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/start" doc:name="HTTP"/>
        <expression-transformer expression="#[groovy:muleContext.registry.get('flowA').start()]" doc:name="Expression"/>
    </flow>

    <flow name="flowB_stop">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/stop" doc:name="HTTP"/>
        <expression-transformer expression="#[groovy:muleContext.registry.get('flowA').stop()]" doc:name="Expression"/>
    </flow>
</mule>

i can start/stop flowA as often as i want.

0
votes

This is quite simple to handle and in a very easy way. You can use If else condition to control that. If the FlowA is already started it will ignore else it will start as following.

  <flow name="flowA" initialState="stopped">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/flowA" doc:name="HTTP"/>
        <logger message="In flow A" level="INFO" doc:name="Logger"/>
    </flow>

    <flow name="flowB">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/flowB" doc:name="HTTP"/>
        <scripting:component>
    <scripting:script engine="groovy">
    if(muleContext.registry.lookupFlowConstruct('flowA').isStarted())
    {
    System.out.println("flowA already started ... ignoring and do nothing")
    }
    else
    {
      muleContext.registry.lookupFlowConstruct('flowA').start()
     }
    </scripting:script>
</scripting:component>
    </flow>

UPDATE

<flow name="FlowB">
    <poll doc:name="Poll">
        <fixed-frequency-scheduler frequency="3000" />
        <logger level="INFO" doc:name="Logger" message="FlowB" />
    </poll>
    <logger message="test" level="INFO" doc:name="Logger" />

    <scripting:component doc:name="Script">
        <scripting:script engine="groovy"><![CDATA[
if(muleContext.registry.lookupFlowConstruct('FlowA').isStarted())
{
System.out.println("flowA already started ... ignoring and do nothing")
}
else
{
  muleContext.registry.lookupFlowConstruct('FlowA').start()
 }]]>
        </scripting:script>
    </scripting:component>

    <flow-ref name="FlowA" doc:name="FlowA" />
    <expression-component doc:name="Expression"><![CDATA[app.registry.FlowA.stop();]]></expression-component>
</flow>


<flow name="FlowA" initialState="stopped">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP" />
    <logger level="INFO" doc:name="Logger" message="In flowA" />
</flow>