Using JMS in Tomcat

Hi all,
Recently I had to use JMS topic in my application. problem was I was working with Tomcat web application.
Tomcat doesnt ship with JMS capabitilites but you can configure and use external JMS and plug it into Tomcat.

There are couple of JMS vendors. I chose to use ActiveMQ.

So first you need to add the following jars to your tomcat lib dir:

activemq-all-5.4.0.jar
commons-logging-1.1.jar

Next, add the resource below to conf/server.xml:










next, add this code to context.xml:




Next, We need to add ActiveMQ props into tomcat class path:

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

In order to start and init our JMS queue we need to add the following code to some class and start it as service so it will get started on server startup:

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

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

Now let’s create small application to demonstrate some Topic movemonet:

Producer:

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);

Reciever:
(Now We create some Client in order to retrieve any message which will be dispatch onto our JMS topic)

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();

}
}

Idan.

Related Articles:

(31084)