我想找到MQ当前的队列深度。我的程序可以读取和写入队列。我能够编写一个消费者来消费队列中的消息。我可以使用计数器来计算队列中有多少消息。但我需要找到队列深度而不丢失消息。try { // Create a connection factory JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER); JmsConnectionFactory cf = ff.createConnectionFactory(); // Set the properties cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, HOST); cf.setIntProperty(WMQConstants.WMQ_PORT, PORT); cf.setStringProperty(WMQConstants.WMQ_CHANNEL, CHANNEL); cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT); cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, QMGR); cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "JmsPutGet (JMS)"); cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true); cf.setStringProperty(WMQConstants.USERID, APP_USER); cf.setStringProperty(WMQConstants.PASSWORD, APP_PASSWORD); // Create JMS objects context = cf.createContext(); destination = context.createQueue("queue:///" + QUEUE_NAME); long uniqueNumber = System.currentTimeMillis() % 1000; TextMessage message = context.createTextMessage("Your lucky number today is " + uniqueNumber); producer = context.createProducer(); producer.send(destination, message); System.out.println("Sent message:\n" + message); consumer = context.createConsumer(destination); // autoclosable String receivedMessage = consumer.receiveBody(String.class, 15000); // in ms or 15 seconds System.out.println("\nReceived message:\n" + receivedMessage); recordSuccess(); }代码应该打印队列深度。
1 回答
红颜莎娜
TA贡献1842条经验 获得超12个赞
队列深度可以通过浏览消息来统计。浏览不消耗消息。
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.DUPS_OK_ACKNOWLEDGE);
Queue q = session.createQueue("exampe_queue");
QueueBrowser browser = session.createBrowser(q);
Enumeration enumeration = browser.getEnumeration();
long msgCounter = 0;
while (enumeration.hasMoreElements()) {
Message msg = (Message) enumeration.nextElement();
msgCounter += 1;
}
System.out.println("Queue depth: " + msgCounter);
然而,当客户端从代理中提取消息时,这种方法非常慢。如果您需要偶尔检查队列深度以进行监控或类似目的,那么它会起作用。
更好的解决方案是使用 get-me-aq-depth 调用,但这不是 JMS api 指定的;然而,它通常存在于特定于实现的库中。
添加回答
举报
0/150
提交
取消