How to embed ActiveMQ in Tomcat

This post will explain how you can embed ActiveMQ in Tomcat and how start/stop itself as a WebApp

Step 1

You need to download ActiveMQ on their website, after that, unzip the files. Here you can launch it as a standalone application to make some tests.
To be able to launch ActiveMQ directly in Tomcat you must copy the following jar in the commons/lib/ directory of tomcat :

  • activemq-core-5.5.0.jar
  • geronimo-j2ee-management_1.1_spec-1.0.1.jar
  • geronimo-jms_1.1_spec-1.1.1.jar
  • geronimo-jta_1.0.1B_spec-1.0.1.jar
  • kahadb-5.5.0.jar
  • log4j-1.2.14.jar
  • slf4j-api-1.5.11.jar
  • slf4j-log4j12-1.5.11.jar

Version number may changes.

Step 2

Now we are going to configure tomcat to embed ActiveMQ.

Open server.xml file from tomcat directory, and add the following code in the GlobalNamingResources section

description="JMS Connection Factory"
brokerURL="tcp://localhost:61616" brokerName="ActiveMQBroker"



This code make an instance of ActiveMQ on localhost:61616, and create a Topic and a Queue.

Now open context.xml files and add this in the context section:

<ResourceLink global="jms/ConnectionFactory" name="jms/ConnectionFactory" type="javax.jms.ConnectionFactory"/>
<ResourceLink global="jms/topic/MyTopic" name="jms/topic/MyTopic" type="javax.jms.Topic"/>
<ResourceLink global="jms/queue/MyQueue" name="jms/queue/MyQueue" type="javax.jms.Queue"/>

This will link the global name of the ressources to a name that you can use in your application.

Step 3

In order to activate ActiveMQ you need to add the following line in the tomcat startup script:


The last line will open JMX to allow monitoring of ActiveMQ. Remember to disable it in production or protect it with password!

Step 4

We need to launch ActiveMQ with our Web Application, so a sevlet can do that. This servlet will open ActiveMQ when started, and close when finished.

First, open the web.xml file of your application in the WEB-INF directory, and add the following line :


These line will create a servet and launch it when the application is started by tomcat, this servlet is automatically destroy when the application is exited.

Now the servlet code :

package info.octera.utils;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class LoadActiveMQ extends HttpServlet {

* serialVersionUID for serialization
private static final long serialVersionUID = -8289081958495740549L;

private BrokerService broker;

private static final Logger logger = Logger.getLogger(LoadActiveMQ.class

public void init() throws ServletException {
// / Automatically java script can run here"Load activeMQ");
// configure the broker
try {
broker = new BrokerService();
broker.start();"ActiveMQ loaded succesfully");
} catch (Exception e) {
logger.error("Unable to load ActiveMQ!");

public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

public void destroy() {
try {"ActiveMQ exiting");
broker.stop();"ActiveMQ exit succesfully");
} catch (Exception e) {
logger.error("Unable to exit ActiveMQ!");

Deploy your application, you will see in the log that ActiveMQ is correctly loaded and now listen on localhost:61616!

Share and Enjoy:
  • Print
  • Facebook
  • Digg
  • Sphinn
  • Mixx
  • Google Bookmarks
  • Add to favorites
  • MSN Reporter
  • MySpace

Tags: , , , ,

1 comment

  1. prince

Trackback e pingback

No trackback or pingback available for this article

Leave a Reply