DevPinoy.org
A Filipino Developers Community
   
Communicating with Geronimo’s JMS(ActiveMQ)

I was reading this article from IBM about how to communicate with Geronimo’s JMS server(ActiveMQ). The sample for the non-J2EE client does not work!

What I did is to take the code from the article and edit the typo and voila! I got some prototype code! I am using IBM Websphere Community Edition(IBM WAS CE), which is synonymous to Geronimo(at least for the first version). By the way, I added the sender code.

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

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() {
   try {
     ActiveMQConnectionFactory connectionFactory =
     new ActiveMQConnectionFactory(url);
     QueueConnection connection = (QueueConnection)connectionFactory.createConnection();
      connection.start();

     MessageConsumer consumer = null;

     QueueSession 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”);
     consumer.close();
     session.close();
     connection.close();

     } catch (Exception e) {
          logger.info(”Caught: ” + e);
          e.printStackTrace();
     }
}

public void processMessage(Message message) {
     try {
          TextMessage txtMsg = (TextMessage) message;
          logger.info(”Received a message: ” + txtMsg.getText());
     } catch (Exception e) {
          logger.info(”Caught: ” + e);
          e.printStackTrace();
     }
}

}

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

  queueSender.close();
  queueSess.close();
  queueConn.close();

  } catch (Exception e) {
    logger.info(”Caught: ” + e);
    e.printStackTrace();
  }
}
}

Anyway, I hope this code might be useful for those interested in JMS. Enjoy and happy coding! If you have queries or if anything is not working, just drop me a note.


Posted 09-22-2006 2:26 AM by javazealot

Comments

lamia wrote re: Communicating with Geronimo’s JMS(ActiveMQ)
on 09-21-2006 8:02 PM

At last!!! Another Java Developer in the house! Or are you not...?

javazealot wrote re: Communicating with Geronimo’s JMS(ActiveMQ)
on 09-21-2006 8:13 PM

Yes I am :)

lamia wrote re: Communicating with Geronimo’s JMS(ActiveMQ)
on 09-21-2006 8:35 PM

Kewl! Now there's two of us here. :)

javazealot wrote re: Communicating with Geronimo’s JMS(ActiveMQ)
on 09-21-2006 10:40 PM

Really?! Maybe I can ask my friends to join here.

Copyright DevPinoy 2005-2008