Communicating with Geronimo’s JMS(ActiveMQ)

Posted on Posted in Java, Technology Center

My team member has a requirement to implement JMS. I was able to dig up some stuff about JMS in my blog. I’m re-posting it here since it might be useful. For this example, I used IBM Websphere Community Edition(IBM WAS CE), which is synonymous to Geronimo(at least for the first version).

Here are the simple steps:

1. Run Geronimo or IBM WAS CE
2. Compile these two classes. Take note that you need the ff. jar files to compile and run these successfully. Here’s the list:

– geronimo-j2ee_1.4_spec.jar
– commons-logging-1.0.4.jar
– concurrent-1.3.4.jar

Note : Some jars might have new versions so be sure to use the latest one.

You’ll find it somewhere under the Geronimo or WAS CE installation directory.

3. Run the clients.

The receiver class

import java.util.logging.Logger;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.activemq.ActiveMQConnectionFactory;

public class JMSReceiver {
    protected Queue queue;

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

    Logger logger = Logger.getAnonymousLogger();

    protected int ackMode = Session.AUTO_ACKNOWLEDGE;

    public static void main(String[] args) {
        JMSReceiver msgReceiver = new JMSReceiver();
        msgReceiver.run();
    }

    public void run() {
        QueueConnection connection;
        QueueSesion session;
        MessageConsumer consumer;
        try {
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
            onnection = (QueueConnection)connectionFactory.createConnection();
            connection.start();
            consumer = null;
            session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
            queue = session.createQueue(queueName);
            consumer = session.createReceiver(queue);
            logger.info(”Waiting for message (max 5)”);
            for (int i = 0; i < 5; i++) {
                Message message = consumer.receive();
                processMessage(message);
            }
            logger.info(”Closing connection”);
            } catch (Exception e) {
                logger.error(”Caught: ”,e);
            } finally {
                consumer.close();
                session.close();
                connection.close();
            }
        }
    }
    public void processMessage(Message message) {
         try {
            TextMessage txtMsg = (TextMessage) message;
            logger.info(”Received a message: ” + txtMsg.getText());
         } catch (Exception e) {
            logger.error(”Caught: ” , e);
         }
    }
}

The sender class

import java.util.logging.Logger;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage

import org.activemq.ActiveMQConnectionFactory;

public class JMSSender {

    private Queue receiveQueue = null;
    protected String queueName = “SendReceiveQueue”;
    protected String url = “tcp://localhost:61616″;
    Logger logger = Logger.getAnonymousLogger();

    public static void main(String[] args) {
        JMSSender msgSender = new JMSSender();
        msgSender.run();
    }

    public void run() {
        QueueConnection queueConn = null;
        QueueSession queueSess = null;
        TextMessage myMessage = null;
        QueueSender queueSender = null;

        try {
            ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);

            queueConn = factory.createQueueConnection();
            queueSess = queueConn.createQueueSession(false,Session.AUTO_ACKNOWLEDGE)
            receiveQueue = queueSess.createQueue(queueName);
            queueSender = queueSess.createSender(receiveQueue);
            myMessage = queueSess.createTextMessage();

            myMessage.setText(”test from JMS client 2″);
            queueSender.send(myMessage);

        } catch (Exception e) {
            logger.info(”Caught: ”, e);
        } finally {
            queueSender.close();
            queueSess.close();
            queueConn.close();
        }
    }
}


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.