【发布时间】:2017-01-25 06:08:02
【问题描述】:
我是消息队列的新手。我正在尝试使用以下代码从 MQ 队列获取消息。
我正在创建一个连接并使用该连接从队列中获取每条消息。这样做是否正确以及我是否需要提交连接。
无限for循环是从队列中一直接收消息的正确方法,对吗? 请给我建议。
try {
createMQConnection(); // getting mq connection
createMQSession(); // getting mq session
createMQDestination(); // getting mq destination
for ( ; ; ) { // infinite loop to receive message from Queue
consumer = session.createConsumer(mqQueue);
jmsTextMessage = (JMSTextMessage) consumer.receive(100);
// Calling application method to process the requested message from queue
}
} catch (Exception e) {
throw e;
} finally {
// closing consumer
// closing session
// closing connection
}
【问题讨论】:
-
如果你使用没有参数的receive(),它应该等待无穷大。另外,我认为您不需要每次都创建消费者。查看来自 IBM 的示例
-
感谢并注明。因为我正在为每个循环一个一个地读取多个队列。