当队列中没有消息时,jms 接收方法不会停止

jms receive method doesnot stop when there is no message in Queue

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

问题:

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

因此我无法到达最终块 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();
            }
       }

那个link没有说"As given in the link when there is no message, I should receive end-of-message-stream control message.",第7步说发送客户端需要
》发送一个空的控制消息来表示消息流结束: producer.send(session.createMessage());
发送未指定类型的空消息是向消费者指示最终消息已到达的便捷方式。"
因此,除非您的消息生成者像示例那样使用未类型化的空消息表示对话的最终消息,您的接收客户端不会按预期工作。