Mule regex filter example

0

In this article we will see how to filter textual messages using regular expression. We will use regex-filter element, specify the regular expression in pattern attribute.

  1. In our first example, we will allow all messages. pattern contains just ‘.’ representing any text.
  2. In our second flow flow2, we allow only numericals with pattern ^[0-9].
  3. In the third and fourth flows, we use message-filter processor, we will use processor way if a message filter is already available to reuse. The pattern “OK|COMPLETE” accepts status which is either OK or COMPLETE.

muleContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:spring="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd      
       http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.8/mule-vm.xsd
       http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.8/mule.xsd ">

	<flow name="flow1">
		<vm:inbound-endpoint address="vm://filter.allowAll" exchange-pattern="one-way"/>
		<regex-filter pattern="." />
		<component class="com.javarticles.mule.TestComponent"/>
	</flow>

	<flow name="flow2">
		<vm:inbound-endpoint address="vm://filter.onlyNum" exchange-pattern="one-way"/>
		<regex-filter pattern="^[0-9]" />
		<component class="com.javarticles.mule.TestComponent"/>
	</flow>
	
	<message-filter name="okOrComplete">
        <regex-filter pattern="OK|COMPLETE"/>
    </message-filter>
    
    <message-filter name="okOrCompleteFilterByRef">
        <filter ref="regFilterOkOrComplete"/>
    </message-filter>
    
    <regex-filter pattern="OK|COMPLETE" name="regFilterOkOrComplete"/> 	
	
	<flow name="flow3">
		<vm:inbound-endpoint address="vm://filter.statusUpdate1" exchange-pattern="one-way"/>
		<processor ref="okOrComplete"/>
		<component class="com.javarticles.mule.TestComponent"/>
	</flow>	
	
	<flow name="flow4">
		<vm:inbound-endpoint address="vm://filter.statusUpdate2" exchange-pattern="one-way"/>
		<processor ref="okOrCompleteFilterByRef"/>
		<component class="com.javarticles.mule.TestComponent"/>
	</flow>		
</mule>

Once the message passes thru the filter, it is sent to the test component which simply prints the message and returns it.

TestComponent:

package com.javarticles.mule;


public class TestComponent {

    public Object doSomething(Object inPayload) {
        System.out.println("do something with '" + inPayload + "'");
        return inPayload;
    }

}

We will now invoke the flow, dispatch different set of payloads.

MuleRegExExample:

package com.javarticles.mule;

import java.io.IOException;

import org.mule.api.MuleContext;
import org.mule.api.MuleException;
import org.mule.module.client.MuleClient;
import org.mule.api.context.MuleContextBuilder;
import org.mule.api.context.MuleContextFactory;
import org.mule.config.DefaultMuleConfiguration;
import org.mule.config.spring.SpringXmlConfigurationBuilder;
import org.mule.context.DefaultMuleContextBuilder;
import org.mule.context.DefaultMuleContextFactory;

public class MuleRegExExample {
    public static void main(String[] args) throws IOException, MuleException {
        DefaultMuleConfiguration dmc = new DefaultMuleConfiguration();
        dmc.setId("muleexample");
        dmc.setWorkingDirectory("/esb/mule");
        SpringXmlConfigurationBuilder configBuilder = new SpringXmlConfigurationBuilder(
                "muleContext.xml");
        MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder();
        contextBuilder.setMuleConfiguration(dmc);
        MuleContextFactory contextFactory = new DefaultMuleContextFactory();
        MuleContext ctx = contextFactory.createMuleContext(configBuilder,
                contextBuilder);
        ctx.start();
        try {
            MuleClient muleClient = new MuleClient(ctx);
            dispatch(muleClient, "vm://filter.allowAll", "XYZ");
            dispatch(muleClient, "vm://filter.onlyNum",  123);
            dispatch(muleClient, "vm://filter.onlyNum", "XYZ");
            dispatch(muleClient, "vm://filter.statusUpdate1", "OK");
            dispatch(muleClient, "vm://filter.statusUpdate1", "COMPLETE");
            dispatch(muleClient, "vm://filter.statusUpdate1", "ERROR");
            dispatch(muleClient, "vm://filter.statusUpdate2", "OK");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
        } finally {
            ctx.dispose();
        }
    }
    
    private static void dispatch(MuleClient muleClient, String uri, Object payload) throws MuleException {
        System.out.println("Sending payload [" + payload + "] to " + uri);
        muleClient.sendNoReceive(uri, payload, null);
    }
}

Output:

Sending payload [XYZ] to vm://filter.allowAll
Sending payload [123] to vm://filter.onlyNum
Sending payload [XYZ] to vm://filter.onlyNum
Sending payload [OK] to vm://filter.statusUpdate1
Sending payload [COMPLETE] to vm://filter.statusUpdate1
do something with 'XYZ'
Sending payload [ERROR] to vm://filter.statusUpdate1
do something with 'OK'
do something with '123'
do something with 'COMPLETE'
Sending payload [OK] to vm://filter.statusUpdate2
do something with 'OK'

Download the source code

This was an example about mule regex filter .

You can download the source code here: muleRegExExample.zip

About Author

Ram’s expertise lies in test driven development and re-factoring. He is passionate about open source technologies and loves blogging on various java and open-source technologies like spring.
You can reach him at [email protected]

Comments are closed.