Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Embedding Mule in a Java Application or Webapp

This page describes how to start and stop Mule from a Java application or to embed it in a Webapp (such as a JSP or servlet), and how to interact with Mule from your code in both scenarios.

Starting Mule from a Java Application

Before running Mule in embedded mode, be sure to manually update all the required dependencies for your application. The Mule ESB Embedded Single .jar contains all Mule classes, but the dependencies must be included manually.

To start Mule from any Java application, you can call one of its configuration builders. To use Mule XML configuration:


        
     
1
2
3
DefaultMuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
SpringXmlConfigurationBuilder configBuilder = new SpringXmlConfigurationBuilder("mule-config.xml");
muleContext = muleContextFactory.createMuleContext(configBuilder);

Make sure you store a reference to the MuleContext, as you will need it to stop Mule.

If you have multiple configuration files, you can provide a comma-separated list or an array of configuration files:


        
     
1
2
SpringXmlConfigurationBuilder configBuilder =
     new SpringXmlConfigurationBuilder(new String[] { "mule-config.xml", "another-config.xml" });

You then call the start method to start the server:


        
     
1
muleContext.start();

Stopping Mule from a Java Application

To stop Mule, you stop its context like this:


        
     
1
2
muleContext.stop();
muleContext.dispose();

Embedding Mule in a Webapp

To embed Mule inside a webapp, you provide one or more configuration file locations as context params and include a context listener to initialize the Mule Server. If you are using Mule XML configuration, use the following -


        
     
1
2
3
4
5
6
7
8
<context-param>
    <param-name>org.mule.config</param-name>
    <param-value>mule-config-main.xml,mule-components.xml</param-value>
</context-param>

<listener>
    <listener-class>org.mule.config.builders.MuleXmlBuilderContextListener</listener-class>
</listener>

The configuration parameter can be a classpath location or file location. You can also specify multiple configuration files on the classpath or on the file system.

Interacting with Mule from Your Code

To interact with the Mule server from your application, JSP, or servlet, you can use the Mule Client.


        
     
1
2
3
4
5
6
7
8
9
10
11
12
13
// in your servlet init() method save servletConfig.getServletContext() to a field
MuleContext muleContext = servletContext.getAttribute(MuleProperties.MULE_CONTEXT_PROPERTY);
// create a client
MuleClient client = new MuleClient(muleContext);

// send a jms message asynchronously
client.dispatch("jms://my.queue", "some data", null);

// or to receive a pop3 message via a configured mailbox
MuleMessage message = client.receive("pop3://myInboxProvider", 3000);

// or synchronous send a inter-vm message
MuleMessage message2 = client.send("vm://my.object", "Some more data", null);