DefaultMuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
SpringXmlConfigurationBuilder configBuilder = new SpringXmlConfigurationBuilder("mule-config.xml");
muleContext = muleContextFactory.createMuleContext(configBuilder);
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 from 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:
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:
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 Webapp
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.)
|
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 -
<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.
// 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);