Skip navigation

Embedding Mule in a Java Application

This page describes how to embed Mule in a Java application so you can start and stop it from that application. If you want to embed Mule inside a webapp, see Embedding Mule in a Webapp.

Starting Mule

To configure Mule from any Java application, you can call one of its configuration builders:

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

You can provide a comma-separated list of configuration files if you have more than one file. Make sure that you store a reference to the MuleContext, as you will need it to stop Mule.

Interacting with Mule from your Code

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

//create a client
MuleClient client = new MuleClient();

//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 synchronously send an inter-vm message
MuleMessage message2 = client.send("vm://my.object", "Some more data", null);

Stopping Mule

To stop Mule, just stop its context like this:

context.stop();
context.dispose();
Adaptavist Theme Builder Powered by Atlassian Confluence