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 –

相關問題