Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a message from the JMS queue

Tags:

java

jms

Is there any API to delete a message from the JMS queue without using the monitoring admin tool.

like image 977
avikodak Avatar asked Mar 02 '12 06:03

avikodak


People also ask

How do I delete messages from Weblogic JMS queue using Wlst?

Enter the username, password, and the admin server url you want to connect to. Change the location to serverRuntime by entering the command serverRuntime(). It shows all the attributes and functions of Queue1.

How do I check messages in JMS queue?

In the Administration Console, expand Services > Messaging > JMS Modules. In the JMS Modules table, click the JMS module that contains the configured queue that you want to access. In the selected JMS module's Summary of Resources table, click the queue that you want to monitor.

How do I stop JMS message listener?

First, on your main thread you need to lock the message listener after starting your JMS connection and invoke the message listener “wait” method. On your message listener, you need to lock again the message listener and then invoke the “notify all” method. Save this answer. Show activity on this post.


1 Answers

I had to additionally call session.commit() for the consumer to delete messages.

Additionally, the receivenowait API doesn't work; call receive(1000) instead.

Here is a working piece of code I wrote that worked on jboss:

try {
  connection = connectionFactory.createConnection();
  session = connection.createSession(true,-1);
  Queue queue = (Queue) QueueConnectionFactory.getInitialContext().lookup("/queue/DLQ");
  QueueBrowser browser = session.createBrowser(queue);
  Enumeration<?> enum1 = browser.getEnumeration();

  while(enum1.hasMoreElements()) {
    TextMessage msg = (TextMessage)enum1.nextElement();
    MessageConsumer consumer = session.createConsumer(queue, "JMSMessageID='" +  msg.getJMSMessageID()  + "'");
    //You can try starting the connection outside while loop as well, I think I started it inside while loop by mistake, but since this code worked I am hence letting you know what worked  
    connection.start();
    Message message = consumer.receive(1000) ;
    if ( message != null ) {
        //do something with message
    }
  }
} 
finally {
  session.commit();
  consumer.close();
  browser.close();
  session.close();
  connection.close();
}
like image 76
rohit sharma Avatar answered Oct 01 '22 09:10

rohit sharma