【问题标题】:Recursively invoke a Mule flow递归调用 Mule 流
【发布时间】:2016-10-11 22:56:48
【问题描述】:

试图找出递归调用 Mule 流的正确方法。

我们有一个流程,它在运行时构建一组工作,然后使用“For Each”块内的“流程引用”递归调用自身。问题是,我们还没有找到将参数传递给这个递归流的正确方法,所以我们没有得到我们期望的结果。

我们尝试使用流属性(Groovy 中的 setInvocationParameter())传递参数,但似乎这些属性在流的多个实例之间共享。 例如,我们让 ForEach 数组遍历包含 [2. 3. 4],但根据时间的不同,其中一些值会丢失(我们通常会看到 2,然后是 4 两次 - 跳过 3)。

我们尝试了不同的 Mule 处理策略,但都没有成功。 Mule 的默认队列异步具有上述问题。同步似乎根本不起作用(这是有道理的,因为我们的递归模型可能至少需要两个实例才能运行)。

这里是配置XML的相关部分(整个流程相当大)。在流程的最后是这样的:

<foreach collection="#[sessionVars['actionArray']]"
         counterVariableName="actionIndex" 
         rootMessageVariableName="actionVar" doc:name="For Each">
   <scripting:component doc:name="Run Each Action">
    <scripting:script engine="Groovy">
         <![CDATA[def aa = message.getSessionProperty('actionArray')
         def this_item = aa.get(message.getInvocationProperty('actionIndex'))
         // Pass the desired action for the recursive call
         message.setInvocationProperty('FlowAction', this_item)
         log.info "Running $this_item" // <- Shows the correct item 
         return]]>
    </scripting:script>
   </scripting:component>
   <flow-ref name="DoAction" doc:name="Do Action"/>
</foreach>

在流的前面,有一个记录器,它显示“FlowAction”流变量。当我们使用我的 [2, 3, 4] 数组进行测试时,这个记录器语句被驱动了 3 次(如预期的那样),但通常使用值 2、4 和 4。

我们在 Mule 3.7 和旧的 3.4 系统(都是社区版)上得到了相同的结果。

感谢Mule mavens的任何建议......

【问题讨论】:

  • 你能发布你的xml吗?

标签: mule


【解决方案1】:

我不确定这是否 100% 正确,但这就是我们所做的...

在花费大量时间试图让“For Each”和“Flow reference”方法可靠地工作之后,我们放弃并改用了另一种技术。我们的替代方法是删除 For Each 块并从一个简短的 Groovy 脚本递归地驱动流程:

     . . .

     // Invoke the flow recursively for every item in the array

     Flow flow = muleContext.getRegistry().lookupFlowConstruct("flow name") 
     actions.each   // actions is an array of integers built earlier 
     { item->
          MuleMessage msg = message.createInboundMessage()
          DefaultMuleSession sess = new DefaultMuleSession(flow, muleContext) 
          DefaultMuleEvent  event = new DefaultMuleEvent(msg, MessageExchangePattern.ONE_WAY, sess)

          // Copy the current inbound properties to the new message

          message.getInboundPropertyNames().each
          {
             event.getMessage().setProperty(it, message.getInboundProperty(it), PropertyScope.INBOUND)
          }

          // Copy the current session variables to the new message too

          message.getSessionPropertyNames().each
          {
             event.setSessionVariable(it, message.getSessionProperty(it))
          }

          // Now set the item we want processed as a flow variable

          event.setFlowVariable("Action", item.toString())

          // Finally, trigger the flow (which runs asynchronously)

          flow.process(event).getMessage()
    }

这在我们的环境中现在可以正常工作。

【讨论】:

    猜你喜欢
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    • 2015-01-07
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多