Java JMS QueueBrowser browse messages

Description

Java JMS QueueBrowser browse messages

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;

public class Main {
   public static void main(String[] args) {
      try {//from www. j ava 2s  . c om
         Context ctx = new InitialContext();
         ConnectionFactory cf = (ConnectionFactory) ctx.lookup(args[0]);
         Connection con = cf.createConnection();
         Session sn = con.createSession(false, Session.AUTO_ACKNOWLEDGE);

         Destination dest = sn.createTopic("ATopic");
         QueueBrowser browser = sn.createBrowser((Queue) dest);
         java.util.Enumeration msgs = browser.getEnumeration();
         if (!msgs.hasMoreElements())
            System.out.println("No messages in queue");
         else {
            while (msgs.hasMoreElements()) {
               Message tempMsg = (Message) msgs.nextElement();
               System.out.println("Message: " + tempMsg);
            }
         }
         sn.close();
         con.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}



PreviousNext

Related