• Subscription Options

  • What I write about

  • RSS Latest News From MuleSoft

  • Archives

  • Technical Features

  • Visitors Online

  • Null Filtering

    Interesting use-case presented to me while I was lecturing the other week – what if I need to filter for Mule messages based upon a message property? Furthermore, what if I needed to filter based upon whether a property existed and was set to null?

    I looked into this matter further as I know that there is a message-property-filter in Mule.

    The message property filter is designed to let you filter based upon the value for a property on a MuleMessage. So, if you’ve just read messages off a JMS queue and wanted to filter for all those with a priority set to 9, you could use this sort of filter configuration:

    <message-property-filter pattern="JMSPriority=9"/>

    You can also reverse the logic here by using its not attribute:

    <message-property-filter pattern="JMSPriority=9" not="true"/>

    As you can see here, the pattern attribute is set to the “name=value” expression we want to use. What if I configured the pattern like this?

    <message-property-filter pattern="JMSPriority"/>

    In this case, the filter will return true if the property exists, whatever its value. This is handy if I want to check for the existence of a property but not quite what I need.

    My first thought was that a new filter needs to be created. This would be rather easy to do as I just need a Java class that implements Mule’s Filter interface. The single method I’d need to code (the Accept would simply check if the property existed and, if so, if it is null.

    However, expressions offer a more elegant way of doing the same thing:

    <expression-filter expression="MyProperty == null" 
        evaluator="groovy"/>

    If you enjoyed this post, make sure you subscribe to my RSS feed!

    Tags: , , ,

    Leave a Reply

    Add video comment
    © Copyright 2005-2008 Ricston, All Rights Reserved
     Sitemap   Privacy Policy    Legal