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

Jakarta ee 当队列中没有消息时,jms接收方法不会停止,jakarta-ee,jms,websphere,ibm-mq,Jakarta Ee,Jms,Websphere,Ibm Mq,我已尝试从队列中接收消息,如中所述。 队列可能包含多个LPLE消息。我想一次读完所有的书 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.QueueCon

我已尝试从队列中接收消息,如中所述。 队列可能包含多个LPLE消息。我想一次读完所有的书

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();
            }
       }
问题:

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();
            }
       }
我使用了下面给出的方法。但当队列中在3分钟(180000毫秒)后没有消息时,我无法看到控件被传递到while循环中的else部分

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();
            }
       }
由于这个原因,我无法到达最后一个街区,汤姆停止了连接

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();
            }
       }
如中所示,当没有消息时,我应该接收消息流结束控制消息。但是我不明白

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循环中的else部分不会被过度使用,并且最终无法到达block来关闭连接

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();
            }
       }
问题是什么

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();
            }
       }
方法:

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();
            }
       }
该链接没有说“如链接中给出的,当没有消息时,我应该接收消息流结束控制消息”。步骤7说,发送客户端需要发送一条空控制消息,以指示消息流结束:
producer.send(session.createMessage());


发送未指定类型的空消息是向消费者表明最终消息已到达的一种方便方式。”
因此,除非您的消息生产者用未指定类型的空消息(如示例所示)表示对话的最终消息,否则您的接收客户端将无法按预期工作。

谢谢。在我收到未指定类型的空消息之前,接收客户端不会停止,对吗?。在我的例子中,生产者永远不会发送空的控制消息。那么,当连接期间队列中没有可用消息时,是否有其他方法停止receive方法。session?@AlagammalP规范说,如果是receive(timeout)方法,它将返回为该消息使用者生成的下一条消息,如果超时过期或此消息使用者同时关闭,则为null-因此,如果消息为null,则中断循环。我们应该在while循环内部调用session.comit(),还是在while循环外部调用session.comit()内部while循环还是外部while循环?因为我在读取消息时收到错误WebSphere MQ调用失败,代码为“2”(“MQCC_失败”)原因为“2024”(“MQRC_SYNCPOINT_LIMIT_reated”)。
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();
            }
       }