activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fehm <>
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
( to empty the queue (operation


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://";
    ActiveMQConnectionFactory connectionFactory = new
    Connection connection = connectionFactory.createConnection();

    // create Session for the queue and register us in the server
    Session session = connection.createSession(false,
    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;

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:
Sent from the ActiveMQ - User mailing list archive at

View raw message