如何正确接收来自队列 IBM MQ 的消息
How to correctly receive a message from the queue IBM MQ
这段代码应该从队列中读取,如果队列为空,则通过定时器重复读取检查Text或Byte的格式。用这种格式检查空队列是否正确?在这种情况下计时器会工作吗?
由于未配置MQ队列,目前无法查看。
public class Main {
public static void main(String[] args) {
boolean tru = true;
try {
/*MQ Конфигурация подключения*/
MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
mqQueueConnectionFactory.setHostName("localhost");
mqQueueConnectionFactory.setChannel("SVRCONN");
mqQueueConnectionFactory.setPort(1414);
mqQueueConnectionFactory.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
mqQueueConnectionFactory.setQueueManager("MQ_APPLE");
mqQueueConnectionFactory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
QueueConnection queueConnection = mqQueueConnectionFactory.createQueueConnection("name", "pass");
MQQueueSession session = (MQQueueSession) queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
MQQueue queue = (MQQueue) session.createQueue("queue:///Q1");
MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(queue);
queueConnection.start();
while(tru){
TextMessage receivedMessage = (TextMessage) receiver.receive();
if(receivedMessage != null){
MStart(receivedMessage);
tru = false;
} else {
Timer timer = new Timer(10000,new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("Timer Run");
}
});
timer.start();
}
}
receiver.close();
session.close();
queueConnection.close();
} catch (JMSException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void MStart(TextMessage receivedMessage) throws JMSException {
if (receivedMessage instanceof BytesMessage) {
TextMessage textMessage = (TextMessage) receivedMessage;
System.out.println("Received message '"
+ textMessage.getText() + "'");
} else if (receivedMessage instanceof TextMessage) {
System.out.println("Received message: " + receivedMessage.getText());
}
}
}
请注意,您的 receiver.receive();
调用将无限期阻塞。来自 MQMessageConsumer
文档:
Receives the next message produced for this message consumer. This
call blocks indefinitely until a message is produced or until this
message consumer is closed.
如果您想在指定的时间内从队列中读取数据,也许您最好使用 receive(long timeout)
方法:
public javax.jms.Message receive(long timeout)
throws javax.jms.JMSException
Receives the next message that arrives within the specified timeout
interval. This call blocks until a message arrives, the timeout
expires, or this message consumer is closed. A timeout of zero never
expires, and the call blocks indefinitely.
除非需要使用计时器,否则我更喜欢使用receive(timeout)
。如果队列中有消息,则接收调用将 return 接收消息,否则它将超时并抛出 2033 原因代码异常。您可以捕获此异常并再次调用接收以等待消息。这也将允许您在被要求时停止此线程。
而不是 (TextMessage) receiver.receive()
只需执行 Message msg = receiver.receive()
然后使用 if(msg instanceof TextMessage)
确定收到的消息类型。
这段代码应该从队列中读取,如果队列为空,则通过定时器重复读取检查Text或Byte的格式。用这种格式检查空队列是否正确?在这种情况下计时器会工作吗? 由于未配置MQ队列,目前无法查看。
public class Main {
public static void main(String[] args) {
boolean tru = true;
try {
/*MQ Конфигурация подключения*/
MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
mqQueueConnectionFactory.setHostName("localhost");
mqQueueConnectionFactory.setChannel("SVRCONN");
mqQueueConnectionFactory.setPort(1414);
mqQueueConnectionFactory.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
mqQueueConnectionFactory.setQueueManager("MQ_APPLE");
mqQueueConnectionFactory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
QueueConnection queueConnection = mqQueueConnectionFactory.createQueueConnection("name", "pass");
MQQueueSession session = (MQQueueSession) queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
MQQueue queue = (MQQueue) session.createQueue("queue:///Q1");
MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(queue);
queueConnection.start();
while(tru){
TextMessage receivedMessage = (TextMessage) receiver.receive();
if(receivedMessage != null){
MStart(receivedMessage);
tru = false;
} else {
Timer timer = new Timer(10000,new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("Timer Run");
}
});
timer.start();
}
}
receiver.close();
session.close();
queueConnection.close();
} catch (JMSException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void MStart(TextMessage receivedMessage) throws JMSException {
if (receivedMessage instanceof BytesMessage) {
TextMessage textMessage = (TextMessage) receivedMessage;
System.out.println("Received message '"
+ textMessage.getText() + "'");
} else if (receivedMessage instanceof TextMessage) {
System.out.println("Received message: " + receivedMessage.getText());
}
}
}
请注意,您的 receiver.receive();
调用将无限期阻塞。来自 MQMessageConsumer
文档:
Receives the next message produced for this message consumer. This call blocks indefinitely until a message is produced or until this message consumer is closed.
如果您想在指定的时间内从队列中读取数据,也许您最好使用 receive(long timeout)
方法:
public javax.jms.Message receive(long timeout) throws javax.jms.JMSException
Receives the next message that arrives within the specified timeout interval. This call blocks until a message arrives, the timeout expires, or this message consumer is closed. A timeout of zero never expires, and the call blocks indefinitely.
除非需要使用计时器,否则我更喜欢使用receive(timeout)
。如果队列中有消息,则接收调用将 return 接收消息,否则它将超时并抛出 2033 原因代码异常。您可以捕获此异常并再次调用接收以等待消息。这也将允许您在被要求时停止此线程。
而不是 (TextMessage) receiver.receive()
只需执行 Message msg = receiver.receive()
然后使用 if(msg instanceof TextMessage)
确定收到的消息类型。