Search code examples
mulemule-studiomule-componentmule-el

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


I have a requirement to count the number of messages entering in to the flow. Once it reaches 100 i need to do some process. I'm doing simple POC on this, but get struck in between the concept.

Starting of the flow, have initialize the object store with value 1 and incrementing the value in script 1+1= 2, and assigning incremental value to the same object store as we have override option. When the second message triggered, i'm expecting objectstore gives the vales as 2 so that i can increment to 3. But here the problem is, when the second message comes in, again the object store assigning the value as '1').

If i remove the override property option in first object store, it is throwing error like key already exists when the second message started triggering in to flow.

All i need to do, i need to store the incremental value when the second messages trigger.

Could any one please suggest the solution, if any code snippet would be helpful. Please find simple my config 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>

Solution

  • Its because you always overwrite it here:

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

    Just try retrieving it first: then check it and overwrite it:

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