What is the scope of variables in Mule expression components, and how does that relate to flow variables? I had a flow with a set-variable
and was surprised to see that the value was being overwritten by an assignment in an expression-component
. For example,
<flow name="HelloWorldFlow1" doc:name="HelloWorldFlow1">
<http:inbound-endpoint exchange-pattern="request-response"
host="localhost" port="9999" doc:name="HTTP"
doc:description="This endpoint receives an HTTP message." path="helloworld"/>
<set-variable variableName="asdf" value="firstvalue" doc:name="Variable"/>
<logger message="#[flowVars["asdf"]]" level="INFO" doc:name="Logger"/>
<expression-component doc:name="Expression"><![CDATA[asdf = "secondvalue";]]></expression-component>
<logger message="#[flowVars["asdf"]]" level="INFO" doc:name="Logger"/>
<expression-component doc:name="Expression"><![CDATA[qwer = "thirdvalue";]]></expression-component>
<logger message="#[flowVars["qwer"]]" level="INFO" doc:name="Logger"/>
</flow>
The output of this is:
INFO 2014-04-25 08:58:46,889 [[helloworld].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: firstvalue
INFO 2014-04-25 08:58:46,893 [[helloworld].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: secondvalue
INFO 2014-04-25 08:58:46,895 [[helloworld].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: null
If possible, can you point me to the documentation of the scoping rules? I've tried a few different searches and keep getting extraneous results.
Clarification: Within the expression-component
, does Mule first check to see if there is a flow variable with a given name and then use that variable instead of creating a new one? If the expression component creates a variable, is the scope limited to just the expression component code? In http://blogs.mulesoft.org/mule-school-the-mulemessage-property-scopes-and-variables/, it says that Mule flow variables "behave like instance properties", but I can't find a definition for what an instance property is. The word scope is also not in the expression component reference page (http://www.mulesoft.org/documentation/display/current/Expression+Component+Reference).
With <set-variable
you're creating a flow variable. Its scope is within flow
it is declared and also sub-flows
or private flow
called by this flow. Think it of like as an instance variable.
Moreover, you've 4 logger statements in your flow while you're showing output of only 3 log statements. If you modify log statement to include what value you're printing, it will be less of confusion trying to figure out that print statement is for which variable like:
<logger message="Value of asdf is: #[flowVars['asdf']]" level="INFO" doc:name="Logger"/>
And you don't need to surround "
arround your variable name. Other shorthand notation is to just say
<logger message="Value of asdf is: #[asdf]" level="INFO" doc:name="Logger"/>
For broader reference to scopes see this: http://blogs.mulesoft.org/mule-school-the-mulemessage-property-scopes-and-variables/
When you overwrite a variable value in a flow, initial value is overwritten by next assigned value just like a variable value assignment functions in java.
EDIT:
You're right, variables created by <set-variable>
can be re-assigned with expression component but variables created inside <expression-component>
doesn't have scope out side that block. Sorry I don't have links to back up with, this is based on my experiment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With