This code使用线程并按预期工作 但我试图在没有线程的情况下做同样的事情。
首先我发送10条消息
for (int i = 0; i < 10; i++) {
String msg = "ABCD" + i;
TextMessage textMessage = Communication._getInstance().session.createTextMessage(msg);
textMessage.setJMSType(msg);
Communication._getInstance().getProducer().send(textMessage);
}
在此处接收10条消息。
public static ArrayList messages() {
EFLogger.LogInfo("MessageProcessor", "in messsages method");
ArrayList msg = new ArrayList();
try {
for (int i = 0; i < 10; i++) {
Message message = Communication._getInstance().getConsumer().receive(2000);
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
msg.add(textMessage.getJMSType());
} else {
EFLogger.LogInfo("MessageProcessor", "Received: " + message);
}
Communication._getInstance().session.close();
Communication._getInstance().connection.close();
}
} catch (JMSException e) {
e.printStackTrace();
}
return msg;
}
但我得到唯一的第一条消息,其他消息甚至不是instanceof TextMessage
并显示为空
注意:每次尝试关闭连接,收到消息后,结果保持不变。
日志
Message Received JMSType [ ABCD1 ] Text [ ABCD1 ] | root | main
2016-10-07 12:24:51,596 | INFO | Closing the connection | root | main
2016-10-07 12:24:53,640 | INFO | Received: null | root | main
2016-10-07 12:24:53,640 | INFO | Closing the connection | root | main
2016-10-07 12:24:55,659 | INFO | Received: null | root | main
2016-10-07 12:24:55,660 | INFO | Closing the connection | root | main
2016-10-07 12:24:57,676 | INFO | Received: null | root | main
2016-10-07 12:24:57,676 | INFO | Closing the connection | root | main
2016-10-07 12:24:59,689 | INFO | Received: null | root | main
2016-10-07 12:24:59,689 | INFO | Closing the connection | root | main