我尝试从MQ接收消息。 我可以在MQ Explorer中看到消息。 但我无法得到消息。它总是返回null。 但我可以将消息丢弃到队列。
代码在这里。
MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
cf.setHostName("localhost");
cf.setPort(1415);
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
cf.setQueueManager("TEST");
cf.setChannel("TEST.CHANNEL");
MQQueueConnection connection = (MQQueueConnection)cf.createQueueConnection("mqusr","q@789945");
MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
MQQueue replyQueue = (MQQueue) session.createQueue("queue:///TEST.REPLY.QUEUE");
MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(replyQueue);
TextMessage receivedMessage = (TextMessage) receiver.receive(10000);
String reponseMessage = receivedMessage != null ? receivedMessage.toString() : "";
答案 0 :(得分:1)
要接收消息,您需要在执行receive.receive(10000)之前发出connection.start() -