'Reading a message from a Queue and display it to the user?

I want to know first how many messages are already existing in a Queue. The following class Browser will return the number of messages existing in Queue. Now, I want user to enter the number of messages to be read from a queue and display only that number of messages to the client. I do not want to read all the messages from a queue but only those number of messages which user wants to read. Please check the code and reply what should be done.

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.
    |       lookup("queue/connectionFactory");
    |                                                                      
    |   // create a queue connection
    |   QueueConnection queueConn = connFactory.createQueueConnection();
    |                                                                      
    |   // create a queue session
    |   QueueSession queueSession = queueConn.createQueueSession(false,
    |       Session.AUTO_ACKNOWLEDGE);
    |                                                                      
    |   // create a queue browser
    |   QueueBrowser queueBrowser = queueSession.createBrowser(queue);
    |                                                                      
    |   // start the connection
    |   queueConn.start();
    |                                                                      
    |   // browse the messages
    |   Enumeration e = queueBrowser.getEnumeration();
    |   int numMsgs = 0;
    |                                                                      
    |   // count number of messages
    |   while (e.hasMoreElements()) {
    |   |   Message message = (Message) e.nextElement();
    |   |   numMsgs++;
    |   }
    |                                                                      
    |   System.out.println(queue + " has " + numMsgs + " messages");
    |                                                                      
    |   // close the queue connection
    |   queueConn.close();
    }
}


To read the number of messages as per user's requirements....
String NUMBER = request.getParameter("number"); 
.......
.......
.......
connection.start();
            for (int s = 0; s <= Integer.parseInt(NUMBER); s++){
             while (true){
                Message m = qReceiver.receive();
                if (m != null){
                    if (m instanceof BytesMessage){
                        BytesMessage bytesMessage = (BytesMessage)m;
                        PrintStream buffer = null;
                        for ( int i = 0; i < (int)bytesMessage.getBodyLength(); i++) {
                            buffer.append((char) bytesMessage.readByte());
                            }
                            String msg = buffer.toString().trim();

                            System.out.println("Reading Message: " + msg);
                       }else if (m instanceof TextMessage){
                        TextMessage textMessage = (TextMessage)m;
                        System.out.println("Reading message: " + textMessage.getText());
                    }else {
                        break;
                    }


Solution 1:[1]

i assume that you are using a MessageListner, so OnMessage() method will be called once a message comes up. In this method you can keep a counter, once this counter reaches maximum allowed, you can call connection.stop() to stop consuming messages from the queue.

You can always restart by calling connection.start() again.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ravi Bhatt