Mule Context Example

0

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

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javarticles.camel</groupId>
	<artifactId>camelRecipientList</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>org.mule</groupId>
			<artifactId>mule-core</artifactId>
			<version>3.8.1</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.12</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.12</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.1.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.mule.modules</groupId>
			<artifactId>mule-module-spring-config</artifactId>
			<version>3.8.1</version>
		</dependency>
		<dependency>
			<groupId>org.mule.transports</groupId>
			<artifactId>mule-transport-vm</artifactId>
			<version>3.8.1</version>
		</dependency>
		<dependency>
			<groupId>org.mule.modules</groupId>
			<artifactId>mule-module-client</artifactId>
			<version>3.8.1</version>
		</dependency>
	</dependencies>
</project>

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.

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 ">

	<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>

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

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.

MuleContextExample:

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

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

MuleCustomProcessor:

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;
    }

}

Output:

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: muleContextExample.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.