问题
I am wordering about how to implement a counter which is will increase 1 step when flow is called. For example: I have a flow named: http://localhost:8080/doSomething and a variable counter. The counter variable will increase by 1 when I hit enter in doSomething service
Many thanks
回答1:
You need a persistent storage solution. Variables in Mule do not live across invocations. Mule has a concept of Object Stores: https://docs.mulesoft.com/mule-user-guide/v/3.7/mule-object-stores
If you are a Mule Enterprise customer, analytics may be included in your subscription.
回答2:
If you need to store runtime data that is available across the application, you can store the data as objects in the Registry. Here is an example
To set the value
<scripting:component doc:name="Groovy">
<scripting:script engine="Groovy">
<![CDATA[muleContext.getRegistry().registerObject("Count", new Integer(14))]]>
</scripting:script>
</scripting:component>
To read the value
<logger message="Count #[app.registry.get('Count')]" level="INFO" doc:name="Logger"/>
回答3:
If you are just trying to collect statistics, then I suggest you to consider using MuleSoft Insight. You will be able to setup Custom Business Events in your flows and track important information. Finally, you can use Insight Dashboard on CloudHub to get appropriate stats by applying various filters.
回答4:
You can have object store in your flow which can store your counter(number of hits on to your flow) either in memory or in a persistent store. You can then retrieve the counter and increase it on subsequent hit to your flow. https://docs.mulesoft.com/mule-user-guide/v/3.9/object-store-module-reference
回答5:
Initialize a variable with integer value 1 in mule app registry in the starting of your flow as below:
<expression-component doc:name="Expression"><![CDATA[#[app.registry.put('counter',1)]]]></expression-component>
To increment the value use:
<expression-component doc:name="Expression"><![CDATA[#[app.registry.put('counter',app.registry['counter']+1)]]]></expression-component>
To retrieve final value of the counter flag, use the expression:
#[app.registry.get('counter')]
来源:https://stackoverflow.com/questions/49993043/mule-esb-how-to-implement-a-counter-variable-to-count-how-much-times-a-flow-is-c