如何使用 Mule 对象存储将第一个触发器的值保存到第二个触发器

How to use Mule object store to persist the value from first trigger to second trigger

我需要计算进入流程的邮件数量。一旦达到 100,我需要做一些处理。我正在对此进行简单的 POC,但在概念之间被打动了。

流程开始,用值 1 初始化 object store 并在脚本 1+1= 2 中递增该值,并将增量值分配给与 object store 相同的 object store我们有覆盖选项。当第二条消息触发时,我希望 objectstore 给出值 2 以便我可以递增到 3。但这里的问题是,当第二条消息出现时,对象存储再次分配值为“1”)。

如果我删除第一个对象存储中的覆盖 属性 选项,它会抛出错误,例如当第二条消息开始触发时密钥已经存在。

我需要做的就是在第二条消息触发时存储增量值。

任何人都可以提出解决方案,如果有任何代码片段会有所帮助的话。请找到简单的我的配置 xml

<objectstore:config name="ObjectStore" maxEntries="100" persistent="true" doc:name="ObjectStore"/>
<flow name="OB test flow" processingStrategy="thread-per-processor">
    <file:inbound-endpoint path="C:\in" responseTimeout="10000" doc:name="File"/>
    <objectstore:store config-ref="ObjectStore" key="OB1" value-ref="#[&quot;1&quot;]" overwrite="true" doc:name="ObjectStore"/>
    <enricher target="#[flowVars.initialValue]" doc:name="Message Enricher">
        <objectstore:retrieve config-ref="ObjectStore" key="OB1" doc:name="ObjectStore"/>
    </enricher>
    <scripting:component doc:name="Script">
        <scripting:script engine="Groovy"><![CDATA[String storePayload =  message.payload;
      String storeInitialValue= flowVars.initialValue;
    int pValue = Integer.parseInt(storeInitialValue);
    int i = 0;
     if ( i < pValue ){
     pValue=pValue+1;
   flowVars.initialValue=pValue;
  return storePayload;
   }
  ]]></scripting:script>
    </scripting:component>
    <objectstore:store config-ref="ObjectStore" key="OB1" value-ref="# [flowVars.initialValue]" overwrite="true" doc:name="ObjectStore"/>
    <enricher target="#[flowVars.finalValue]" doc:name="Message Enricher">
        <objectstore:retrieve config-ref="ObjectStore" key="OB1" doc:name="ObjectStore"/>
    </enricher>
    <logger message="***FinalValue:#[flowVars.finalValue]" level="INFO" doc:name="Logger"/>
    <file:outbound-endpoint path="c:/out" responseTimeout="10000" doc:name="File"/>
</flow>

这是因为你总是在这里覆盖它:

<objectstore:store config-ref="ObjectStore" key="OB1" value-ref="#[&quot;1&quot;]" overwrite="true" doc:name="ObjectStore"/>

首先尝试检索它:然后检查并覆盖它:

<enricher target="#[flowVars.initialValue]" doc:name="Message Enricher">
            <objectstore:retrieve config-ref="ObjectStore"
                key="OB1" doc:name="ObjectStore" defaultValue-ref="#[1]" />
        </enricher>

        <set-variable variableName="newValue"
            value="#[flowVars.initialValue !=null ? flowVars.initialValue + 1 : 1]"
            doc:name="Variable" />
        <objectstore:store config-ref="ObjectStore" key="OB1"
            value-ref="#[flowVars.newValue]" overwrite="true" doc:name="ObjectStore" />