骡子 MEL 验证

Mule MEL validation

我的 属性 文件值为:

companies = test
companies1 =
companies2 = 'another'

现在,如果我有如下的 MEL 验证不起作用。

<choice  doc:name = "PageNum-1">
    <when expression = "#[ ${companies} != '' ]">
        <logger message = "===== Nothing to execute in process 1 ===" level="INFO" doc:name = "Logger"/>    
    </when>
    <otherwise>
        <logger message = "===== Nothing to execute in process 0 ===" level="INFO" doc:name = "Logger"/>    
    </otherwise>
</choice>

错误:

Exception stack is:
1. [Error: illegal use of operator: !=]
[Near : {... !='' ....}]
             ^
[Line: 1, Column: 1] (org.mule.mvel2.CompileException)

编辑:

作为变通解决方案,我添加了一个流变量并将表达式中的数据验证为

<set-variable variableName = "company1" value = "${companies1}" doc:name = "company1"/>
<when expression = "#[flowVars.company1.isEmpty()]">

但是,有没有更简单的方法可以只使用 $ as ${commpany1}

您的问题与属性的替换方式有关。它们以一个接一个的方式被替换,所以如果你不用引号包围 属性 价值公司,就像你在公司 2 中所做的那样,它不会起作用。

此外,在 expression 属性中不需要使用 #[]。以下对我有用:

<context:property-placeholder location="myprop.properties" />

<flow name="test-http">
    <http:inbound-endpoint host="127.0.0.1" port="8081"
        doc:name="HTTP" />
    <choice doc:name="PageNum-1">
        <when expression="${companies}!=''">
            <logger message="===== Nothing to execute in process 1 ==="
                level="INFO" doc:name="Logger" />
        </when>
        <otherwise>
            <logger message="===== Nothing to execute in process 0 ==="
                level="INFO" doc:name="Logger" />
        </otherwise>
    </choice>
</flow>

如果您希望 MEL 将 属性 值作为字符串处理,您应该在表达式或属性文件中添加引号,而不是像您所做的那样在两个位置或混合位置添加引号。因此,要测试文件中的 属性 ${companies} 是否为空,您可以使用。

<when expression="'${companies}' != empty" >

解析属性后,这将变为

<when expression="'' != empty" >

当属性文件包含

 companies=

会变成

<when expression="'Acme Inc' != empty" >

当属性文件包含

 companies=Acme Inc

但是,如果您的属性文件包含

 companies='Snake Oil Ltd'

结果将是

<when expression="''Snake Oil Ltd'' != empty" >

这会产生错误,因此在这种情况下,属性文件中不应包含引号。

如果您需要 属性 包含单引号,一种选择是对表达式属性使用单引号,并用双引号括住字符串值。

    <when expression='"${companies}" != empty' >

这完全符合 XML 并在运行时按预期工作,但是 Anypoint Studio 编辑器会将其标记为错误(即使它不是)所以由于 Anypoint Studio 中的这个错误我建议您可以使用自己找到的解决方法并将 属性 加载到流变量中,然后在表达式中使用它。

<set-variable variableName="company1" value="${companies1}" doc:name="company1"/>
<when expression="#[flowVars.company1.isEmpty()]">