2017-06-08 85 views
0

我保留一个流程,将'流程A'作为初始状态停在“停止”状态。然后在另一个流程中使用groovy脚本或MEL表达式来说'流程B';我开始了同样的'流程A'。在'流程A'结束时,我以编程方式停止流程。现在,如果我再次通过“流程B”启动'流程A',它将失败,说'流程A'已经启动;无法重新启动。以编程方式启动/停止骡子流程

任何解决方案。

我想随时启动自己的流程,保持初始状态为停止&再次停止使用脚本流程。

下面是代码:

<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> 

我使用轮询这么开始流A.,如果我再次运行流程B开始流通A;它抛出一个例外。

回答

0

无论是停止flowA都不起作用(无论出于何种原因),还是在停止后立即尝试启动flowA。 AFAIK启动/停止异步发生,这意味着即使stop()方法已经返回,flowA也可能处于启动状态。

这里是一个工作示例,暴露了/stop/start http端点。

<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> 

我可以根据需要随时启动/停止flowA

+0

我认为你是对的。返回stop()后,流程不能再次启动,因为它们异步工作。请看看我的代码。 –

+0

您可以在停止后再次启动流程...但您必须等待,直到将状态从开始状态切换为停止状态完成。这个时间问题的一个可能的解决方案可能是Anirbans isStarted-check和增加轮询频率的组合。 – Yevgeniy

0

这是非常简单的处理和一个非常简单的方法。您可以使用If else条件来控制它。如果FlowA已经启动,它将忽略,否则它将开始如下。

<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> 
+0

感谢您的回复。我可以使用上述方法启动流程。但我的要求是在开始相同的流程之后,我想在一段时间之后停止。如果它是可能的?如果是,那么如果我执行相同的脚本来启动流程,它将失败。异常抛出说这个流程已经开始,不能重新启动。所以,我想stop()方法不起作用。 –

+0

你试过这个吗?你只需要在flowA结尾放置停止脚本。一旦flowA将启动并执行,它将在最后停止 –

+0

对于“此流程已启动并且无法重新启动。”问题,它在flowB中处理,你不会得到这个 –