JMX监测ActiveMQ中队列的状态
ref:
http://activemq.apache.org/jmx.html
1.enable JMX support useJmx="true"
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}" useJmx="true">
...
2.enable createConnector,set connectorPort and jmxDomainName
<managementContext>
<managementContext createConnector="true" connectorPort="11099" jmxDomainName="jmx-broker"/>
</managementContext>
3.java code
public class TestMqStatus {
public static void main(String[] args) {
//JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:"+RunServer.connectorPort+RunServer.connectorPath);
JMXServiceURL url;
try {
url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:11099/jmxrmi"); //jmxrmi is connectorPah
JMXConnector connector = JMXConnectorFactory.connect(url, null);
connector.connect();
MBeanServerConnection connection = connector.getMBeanServerConnection();
// 需要注意的是,这里的jmx-broker必须和上面配置的名称相同
//ObjectName name = new ObjectName(RunServer.jmxDomain+":BrokerName=localhost,Type=Broker");
//ObjectName name = new ObjectName("jmx-broker:BrokerName=localhost,Type=Broker");//Windows下不区分大小写
ObjectName name = new ObjectName("jmx-broker:BrokerName=localhost,Type=Broker");//Linux下区分大小写
BrokerViewMBean mBean = (BrokerViewMBean)MBeanServerInvocationHandler.newProxyInstance(connection, name, BrokerViewMBean.class, true);
// System.out.println(mBean.getBrokerName());
for(ObjectName queueName : mBean.getQueues()) {
QueueViewMBean queueMBean = (QueueViewMBean)MBeanServerInvocationHandler.newProxyInstance(connection, queueName, QueueViewMBean.class, true);
System.out.println("\n------------------------------\n");
System.out.println("States for queue --- " + queueMBean.getName());// 消息队列名称
System.out.println("Size --- " + queueMBean.getQueueSize());// 队列中剩余的消息数
System.out.println("Number of consumers --- " + queueMBean.getConsumerCount());// 消费者数
System.out.println("Number of enqueue ---" + queueMBean.getEnqueueCount() );// 入队数
System.out.println("Number of dequeue ---" + queueMBean.getDequeueCount() );// 出队数
}
} catch (Exception e) {
e.printStackTrace();
}
}
}