activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fehm <Felix....@cern.ch>
Subject Re: How can I remove a message from Queue
Date Tue, 06 Oct 2009 09:06:08 GMT

Hi abakar,

This example below will read all message from a queue (until it is empty).
Make sure that no producer runs.
If you need to delete all messages you can also use jconsole
(http://activemq.apache.org/jmx.html) to empty the queue (operation
"purge").


Felix



import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.jms.MessageConsumer;
import javax.jms.Session;


import org.apache.activemq.ActiveMQConnectionFactory;

public class Test {

  /**
   * @param args
   */
  public static void main(String[] args) throws Exception {

    Message message = null ;
    // your broker url
    String sConnection = "tcp://0.0.0.0:61620";
    ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory(sConnection);
    Connection connection = connectionFactory.createConnection();
    connection.start();

    // create Session for the queue and register us in the server
    Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
    Destination queue = session.createQueue("YOUR_QUEUE_NAME");
    MessageConsumer consumer = session.createConsumer(queue);

    // polling for messages
    while ( (message = consumer.receive()) != null ){
      if(message instanceof TextMessage){
        TextMessage tx = (TextMessage)message;
        System.out.println(tx);
      }
    }
  }
}






abakar wrote:
> 
> Hello i am a new by ActiveMQ. I have tried form my java code to delete a
> message form queue but didn't work.
> 
> I don't know how can i do that!!!!!  
> 
> how can I remove alle messages from a queue or only one message from
> Queue.
> 
> Ich have lookout at google, Nabble and activeMQ Community but i didn't
> find one example as java code.
> 
> please help me, i have more than two weeks and i tried many ways but
> without sucess. :-(
> 
> Thank you so much 
> 
> 


-- 
View this message in context: http://www.nabble.com/How-can-I-remove-a-message-from-Queue-tp25749799p25765164.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Mime
View raw message