【问题标题】:How to use flowVars inside wildcard filter and expression filter Mule如何在通配符过滤器和表达式过滤器 Mule 中使用 flowVars
【发布时间】:2015-11-27 17:16:14
【问题描述】:

是否可以直接在通配符或表达式过滤器中使用flowVariable。 我需要根据流变量值停止流。

示例:我的流变量名称keyValue 的值类似于customer/feed/h26/h56 在此“h26/h56”中应动态设置,但customer/feed 始终为常量。如果过滤器包含任何字符,我只需要在“/feed/”之后设置过滤器。

  <flow name="testFlow1" doc:name="testFlow1">
    <file:inbound-endpoint responseTimeout="10000" doc:name="File" path="c:/in"/>
     .......( Many component)
    <set-variable variableName="keyValue" value="#[customer/feed/h26/h56]" doc:name="Variable"/>
     .......( Many component)    
     <message-filter doc:name="Message">
        <wildcard-filter pattern="customer/feed/+*" caseSensitive="true"/>
    </message-filter>
</flow>

在模式中使用+ 来检查它是否包含一个或多个字符。

我也使用了表达式过滤器,不知道如何在过滤器表达式中使用流变量。你能帮我解决这个问题吗?

我不想使用属性过滤器。

【问题讨论】:

    标签: regex mule mule-studio mule-component mule-el


    【解决方案1】:

    改用表达式过滤器,因为你的表达式很简单,只需使用String的startsWith方法。

    例如

        <expression-filter expression="flowVars.keyValue.startsWith('customer/feed/')" doc:name="Expression"/>
    

    这将允许消息

    【讨论】:

    • 感谢您的回复。你能给我动态进入的表达式吗?实际上,如果 keyValue 有 'customer/feed/' 单独我应该放弃该消息。只有当它后面有一些额外的字符时,它才应该继续“客户/饲料/某事”。您能否为此提供表达方式。
    • 这里,使用正则表达式。
    【解决方案2】:

    首先,您不能在flowVars 上直接使用wildcard-filter,因为它会将通配符模式应用于消息负载。这是org.mule.routing.filters.WildcardFilter类的实现摘录

    public boolean accept(MuleMessage message) {
        try {
            return accept(message.getPayloadAsString());
        } catch (Exception e) {
            logger.warn("An exception occurred while filtering", e);
            return false;
        }
    }
    

    所以很明显WildcardFilter 将有效负载转换为字符串并应用过滤器。

    此外,在regex-filter 的情况下,它会将正则表达式模式应用于消息负载。这是org.mule.routing.filters.RegExFilter的实现摘录

    public boolean accept(MuleMessage message) {
        try {
            return accept(message.getPayloadAsString());
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
    

    现在回答您的问题,您可以按照 Tyrone Villaluna 的建议很好地使用 expression-filter。但您可能希望在开始和结束符号中包含表达式,例如 ^customer/feed/.+$

    所以

    <expression-filter expression="flowVars.keyValue.matches('^customer/feed/.+$')" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 2013-06-01
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多