Configure Apache ActiveMQ with Tomcat 6

Hi,

Apache ActiveMQ:

Apache ActiveMQ is an open source (Apache 2.0 licensed) message broker which fully implements the Java Message Service 1.1 (JMS). It provides “Enterprise Features”[1] like clustering, multiple message stores, and ability to use any database as a JMS persistence provider besides VM, cache, and journal persist-ency. Below is the list of some other Main features.  ActiveMQ is often a good recommendation for SOA infrastructure projects.

Few Main Features:
1.  full support for the Enterprise Integration Patterns both in the JMS client and the Message Broker
2.  OpenWire for high performance clients in Java, C, C++, C#
3. Supports very fast persistence using JDBC along with a high performance journal
4. Fully supports JMS 1.1 and J2EE 1.4 with support for transient, persistent, transactional and XA messaging

Connectivity:
Apache ActiveMQ is designed to support a large number if Cross Language Clients, speak a wide range of Protocols and be usable in a wide variety of Containers in addition to being usable in a stand alone Java SE platform. Read more information in the below URL.
http://activemq.apache.org/connectivity.html

Integration with Tomcat 6:

Install the ActiveMQ related jars into the [TOMCAT_ROOT]/lib/
activemq-all-5.4.0.jar
commons-logging-1.1.jar

Step 1:

Open the [TOMCAT_ROOT]/conf/server.xml file and add the below resource to the GlobalNamingResources.


<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=”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 “/>

</GlobalNamingResources>

Step 2:
In the same location locate context.xml and add the below resource linking.

<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”/>

Step 3:
Create one small application in tomcat,  and Write One Servlet to publish a message to the Topic.

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.createTopic(“jms/topic/MyTopic”);
MessageProducer producer = session.createProducer(destination);
TextMessage msg=session.createTextMessage();
msg.setText(“Message sent”);
producer.send(msg);

Step 4:

Write the JMS Client to receive the messages

public class Reciever {

protected Topic queue;

protected String queueName = “jms/topic/MyTopic”;

protected String url = “tcp://localhost:61616”;

protected int ackMode = Session.AUTO_ACKNOWLEDGE;

public static void main(String[] args) {
Reciever rec=new Reciever();
try
{

rec.run();
} catch (Exception e) {
e.printStackTrace();
}

}

public void run() throws JMSException{

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
TopicConnection connection = (TopicConnection)connectionFactory.createTopicConnection();

connection.start();
MessageConsumer consumer = null;
Session session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
queue = session.createTopic(queueName);
consumer = session.createConsumer(queue);

System.out.println(” Waiting for message (max 5) “);

for (int i = 0; i < 5; i++) {
Message message = consumer.receive();
processMessage(message);

}

System.out.println(“Closing connection”);
consumer.close();
session.close();
connection.close();

}
public void processMessage(Message message) {

try {

TextMessage txtMsg = (TextMessage) message;

System.out.println(“Received a message: ” + txtMsg.getText());

} catch (Exception e) {

e.printStackTrace();

}
}

Step 5:

Now configure the ActiveMQ properties in Tomcat classpath.

Edit catalina.bat [TOMCAT_ROOT]/bin/

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

Or if you are using Tomcat windows service then inside “java” tab

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

Once this is done, start the server and access the servlet to send message to the Topic.  And Open a new command console and run the message receiver to receive the messaged being published to the Topic.

[PS] If you face any exception “Connection Refused”, then simply add one class in tomcat classpath to start the ActiveMQ Broker Service, Or you can also add one jsp which will initialize the BrokerService on App start.

<%
BrokerService broker = new BrokerService();
// configure the broker
broker.addConnector(“tcp://localhost:61616”);
broker.start();

//use broker.stop() to stop the service.
%>

[NOTE]:
ActiveMQ
The ActiveMQ Web Console is a web based administration tool for working with ActiveMQ. When used with the JMX support it can be an invaluable tool for working with ActiveMQ. Read below URL for more details.
http://activemq.apache.org/web-console.html

Hope this quick guide would help.


Thanks
R Vashi

Advertisements

13 thoughts on “Configure Apache ActiveMQ with Tomcat 6

  1. Mate!!! thanks for this post.. really nice one, saw ur blog… really very much impressed.. simply bookmarked the link… good job overall.. keep the fire.. cheers!!

  2. Hi R Vashi,

    Could you email me your source code so that I can see what I’m doing wrong? I’m newbie in these technologies. Thanks in advance.
    PS:. Also you catalina.bat

    • HI Adriano,

      Could you pls tell me which Web container/APp server you are using. THat would help me to pass you the configuration accordingly.


      THanks
      RVashi

      • Hi RVashi,
        I’m using tomcat 6.0.20 under Mac OS X. My problems are mainly with steps 3,4 and 5 of your post. That’s why I’d like the source code. I hope this clarifies.
        Thanks in advance.

  3. Hi Adriano,

    I have done one mini-application to send text messages to a topic and the is one subscriber launched at the start that receeives the messages.

    I would want to send messages to this topic from the activemq webconsole, but when i send a message from the console it’s not arriving to my topic, althought i’am putting the same name to the topic than the configured in server.xml, for the attribute physicalName in topic reource.

    Could you help me?

    Thanks in advance, Miguel.

  4. Hi, thanks for the post, is very useful.

    Everything works fine if I create Queues or Topic from ActiveMQ console.
    I’d like to publish them with the catalina configuration

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

    I doesn’t work.

    You suggested to

    [PS] If you face any exception “Connection Refused”, then simply add one class in tomcat classpath to start the ActiveMQ Broker Service, Or you can also add one jsp which will initialize the BrokerService on App start.

    But, can you please explain why it doesn’t work, and if it’s possible to do something else.
    I don’t want to create a class in tomcat or a jsp, because I need to create a clean installation tool for the deploy (so, i just wanto to have a tomcat the clearest as it is possible).

    Thanks

    Vittorio

  5. Hi there,,,
    I do the same steps but finally I got this error:
    org.apache.activemq.command.ActiveMQTopic cannot be cast to javax.jms.Queue

    can u pls help

  6. I’m unable to proceed with these steps with IDE Eclipse.can u pls share this source code project to me.
    email:chapalanagendrababu@gmail.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s