Contact Us 1-800-596-4880

Mule Filters Wrappers and Routers

Mule includes a variety of message processors designed to facilitate message routing or filtering, and message processor wrapping.

filter2 Mule filters evaluate a message to determine whether it can proceed through a flow. The simplest filters implement basic logic operators (such as and, or, and not), but these simple elements can be combined in various ways to specify complex logical conditions. For example, you can use a filter at the beginning of your flow to reject any requests from a particular range of IP addresses, or simply use a filter to reject any messages with a particular payload, or with a null payload.

wrapper2 Mule wrappers – or scopes, as they’re known within Mule Studio – work to encapsulate other message processors so that they function as a single unit. You might wrap several message processors together to form a transactional unit, so that they succeed or fail to process a message together, thus ensuring accurate updating of a database, for example. You might wrap several message processors together within a cache scope to store the result of their processing for reuse, or wrap a single message processor within a message enricher scope to add to a message payload without manipulating the original contents.

router2 Mule routers – or flow controls, as they’re known in Mule Studio – function much as their name implies: to direct or otherwise control messages within a flow.

At times, flow controls act as splitters, resequencers, or aggregators, splitting messages into individual items for processing, resequencing message content, or aggregating split messages.

At times, they act as forks in the road, evaluating a message’s payload or header, then routing to the message down a particular "pathway" according to those contents. Thus, flow controls facilitate content-based routing. For example, you can use a choice flow control to examine part of a message payload, then route the message to the first "pathway" for which the set condition evaluates to true, such as where an order exceeds $5,000.

Examples

Expression Filter

<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core" version="EE-3.4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
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">

    <flow name="EchoFlow" doc:name="EchoFlow">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8084" doc:name="HTTP" doc:description="Process HTTP requests or responses."/>
        <expression-filter expression="#[groovy:payload.getClass().getName() != 'org.mule.transport.NullPayload']" doc:name="Expression"/>
        <logger message="About to echo #[message.payload]" level="INFO" doc:name="Logger"/>
        <echo-component doc:name="Echo"/>
    </flow>

</mule>

Message Enricher and Cache Scope

Message Enricher and Cache
<mule xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:jms="http://www.mulesoft.org/schema/mule/jms"

    xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:http="http://www.mulesoft.org/schema/mule/http"

    xmlns:jdbc-ee="http://www.mulesoft.org/schema/mule/ee/jdbc" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"

    xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:jdbc="http://www.mulesoft.org/schema/mule/ee/jdbc"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="EE-3.4.0"

    xsi:schemaLocation="http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd

http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.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/ee/jdbc http://www.mulesoft.org/schema/mule/ee/jdbc/current/mule-jdbc-ee.xsd">

...

    <flow name="inhouseOrder" doc:name="inhouseOrder">
        <jms:inbound-endpoint queue="inhouseOrder"
            connector-ref="Active_MQ" doc:name="inhouseOrder">
            <xa-transaction action="ALWAYS_BEGIN" />
        </jms:inbound-endpoint>
        <set-variable variableName="price" value="0"
            doc:name="Initialise Price" />
        <enricher target="#[price]" doc:name="Enrich with price">
            <ee:cache cachingStrategy-ref="Caching_Strategy" doc:name="Cache the Price">
                <http:outbound-endpoint exchange-pattern="request-response"
                    host="localhost" port="9999" path="api/prices/#[payload.productId]"
                    method="GET" disableTransportTransformer="true" doc:name="Invoke Price Service" />
                <object-to-string-transformer doc:name="Object to String" />
            </ee:cache>
        </enricher>
        <jdbc-ee:outbound-endpoint exchange-pattern="one-way"
            queryKey="insertOrder" queryTimeout="-1" connector-ref="JDBCConnector"
            doc:name="Save Order Item">
            <xa-transaction action="ALWAYS_JOIN" />
            <jdbc-ee:query key="insertOrder"
                value="insert into orders (product_id, name, manufacturer, quantity, price) values (#[payload.productId], #[payload.name], #[payload.manufacturer], #[payload.quantity], #[price])" />
        </jdbc-ee:outbound-endpoint>
        <set-variable variableName="totalPrice" value="#[price * payload.quantity]"
            doc:name="totalPrice = price * payload.quantity" />
        <set-session-variable variableName="totalValue"
            value="#[totalValue + totalPrice]" doc:name="totalValue += totalPrice" />
        <scripting:transformer doc:name="Groovy">
            <scripting:script engine="Groovy">
                <scripting:text><![CDATA[receipt = new com.mulesoft.se.orders.PurchaseReceipt(); receipt.setStatus(com.mulesoft.se.orders.Status.ACCEPTED); receipt.setTotalPrice(Float.valueOf(message.getInvocationProperty('totalPrice')));
return receipt;]]></scripting:text>
            </scripting:script>
        </scripting:transformer>
        <rollback-exception-strategy
            maxRedeliveryAttempts="3" doc:name="Rollback Exception Strategy">
            <logger message="#[payload:]" level="INFO" doc:name="Logger" />
            <on-redelivery-attempts-exceeded
                doc:name="Redelivery exhausted">
                <flow-ref name="defaultErrorHandler" doc:name="Invoke defaultErrorHandler" />
            </on-redelivery-attempts-exceeded>
        </rollback-exception-strategy>
    </flow>
...

</mule>

Choice Router

Choice Router
<mule
...
    xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:http="http://www.mulesoft.org/schema/mule/http"
...
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="EE-3.4.0"
    xsi:schemaLocation="http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd

http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd

http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">

...
    <flow name="orderService" doc:name="orderService">
        <http:inbound-endpoint exchange-pattern="request-response"
            host="localhost" port="1080" path="orders" doc:name="/orders"
            doc:description="Process HTTP reqests or responses." connector-ref="HttpConnector" />
        <cxf:jaxws-service serviceClass="com.mulesoft.se.orders.IProcessOrder"
            doc:name="Order WebService" doc:description="Make a web service available via CXF" />
        <set-session-variable variableName="totalValue"
            value="0" doc:name="totalValue=0" />
        <foreach collection="#[payload.orderItems]" doc:name="For each Order Item">
            <enricher
                target="#[rootMessage.payload.orderItems[counter - 1].purchaseReceipt]"
                doc:name="Enrich with purchase receipt">
                <choice doc:name="Choice">
                    <when expression="#[payload.manufacturer == 'Samsung']">
                        <vm:outbound-endpoint exchange-pattern="request-response"
                            path="samsungOrder" doc:name="Dispatch to samsungOrder" />
                    </when>
                    <otherwise>
                        <jms:outbound-endpoint exchange-pattern="request-response"
                            queue="inhouseOrder" connector-ref="Active_MQ" doc:name="Dispatch to inhouseOrder" />
                    </otherwise>
                </choice>
            </enricher>
        </foreach>
        <vm:outbound-endpoint exchange-pattern="one-way"
            path="audit" responseTimeout="10000" mimeType="text/plain" doc:name="Dispatch to audit" />
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <flow-ref name="defaultErrorHandler" doc:name="Invoke defaultErrorHandler" />
        </catch-exception-strategy>
    </flow>
    ...

</mule>

See Also