3

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;
                    }
2
  • This sounds as easy as just looping until you've read N messages, where N is the number requested by the user. Why won't that work for you and what specific problem are you getting?
    – alpian
    May 24, 2011 at 20:02
  • This type of feature falls more into the administrative features of a JMS Broker. Viewing the #msgs in a queue is not part of the JMS spec. Which JMS Provider are you using ? Very often there is a separate Admin API for that kind of thing. E.g. TIBCO EMS, ActiveMQ, IBM MQ all have this type of admin API. Jan 4, 2018 at 8:28

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.