Java : Embedding Mule 2 into an existing Spring application

This page last changed on Jun 29, 2010 by Kees de Kooter

Intro

Mule does not provide a default way of embedding itself in an existing spring app.

Bootstrapping a Mule context

Instead I wrote a bean that bootstraps the mule context and connects it to the parent application context.

import org.mule.api.MuleContext;
import org.mule.api.MuleException;
import org.mule.api.config.ConfigurationException;
import org.mule.api.lifecycle.InitialisationException;
import org.mule.config.spring.SpringXmlConfigurationBuilder;
import org.mule.context.DefaultMuleContextFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Mule 2 does not support being embedded into an existing Spring application.
 * This bean performs the startup for us.
 * 
 * @see http://www.mulesoft.org/jira/browse/MULE-2809
 */
public class MuleStarter implements InitializingBean, DisposableBean, ApplicationContextAware {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private String contextPathName;
    private int startDelay = 0;
    private MuleContext muleContext;
    private ApplicationContext applicationContext;

    public MuleStarter(String contextPathName) {
        this.contextPathName = contextPathName;
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void afterPropertiesSet() throws Exception {

        if (startDelay > 0) {
            Thread.currentThread();
            Thread.sleep(startDelay);
        }

        DefaultMuleContextFactory contextFactory = new DefaultMuleContextFactory();
        try {
            SpringXmlConfigurationBuilder configurationBuilder = 
                new SpringXmlConfigurationBuilder(contextPathName);
            configurationBuilder.setParentContext(applicationContext);
            muleContext = contextFactory.createMuleContext(configurationBuilder);

            muleContext.start();

            logger.info("Started Mule Context " + contextPathName);
        } catch (ConfigurationException e) {
            logger.error(e.toString());
        } catch (InitialisationException e) {
            logger.error(e.toString());
        } catch (MuleException e) {
            logger.error(e.toString());
        }
    }

    public void destroy() throws Exception {
        if (muleContext.isStarted()) {
            muleContext.stop();
            logger.info("Stopped Mule Context " + contextPathName);
        }
    }

    public void setContextPathName(String contextPathName) {
        this.contextPathName = contextPathName;
    }

    public void setStartDelay(int startDelay) {
        this.startDelay = startDelay;
    }
}

Wiring beans