Combining ActiveMQ, Tomcat and Mule ESB

This post describes how we managed to use standalone ActiveMQ (v5.2.0) as JNDI datasource in our Mule ESB application deployed in Apache Tomcat(v6.0). If you are still with me after reading this first line then I may assume you know your way in this part of the Java open-source world 🙂
Here is the situation:
We have deployed a number (about 10) of Mule applications as WAR’s in our Tomcat installation. Each WAR is using the same standalone ActiveMQ instance as JMS provider. We implemented this by defining an ActiveMQ connector in each WAR (actually in a separate JAR that is included in each WAR).

However, recently we defined our database datasource as a JNDI resource in Tomcat. The big advantage is that it is only configured in just one place and maintained by the container instead of each WAR creating its own pool of connections.
So we decided we could do the same for the ActiveMQ datasource (actually I was triggered by this article).

So here are the steps we took to make this work:

  1. Define a global resource in server.xml of the Tomcat instance:
  2.   <Resource name="jms/ConnectionFactory"
              description="JMS Connection Factory"
  3. Define a global resourceLink in the context.xml of the Tomcat instance:
  4.   <ResourceLink name="jms/myJMS" global="jms/ConnectionFactory"
              type="org.apache.activemq.ActiveMQConnectionFactory" />
  5. Add to necessary libs to your /TOMCAT_HOME/lib directory:
  6. For your Tomcat instance to be able to work with the standalone ActiveMQ it must have loaded the necessary classes on its classpath. To make this possible you should copy the jar activemq-all-5.2.0.jar into your $TOMCAT_HOME$/lib directory.

These are the steps to setup Tomcat. Now you have to tell your Mule application it has to access this datasource instead creating its own pool. For this add the following to your Mule config:

  • Define your (Spring) datasource:
  •   <bean id="activeMqConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="java:comp/env/jms/shipJMS"/>
  • Define the activeMQ connection like this:
  •   <jms:activemq-connector name="JMSConnector" specification="1.1" dynamicNotification="true"
            <custom-exception-strategy class="nl.redstream.exceptionhandlers.DefaultConnectorExceptionHandler" enableNotifications="true">
                <vm:outbound-endpoint connector-ref="vmConnector" path="error-handler" />
            <ee:retry-forever-policy frequency="30000"/>

In the next post I show how to config your unit tests to work with this configuration

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in Mule2, Spring Framework and tagged , , . Bookmark the permalink.