Saturday, November 3, 2012

Setting up ActiveMQ with Tomcat 7

Step 1. Place the activemq-all-5.7.0.jar in [TOMCAT_HOME]/lib directory

Step 2. Open the [TOMCAT_ROOT]/conf/context.xml file and add the below resource to the Context.

<Context>
 <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"/>

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

</Context>
Step 3. Add the JNDI in your java code.
   InitialContext initCtx = new InitialContext();
        Context envContext = (Context) initCtx.lookup("java:comp/env");
        
        ConnectionFactory connectionFactory = (ConnectionFactory) envContext.lookup("jms/ConnectionFactory");
        Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue("jms/queue/Queue");
        MessageProducer producer = session.createProducer(destination);
        TextMessage msg = session.createTextMessage();
        msg.setText("Message sent");
        producer.send(msg);

3 comments:

  1. Existing system:
    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 Active Mq so that I would keep all request in a queue and process the api. How to do this and integrate with tomcat7. From this article; I would be able to integrate ActiveMq with Tomcat7. but how to call the service.

    Please guide me. I am in great need of your guidance.

    ReplyDelete
  2. how to use this in Dynamic Web Project in Eclipse.

    ReplyDelete