2015-10-16 65 views
0

我有一个要求来计算输入到流中的消息数。一旦达到100我需要做一些过程。我在这方面做了简单的POC,但在这个概念之间受到了打击。如何使用Mule对象存储将值从第一个触发器保存到第二个触发器

流的开始,有初始化object store与价值1和脚本1+1= 2递增值,和分配增量值相同的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> 

回答

1

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

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

是的,你是正确的,我尝试了这种方法。由于我们试图检索密钥,并且密钥不存在。对于消息的第一个输入来说,它会在错误中引发错误“检索”键“不存在”。有什么方法可以检查对象存储键而不会抛出错误。请建议并感谢您的回复。 – star

+0

我们是否能够抑制它抛出的错误,我很好地采用这种方法,只有阻塞是抛出错误,如果钥匙不存在。 – star

+1

更新了我的答案。设置defaultValue-ref。如果这个参数不存在,则会抛出异常。供参考:https://github.com/mulesoft/objectstore-connector/blob/develop/src/main/java/org/mule/modules/objectstore/ObjectStoreConnector.java#L223 –

相关问题