找到MQ的队列深度
find the queue depth of MQ
我想知道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();
}
代码应打印队列深度。
队列深度可以通过浏览消息来统计。浏览不消费消息。
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-a-q-depth 调用,但这不是 JMS 指定的 api;但是它通常存在于特定于实现的库中。
我想知道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();
}
代码应打印队列深度。
队列深度可以通过浏览消息来统计。浏览不消费消息。
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-a-q-depth 调用,但这不是 JMS 指定的 api;但是它通常存在于特定于实现的库中。