【问题标题】:jms receive method doesnot stop when there is no message in Queue当队列中没有消息时,jms接收方法不会停止
【发布时间】:2023-03-26 13:57:01
【问题描述】:

我已尝试从link 中提到的队列中接收消息。 队列可能包含多条消息。我想一口气读完。

问题:

我使用了下面给出的方法。但是当 3 分钟(180000 毫秒)后队列中没有消息时,我看不到控件被传递给 while 循环内的 else 部分。

因此,我无法到达 finally 块 tom 停止连接。

正如link 中给出的,当没有消息时,我应该会收到消息流结束控制消息。但我不明白。

由于 while 循环中的 else 部分没有被执行,并且 finally 块永远不会到达关闭连接

可能是什么问题?

方法:

import javax.naming.InitialContext;

import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.QueueSession;
import javax.jms.QueueReceiver;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;

public class Receiver
{
      @Resource(lookup = "jms/ConnectionFactory")
      private static QueueConnectionFactory connectionFactory;

      @Resource(lookup = "jms/Queue")
      private static Queue queue;

      public void readQueueMessages() {                                                                   
      try {
         // create a queue connection
         QueueConnection queueConn = connFactory.createQueueConnection();

         // create a queue session
         QueueSession queueSession = queueConn.createQueueSession(false,
         Session.AUTO_ACKNOWLEDGE);

        // create a queue receiver
         QueueReceiver queueReceiver = queueSession.createReceiver(queue);

         // start the connection
         queueConn.start();

         // receive a message
          while(true) {
          TextMessage message = (TextMessage) queueReceiver.receive(180000);
              if (message != null) { 
                      if (message instanceof TextMessage) {
                        / print the message
                          System.out.println("received: " + message.getText());
                      } else {
                             break; // when the end-of-message stream control is message is received, that cannnot be of Textmessage type. So the loop should terminate.
                      }
                 }
           }
        } catch(JMSException exp) {
           // Handle this exception
       } finally {      
            if(queueConn != null) {                                                     
                 // close the queue connection
                queueConn.close();
            }
       }

【问题讨论】:

  • 我们应该在 while 循环内还是在 while 循环外调用 session.comiit()?因为我收到错误 WebSphere MQ call failed with compcode '2' ('MQCC_FAILED') reason '2024' (' MQRC_SYNCPOINT_LIMIT_REACHED') 虽然我阅读了消息。

标签: jakarta-ee jms websphere ibm-mq


【解决方案1】:

那个链接没有说“当没有消息时,我应该收到消息结束流控制消息。”,第7步说发送客户端需要
“发送一个空控制消息以指示消息流的结束: producer.send(session.createMessage());
发送一条没有指定类型的空消息是向消费者指示最终消息已到达的一种便捷方式。”
因此,除非您的消息生产者使用无类型的空消息(如示例)表示对话的最终消息,否则您的接收客户端无法按预期工作。

【讨论】:

  • 谢谢。接收客户端不会停止,直到我收到一条没有指定类型的空消息,对吧?就我而言,生产者永远不会发送空控制消息。那么在connection.session期间队列中没有可用消息时,还有其他方法可以停止接收方法吗?
  • @AlagammalP 规范说,在接收(超时)方法的情况下,它返回 - 为该消息消费者生成的下一条消息,如果超时到期或该消息消费者同时关闭,则返回 null - 所以只需中断如果消息为空,则退出循环。
  • 我们应该在while循环内还是在while循环外调用session.comiit()?
猜你喜欢
  • 2012-01-19
  • 2013-08-01
  • 2011-06-14
  • 2015-06-26
  • 2018-11-28
  • 2015-08-06
  • 2012-05-06
  • 2010-09-19
  • 2018-03-14
相关资源
最近更新 更多