java 获取 ActiveMQ 队列长度的任何简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7714866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:06:40  来源:igfitidea点击:

Any simple way to get the queue length of an ActiveMQ?

javajmsactivemq

提问by user705414

How to obtain the queue length (number of unconsumed messages sent to queue) in ActiveMQ, using Java?

如何使用 Java 在 ActiveMQ 中获取队列长度(发送到队列的未消耗消息数)?

采纳答案by Dag

You have to use JMX, since the Queue interface does not provide such information.

您必须使用 JMX,因为 Queue 接口不提供此类信息。

Example of retrieving the size of a specific queue:

检索特定队列大小的示例:

// connection
String url = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi";
JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(url));
MBeanServerConnection connection = connector.getMBeanServerConnection();
// get queue size
ObjectName nameConsumers = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=myqueue");
DestinationViewMBean mbView = MBeanServerInvocationHandler.newProxyInstance(connection, nameConsumers, DestinationViewMBean.class, true);
long queueSize = mbView.getQueueSize();

Reference: ActiveMQ JMX, Required MBeans

参考:ActiveMQ JMX必需的 MBean

Example: managing ActiveMQ with JMX APIs

示例:使用 JMX API 管理 ActiveMQ

回答by Ratha

Like this;

像这样;

QueueBrowser browser = session.createBrowser(queue);
Enumeration enu = browser.getEnumeration();
List list = new ArrayList();        
  while (enu.hasMoreElements()) {
    TextMessage message = (TextMessage) enu.nextElement();          
    list.add(message.getText());
   }
System.out.println("Size " + list.size());