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.
MuleClient client = new MuleClient();
client.dispatch("jms:, "some data", null);
MuleMessage message = client.receive("pop3:, 3000);
MuleMessage message2 = client.send("vm:, "Some more data", null);
Stopping Mule
To stop Mule, just stop its context like this:
context.stop();
context.dispose();