代码之家  ›  专栏  ›  技术社区  ›  star

如何在通配符过滤器和表达式过滤器Mule中使用flowVars

  •  0
  • star  · 技术社区  · 10 年前

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

    示例:My flow变量名称 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>
    

    习惯于 + 以检查它是否包含一个或多个字符。

    我还使用了表达式过滤器,不知道如何在过滤器表达式中使用流变量。你能帮我吗。

    我不想使用属性筛选器。

    2 回复  |  直到 10 年前
        1
  •  1
  •   Avanaur    10 年前

    请改用表达式筛选器,因为表达式很简单,所以只需使用字符串的startsWith方法即可。

    例如

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

    这将允许消息

        2
  •  0
  •   Ram Bavireddi    10 年前

    首先,你不能使用 wildcard-filter 在…上 flowVars 这是因为它对消息有效负载应用了通配符模式。以下是 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);
        }
    }
    

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

    所以

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