Mule表达变量范围 [英] Mule expression variable scope

查看:329
本文介绍了Mule表达变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mule表达式组件中变量的范围是什么,它与流变量有关?我有一个设置变量的流程,并惊讶地发现该值被表达式组件。例如,

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[&quot;asdf&quot;]]" level="INFO" doc:name="Logger"/>
    <expression-component doc:name="Expression"><![CDATA[asdf = "secondvalue";]]></expression-component>
    <logger message="#[flowVars[&quot;asdf&quot;]]" level="INFO" doc:name="Logger"/>

    <expression-component doc:name="Expression"><![CDATA[qwer = "thirdvalue";]]></expression-component>
    <logger message="#[flowVars[&quot;qwer&quot;]]" level="INFO" doc:name="Logger"/>
</flow>

其输出是:

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.

澄清:表达式组件,Mule首先检查是否有一个具有给定名称的流变量,然后使用该变量,而不是创建一个新变量?如果表达式组件创建一个变量,范围是否仅限于表达式组件代码?在 http://blogs.mulesoft.org/mule -school-the-mulemessage-property-scopes-and-variables / ,它说,Mule流变量表现得像实例属性,但是我找不到一个实例属性的定义。单词范围也不在表达式组件参考页面中( http://www.mulesoft.org/documentation/display/current/Expression+Component+Reference )。

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).

推荐答案

使用< set-variable 创建一个流变量。它的范围在中,它被声明,而子流 private flow 被这个流程调用。将其视为实例变量。

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.

此外,当您显示只有3个日志语句的输出时,您的流中有4个记录器语句。如果您修改log语句以包含要打印的值,那么尝试找出print语句是用于以下变量的情况就会更少混淆:

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

您不需要环绕& quot; arround你的变量名。其他简写符号只是说

And you don't need to surround &quot; arround your variable name. Other shorthand notation is to just say

<logger message="Value of asdf is: #[asdf]" level="INFO" doc:name="Logger"/>

对于更广泛的参考范围,请参阅: http://blogs.mulesoft.org/mule-school-the-mulemessage-property-scopes-and-variables /

For broader reference to scopes see this: http://blogs.mulesoft.org/mule-school-the-mulemessage-property-scopes-and-variables/

当您覆盖流中的变量值时,初始值将被下一个分配的值覆盖,就像java中的变量赋值函数一样。

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.

编辑:

你是对的,由< set-变量> 可以重新分配表达式组件,但< expression-component> 中创建的变量没有范围外的块。对不起,我没有要备份的链接,这是基于我的实验。

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.

这篇关于Mule表达变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆