Pages

Sunday, November 30, 2008

Integrating ActiveMQ 5.1 with Tomcat 6

Step 1: Add resource entry to CATALINA_HOME/conf/server.xml under GlobalNamingResources..

    <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="LocalActiveMQBroker"
useEmbeddedBroker="false"
/>
<Resource
name="jms/Data"
auth="Container"
type="org.apache.activemq.command.ActiveMQTopic"
description="Data Topic"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="queue.data"
/>

Step 2: Add resource link entry to CATALINA_HOME/conf/context.xml (For God knows What reason, An entry the context.xml in META-INF of my webapp did not work)
<ResourceLink global="jms/ConnectionFactory" name="jms/ConnectionFactory" type="javax.jms.ConnectionFactory"/> 
<ResourceLink global="jms/Data" name="jms/Data" type="javax.jms.Topic"/>

Step 3: Add resource ref entry in web.xml
    <resource-ref> 
<res-ref-name>jms/ConnectionFactory</res-ref-name>
<res-type>javax.jms.ConnectionFactory</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<resource-ref>
<res-ref-name>jms/Data</res-ref-name>
<res-type>javax.jms.Topic</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

Step 4: Put the following jars in CATALINA_HOME/lib from ActiveMQ_HOME/lib
activemq-core-5.1.0.jar
commons-logging-1.1.jar
geronimo-j2ee-management_1.0_spec-1.0.jar
geronimo-jms_1.1_spec-1.1.1.jar
geronimo-jta_1.0.1B_spec-1.0.1.jar



Step 5: Start the server, you should be able to use JNDI to connect to ActiveMQ.


Step 6: When you use IntialContext in your code, first get the context of "java:comp/env".
    Context initCtx = new InitialContext(); 
jndiContext = (Context) initCtx.lookup("java:comp/env");
Then you can use:
connectionFactory = (ConnectionFactory)jndiContext.lookup("jms/ConnectionFactory");
destination = (Destination)jndiContext.lookup("jms/Data");


Then, carry on with normal JMS flow.

3 comments:

  1. Nice tips on integrating ActiveMQ 5.1 with Tomcat. Would you be interested in providing an article to Wazi - a new site sponsored by OpenLogic that offers tips and tricks for open source users/developers? Info here:
    http://tinyurl.com/5wcmxx

    Also, for what it is worth, OpenLogic doing webinar Dec 11 comparing JBoss, Glassfish, Geronimo, Tomcat, and the new SpringSource dm Server.
    http://tinyurl.com/647x7f

    ReplyDelete
  2. Hi Bret,

    I would love to contribute to the Wazi. I did go through the site, but could not find a way to register or create an account. Please let me know the details.

    Well, am interested for Webinar, but since I stay at India, it will 1:30 AM, so, I will not be able to join that.

    ..Shreyas..

    ReplyDelete
  3. Hi Bret, Please give your suggestions on below. Thanks for your time.

    I have a Restful service API developed with JAX-RS and jersey. I have deployed the same in TOMCAT 7. Now I would like to implement Activemq so that I would keep all request in a queue and process the request resource. How to do this and integrate with tomcat7. How to integrate ActiveMq with Tomcat7 or my rest service webapp. How to call the service.

    Important :- Inside the Rest Api, I am using FilterChaining concept for security concern and after verification of the calling party, I am simply forwarding the request to the resource. For this I have added in web.xml.

    Thanks

    ReplyDelete