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

<Resource
name="jms/ConnectionFactory"
auth="Container"
type="org.apache.activemq.ActiveMQConnectionFactory"
description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="tcp://localhost:61616" brokerName="ActiveMQBroker"
useEmbeddedBroker="false"/>

<Resource
name="jms/topic/MyTopic"
auth="Container"
type="org.apache.activemq.command.ActiveMQTopic"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="APP.JMS.TOPIC"/>

<Resource
name="jms/queue/MyQueue"
auth="Container"
type="org.apache.activemq.command.ActiveMQQueue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="APP.JMS.QUEUE"/>

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:

-Dwebconsole.type=properties
-Dwebconsole.jms.url=tcp://localhost:61616
-Dwebconsole.jmx.url=service:jmx:rmi:///jndi/rmi://localhost:1099/Jmxrmi

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 :

<servlet>
<servlet-name>LoadActiveMQ</servlet-name>
<servlet-class>info.octera.utils.LoadActiveMQ</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

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 java.io.IOException;

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

import org.apache.activemq.broker.BrokerService;
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
.getName());

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

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

@Override
public void destroy() {
try {
logger.info("ActiveMQ exiting");
broker.stop();
logger.info("ActiveMQ exit succesfully");
} catch (Exception e) {
e.printStackTrace();
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
  • del.icio.us
  • 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