Contact Us 1-800-596-4880

Embedding Mule in a Java Application or Web app

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

Starting Mule for a Java Application

Before running Mule in embedded mode in either a Java application or a Web app, be sure to manually update all the required dependencies for your application. The Mule Embedded Runtime JAR contains all Mule classes, but you need to include the dependencies manually.

Download the Mule Embedded Runtime JAR from https://developer.mulesoft.com/download-mule-esb-runtime to add to the classpath for your application before starting Mule.

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

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

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

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

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

You then call the start method to start the server:

muleContext.start();

Stopping Mule from a Java Application

To stop Mule, you stop its context like this:

muleContext.stop();
muleContext.dispose();

Embedding Mule in a Web app

Mule Standalone vs. Application Server

Listed below are some of the advantages of running Mule Standalone vs. running it as a web application deployed in an application server (Tomcat, WebSphere, JBoss, etc.)

  • Wrapper control: Mule Standalone makes use of a Java Service Wrapper which controls the JVM from your operating system and starts Mule. The wrapper can handle system signals and provides better interaction between the JVM and the underlying operating system.

  • Smaller memory footprint: Mule standalone loads only the required resources for an application; an application server adds its own resources.

  • Anypoint DataMapper support: DataMapper runs on Mule Enterprise Standalone runtime; other application servers do not support DataMapper.

  • Deployment and Application Management: Through the Mule Management Console, you have full control of the applications running on Mule Standalone, allowing you to deploy/undeploy applications and restart/stop the server. Other application servers do not support this visibility.

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

<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 of file location. You can also specify multiple configuration files on the classpath or on the file system.

Example

Access the Bookstore example application to witness Mule as an embedded as a web application inside a Servlet container.

Interacting with Mule from Your Code

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

// 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);