1 Reply Latest reply on Oct 23, 2011 1:02 PM by everson .

    consume message inside a QueueBrowser loop

    everson . Newbie

      I would like after do something consume/delete/take out the message from queue, but I can't ....

      I have  4 messages on queue and I want consume just one


      QueueConnectionFactory factory = (QueueConnectionFactory) getInitialContext().lookup("/ConnectionFactory");//java:/ConnectionFactory

                  Queue queue = (Queue) getInitialContext().lookup("queue/MyQueue");

                  connection  = factory.createQueueConnection();

                  session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

                  QueueBrowser queueBrowser = session.createBrowser(queue);


                  Enumeration<ObjectMessage> messages = queueBrowser.getEnumeration();

                  while (messages.hasMoreElements()) {

                      ObjectMessage objectMessage = (ObjectMessage) messages.nextElement();

                      Person p = (Person) objectMessage.getObject();


                      //do something....

                      //* after do something take out the message from queue



      * I tried code below, but i got error

      QueueReceiver receiver = session.createReceiver(queue, "JMSMessageID="+objectMessage.getJMSMessageID());

      MessageConsumer consumer = session.createConsumer(queue, "JMSMessageID="+objectMessage.getJMSMessageID());