JAVA, JMS broswing

How do I browse a queue in JMS

Could someone post a short example

Los Angeles1Asked:
Who is Participating?
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
Seriously?  This is from the first Google search result for "java jms browser example"...
import java.util.Enumeration;
import javax.naming.InitialContext;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Message;
import javax.jms.QueueSession;
import javax.jms.QueueBrowser;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
public class Browser
    public static void main(String[] args) throws Exception
        // get the initial context
        InitialContext ctx = new InitialContext();
        // lookup the queue object
        Queue queue = (Queue) ctx.lookup("queue/queue0");
        // lookup the queue connection factory
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.
        // create a queue connection
        QueueConnection queueConn = connFactory.createQueueConnection();
        // create a queue session
        QueueSession queueSession = queueConn.createQueueSession(false,
        // create a queue browser
        QueueBrowser queueBrowser = queueSession.createBrowser(queue);
        // start the connection
        // browse the messages
        Enumeration e = queueBrowser.getEnumeration();
        int numMsgs = 0;
        // count number of messages
        while (e.hasMoreElements()) {
            Message message = (Message) e.nextElement();
            // Do something with each Message here

        System.out.println(queue + " has " + numMsgs + " messages");
        // close the queue connection

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.