我用以下选择器创建了一个MQ接收器:
String selector = "JMSTimestamp BETWEEN 1446297200000 AND 1552479882777"
运行代码时,选择器正在拾取超出这些范围的消息,而没有拾取实际上应该被拾取的消息。 日志片段如下所示:
**lines****
--------- Receiving messages from MQ.
--------- Created consumer with Message selector JMSTimestamp BETWEEN 1446297200000 AND 1552479882777.
--------- Receiving messages.
--------- Message type is text.
--------- JMSTimestamp is 1552480011190.
--------- JMSTimestamp with format Mar 13,2019 13:26:51 .
--------- Processing Message.
***lines***
正如我们在日志中看到的那样,消息的JMSTimeStamp大于选择器上限1552479882777中提到的消息。
选择器有什么问题,应该怎么写?请帮助进行分析。
完整代码如下:
@Cleanup
QueueConnection connection = null;
@Cleanup
QueueSession session = null;
@Cleanup
MessageConsumer receiver = null;
@Cleanup
InitialContext ctx = null;
ctx = new InitialContext();
QueueConnectionFactory cf = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
connection = (QueueConnection) cf.createQueueConnection();
session = (QueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
log.info("Receiving Message from MQ");
Queue replyQueue = (Queue) session.createQueue(REPLY_QUEUE);
//getting time till 3 minutes ago
Long threeMinutesAgo = System.currentTimeMillis() - 180000;
// below expression is to select messages which are sitting in queue for more than 3 minutes,
String messageSelector = "JMSTimestamp BETWEEN 1446297200000 AND " + threeMinutesAgo;
receiver = (MessageConsumer) session.createConsumer(replyQueue, messageSelector);
log.info("created Consumer with message selector {}", messageSelector);
connection.start();
while (true) {
log.info("------------receiving response--------------");
Message asyncMessage = receiver.receiveNoWait();
if (asyncMessage instanceof TextMessage) {
log.info("Message type is text");
log.info("JMSTimeStamp is {}", String.valueOf(asyncMessage.getJMSTimestamp()));
log.info("JMSTimeStamp with format {}", sdf.format(new Date(asyncMessage.getJMSTimestamp())));
log.info("ThreeMinutesAgo with format {}", sdf.format(new Date(threeMinutesAgo)));
log.info("Difference is {}", String.valueOf(System.currentTimeMillis() - asyncMessage.getJMSTimestamp()));
log.info("Difference with format {}",
sdf.format(new Date(System.currentTimeMillis() - asyncMessage.getJMSTimestamp())));
String replyString = ((TextMessage) asyncMessage).getText();
asyncMessage.acknowledge();
processIntoText(replyString);
} if (asyncMessage == null) {
log.error("Queue is now empty for specified messages or null. ");
break;
}
}
} catch (JMSException | JAXBException | ParserConfigurationException | SAXException | NamingException e) {
log.error("Error in messaging {}", e);
}