DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Build a Query in MuleSoft With Optional Parameters
  • DataWeave Interview Question: Compare IDs From Two Arrays and Create a New Array
  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction

Trending

  • The Hidden Latency of Autoscaling
  • The Network Attach Problem Nobody Warns You About
  • Integrating AI-Driven Decision-Making in Agile Frameworks: A Deep Dive into Real-World Applications and Challenges
  • Architecting an Embedded Efficiency Layer: A Platform Deep Dive into Day-Two Operational Tuning
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Types of Anypoint Filters With Mulesoft

Types of Anypoint Filters With Mulesoft

Whether you want to apply regular expression patterns on a message payload or identify duplicate messages, there's a filter for that.

By 
Jitendra Bafna user avatar
Jitendra Bafna
·
Updated Feb. 23, 17 · Opinion
Likes (9)
Comment
Save
Tweet
Share
30.6K Views

Join the DZone community and get the full member experience.

Join For Free

Filters in Mule flows play a vital role in deciding whether messages can be passed to other message processors or not. When a message reaches a filter in the flow and meets the required conditions, then it passed to other processors in the flow. Otherwise, flow execution will be terminated and no more processing will be done.

Idempotent Filter

The idempotent filter is one of the most important filters when it comes to processing unique messages within a flow. You can set Id Expression using MEL to identify the duplicate message on basis of the expression defined. The default expression used is #[message:id].

ThrowOnUnaccepted being checked implies that when duplicate messages are received within the flow, the further processing stops and control is transferred to the error handler.

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8087" doc:name="HTTP Listener Configuration" />
    <flow name="logicmuleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/unique" allowedMethods="POST" doc:name="HTTP" />
        <idempotent-message-filter idExpression="#[message.inboundProperties.'http.query.params'.code]" doc:name="Idempotent Message" throwOnUnaccepted="true" />
        <file:outbound-endpoint path="src/main/resources/out" responseTimeout="10000" doc:name="File" />
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload value="&quot;Message already received.&quot;" doc:name="Set Payload" />
            <set-property propertyName="http.status" value="400" doc:name="Property" />
        </catch-exception-strategy>
    </flow>
</mule>

Regex Filter

The regex filter applies regular expression patterns on the message payload. If the message doesn’t match the pattern, then further processing stops. The filter applies the toString() method to the payload to convert the payload to a String.

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8087" doc:name="HTTP Listener Configuration" />
    <flow name="logicmuleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/unique" allowedMethods="POST" doc:name="HTTP" />
        <regex-filter pattern="^[a-zA-Z]{3,15}$" doc:name="Regex" />
        <file:outbound-endpoint path="src/main/resources/out" responseTimeout="10000" doc:name="File" />
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload value="&quot;Message doesn't match pattern.&quot;" doc:name="Set Payload" />
            <set-property propertyName="http.status" value="400" doc:name="Property" />
        </catch-exception-strategy>
    </flow>
</mule>

Wildcard Filter

The wildcard filter applies a pattern on the message payload. If the message doesn’t match the pattern, then further processing stops. The filter applies the toString() method to the payload to convert the payload to a String. 

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8087" doc:name="HTTP Listener Configuration" />
    <flow name="logicmuleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/unique" allowedMethods="POST" doc:name="HTTP" />
        <wildcard-filter pattern="the fox is cunning*" caseSensitive="false" doc:name="Wildcard" />
        <file:outbound-endpoint path="src/main/resources/out" responseTimeout="10000" doc:name="File" />
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload value="&quot;Message doesn't match pattern.&quot;" doc:name="Set Payload" />
            <set-property propertyName="http.status" value="400" doc:name="Property" />
        </catch-exception-strategy>
    </flow>
</mule>

Schema Validation Filter

The schema validation filter takes XML as an input and validates against the referenced XSD. You can place this filter before another message processor (such as a connector) to determine whether the incoming message or event should be handled or not, allowing the message to only continue on along the flow when the filter validations are met and the included XML is considered valid.

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8087" doc:name="HTTP Listener Configuration" />
    <flow name="logicmuleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/unique" allowedMethods="POST" doc:name="HTTP" />
        <mulexml:schema-validation-filter schemaLocations="src/test/resources/cust.xsd" returnResult="true" doc:name="Schema Validation" />
        <file:outbound-endpoint path="src/main/resources/out" responseTimeout="10000" doc:name="File" />
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload value="&quot;Input xml message doesn't match schema.&quot;" doc:name="Set Payload" />
            <set-property propertyName="http.status" value="400" doc:name="Property" />
        </catch-exception-strategy>
    </flow>
</mule>

Logic Filter

The logic filter allows filtering message on and/or/not expressions. Logic filters apply the and/or/not logic to one or more nested filters that they enclose. When you use these logic filters, you add nested filters to them from within the nested pane for the and/or/not filter.

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8087" doc:name="HTTP Listener Configuration" />
    <flow name="logicmuleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/unique" allowedMethods="POST" doc:name="HTTP" />
        <and-filter doc:name="And">
            <regex-filter pattern="^[0-9]" />
            <wildcard-filter pattern="this is article*" caseSensitive="false" />
        </and-filter>
        <file:outbound-endpoint path="src/main/resources/out" responseTimeout="10000" doc:name="File" />
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload value="&quot;Input xml message doesn't match schema.&quot;" doc:name="Set Payload" />
            <set-property propertyName="http.status" value="400" doc:name="Property" />
        </catch-exception-strategy>
    </flow>
</mule>

Filter Reference

Filter reference is used to reference the global filter. You must have already created a global configuration element and entered the filter name in the global reference property.Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8087" doc:name="HTTP Listener Configuration" />
    <flow name="logicmuleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/unique" allowedMethods="POST" doc:name="HTTP" />
        <filter ref="Regex_Global_Filter" doc:name="Filter Reference" />
        <file:outbound-endpoint path="src/main/resources/out" responseTimeout="10000" doc:name="File" />
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload value="&quot;Input xml message doesn't match schema.&quot;" doc:name="Set Payload" />
            <set-property propertyName="http.status" value="400" doc:name="Property" />
        </catch-exception-strategy>
    </flow>
</mule>

The Idempotent Filter has property ThrowOnUnaccepted by default. If it is checked, this means that whenever duplicate messages are received, control is transferred to Error Handler. However, this is not the case with other filters like regex, wildcard, schema validation, etc. To achieve this, wrap the filter with Message Filter and mark ThrowOnUnaccepted as true on the Message Filter. This way, in case of failure, the control will transfer to Error Handler.

<message-filter throwOnUnaccepted="true">
<mulexml:schema-validation-filter schemaLocations="src/test/resources/customer.xsd" returnResult="true" doc:name="Schema Validation"/>
</message-filter>

I hope that this article clarifies any confusion that you may have had with Anypoint Mule filters!

Filter (software) MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • Build a Query in MuleSoft With Optional Parameters
  • DataWeave Interview Question: Compare IDs From Two Arrays and Create a New Array
  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook