Mule Context Example


In this article we will see an example of creating and starting a mule context programatically. Mule is an event driven enterprise service bus where the message is passed around a chain of processors, allows you to transform, enrich and re-route the message.

Mule Setup


<project xmlns="" xmlns:xsi=""

Mule Configuration XML

We have kept the mule configuration pretty simple as our main objective in this article is to get a feel of the mule components. Mule uses flow as the main control structure in its configuration. Our example contains two flows flow1 and flow2. A flow starts with a source of message. Here it is the inbound endpoint vm://request.msg. In this article, we will come across the below mule components.

  • Mule message – This is the message that is passed around through each flow in form of a mule event.
  • Message sources – A flow starts with a message source, in our case, it is vm inbound endpoint
  • Message processor – Processes the message. It can be an outbound endpoint that can dispatch the message to a different flow or a java class that can work upon the message received for example, modify the message or do some business operation based on the message.
  • Exchange patterns – Defines the way endpoint interaction takes place which is wither synchronous
    or asynchronous. It is defined by attribute exchange-pattern. It can be one-way or request-response./li>
  • Endpoints – An endpoint enables an application to send and receive messages. An inbound endpoint acts as a message source whereas an outbound endpoint acts as a destination. In our example, we have used a vm endpoint which can be thought of as an in-memory queue that allows you
    to integrate different flows.

We have used VM endpoints to tie flow1 and flow2 together. Since we have used one-way, the calling flow is expecting a response so events generated from one-way endpoints will be executed asynchronously. When we post a message to the vm endpoint, flow flow1 receives the mule message and then passes the message to the outbound vm endpoint. Thereafter the message goes through flow2 and is finally processed by MuleCustomProcessor.


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

	<vm:connector name="testConnector"/>

	<flow name="flow1">
		<vm:inbound-endpoint address="vm://request.msg"
			exchange-pattern="one-way" connector-ref="testConnector" />
		<vm:outbound-endpoint address="vm://handle.msg" />

    <flow name="flow2">
        <vm:inbound-endpoint address="vm://handle.msg"/>
        <custom-processor class="com.javarticles.mule.MuleCustomProcessor"/>

Mule flow

Mule Context

We first create a mule context programatically using the mule configuration XML and then start the mule context.
Next we create a mule client and send message to vm://request.msg.


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 MuleContextExample {
    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);
            String payload = "XYZ";
            System.out.println("Sending payload [" + payload + "]");
            muleClient.sendNoReceive("vm://request.msg", payload, null);
            try {
            } catch (InterruptedException e) {
        } finally {

In our custom processor we simply receive the event, extract the message and print the payload.


package com.javarticles.mule;

import org.mule.api.MuleEvent;
import org.mule.api.MuleException;
import org.mule.api.MuleMessage;
import org.mule.api.processor.MessageProcessor;

public class MuleCustomProcessor implements MessageProcessor {

    public MuleEvent process(MuleEvent inEvent) throws MuleException {
        MuleMessage message = inEvent.getMessage();
        Object o = message.getPayload();   
        System.out.println("Payload: " + o);
        return inEvent;



Sending payload [XYZ]
Payload: XYZ

Download the source code

This was an article about creating and starting mule context programatically.

You can download the source code:

Comments are closed.