Mule regex filter example


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.


<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="" xmlns:xsi=""
	xmlns:vm="" xmlns:spring=""
	xsi:schemaLocation="     ">

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

	<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"/>
	<message-filter name="okOrComplete">
        <regex-filter pattern="OK|COMPLETE"/>
    <message-filter name="okOrCompleteFilterByRef">
        <filter ref="regFilterOkOrComplete"/>
    <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 name="flow4">
		<vm:inbound-endpoint address="vm://filter.statusUpdate2" exchange-pattern="one-way"/>
		<processor ref="okOrCompleteFilterByRef"/>
		<component class="com.javarticles.mule.TestComponent"/>

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


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.


package com.javarticles.mule;


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();
        SpringXmlConfigurationBuilder configBuilder = new SpringXmlConfigurationBuilder(
        MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder();
        MuleContextFactory contextFactory = new DefaultMuleContextFactory();
        MuleContext ctx = contextFactory.createMuleContext(configBuilder,
        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 {
            } catch (InterruptedException e) {
        } finally {
    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);


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:

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.