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

时间:2011-10-10 15:10:37

标签: java jms activemq

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

2 个答案:

答案 0 :(得分:12)

您必须使用JMX,因为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();

参考:ActiveMQ JMXRequired MBeans

示例:managing ActiveMQ with JMX APIs

答案 1 :(得分:4)

喜欢这个;

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());