如何从JMS队列中删除消息

问题描述 投票:12回答:5

是否有任何API可以从JMS队列中删除消息,而无需使用监视管理工具。

java jms
5个回答
10
投票

没有用于删除消息的直接API。您可以调用queueReceiver.receive()方法从队列中删除消息。 QueueBrowser不会从队列中删除消息。


4
投票

这是我可以工作的地方:

我假设您要删除符合特定条件的消息。

Queue destination = session.createQueue("your_q");

QueueBrowser browser = session.createBrowser(destination);

Enumeration<?> enum1 = browser.getEnumeration();

while(enum1.hasMoreElements())
{
   TextMessage msg = (TextMessage)enum1.nextElement();
   if(msg.getStringProperty("any_prop").equals("some_prop"))
   {
       MessageConsumer consumer = session.createConsumer(destination, "id='" +   msg.getStringProperty("id") + "'");
      consumer.receive(1000);
   }
}

4
投票

我还必须致电session.commit()以便使用者删除消息。

此外,receivenowait API不起作用;请致电receive(1000)

这是我编写的在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();
}

1
投票

您可能需要QueueBrowser对象。我认为它具有删除方法(或类似方法)


1
投票

这是我已经在weblogic上实际成功测试的东西:

destinationJNDI应该包含队列的JNDI名称,例如“ [email protected]_Notifications.NLNotificationReprocessQ”。

ctx应该是有效的InitialContext,例如:


  Properties env = new Properties();
  env.put(javax.naming.Context.PROVIDER_URL, PROVIDER_URL);
  env.put(Context.SECURITY_PRINCIPAL, WL_USER);
  env.put(Context.SECURITY_CREDENTIALS, WL_PASSWORD);
  env.put(Context.INITIAL_CONTEXT_FACTORY, WL_INITIAL_CONTEXT_FACTORY);
  InitialContext ctx = new InitialContext(env);

idlist应该是要删除的消息ID的CSV列表

这是代码:



  javax.jms.Queue queue = (javax.jms.Queue) ctx.lookup(destinationJNDI.toString());

  // lookup the queue connection factory
  QueueConnectionFactory queueConnFactory = (QueueConnectionFactory) ctx.lookup(WEBLOGIC_JMS_XA_CONNECTION_FACTORY);
  // create a queue connection
  QueueConnection queueConn = queueConnFactory.createQueueConnection();
  queueConn.start();

  // create a queue session
  Session queueSession = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
  for (String id : idlist.split(",")) {
      if (id.startsWith("ID:") ) {
        MessageConsumer consumer = queueSession.createConsumer(queue, " JMSMessageID='" +   id + "'");
        Message message = consumer.receive(1000);
        out.write("message = " + message + " ");
        out.write("deleted ID " + id + " ");
      }
  }
  queueSession.close();
  queueConn.close();


© www.soinside.com 2019 - 2024. All rights reserved.