How to remove the messages in the queue from the Activemq

865 views Asked by At

I need to remove the messages in QUEUE from ActiveMQ by using java program.I have search in the google but I didn't find any proper examples. I am able to get the all the messages in the QUEUE.Once fetching is done then I need to remove the all the messages in the Queue.

I am using following code to get the all the messages:

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
          ActiveMQConnection connection = (ActiveMQConnection)connectionFactory.createConnection();
         // DestinationSource ds = connection.getDestinationSource();
          System.out.println("DestinationSource created");
          QueueSession queueSession = connection.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);
          connection.start();
          System.out.println("session Created");
          Queue queue = queueSession.createQueue(QUEUENAME);
          System.out.println("Connected");
          QueueBrowser browser = queueSession.createBrowser(queue);
          Enumeration<?> messagesInQueue = browser.getEnumeration();           
          System.out.println("messagesInQueue ==>"+messagesInQueue);             
          //System.out.println(""+messagesInQueue.hasMoreElements());
          int size = 0;
          while (messagesInQueue.hasMoreElements()) 
          {
              Message queueMessage = (Message) messagesInQueue.nextElement();
              System.out.println(queueMessage.getJMSMessageID());              
              size = size+1;
              System.out.println(queueMessage);
              TextMessage text = (TextMessage) queueMessage;
              System.out.println(new Date()+"Message is : " + text.getText());                                                         
          }

          System.out.println("Size===>"+size);

Some one please share any sample java code to remove the message in the QUEUE...

0

There are 0 answers