利用JMX监控ActiveMQ

一.开放ActiveMQ的监控功能(引用于apache):

1.修改conf/中的activemq.xml,在



  ...

  
     
  

  ...



2.修改jmx.access和jmx.password,在其中添加一个用户

jmx.access

# The "monitorRole" role has readonly access.
# The "controlRole" role has readwrite access.
monitorRole readonly
controlRole readwrite

jmx.password

# The "monitorRole" role has password "abc123".
# The "controlRole" role has password "abcd1234".
monitorRole abc123
controlRole abcd1234

3.修改bin/文件夹中的启动脚本activemq

1. Windows

  SUNJMX=-Dcom.sun.management.jmxremote.port=1616 -Dcom.sun.management.jmxremote.ssl=false \
    -Dcom.sun.management.jmxremote.password.file=%ACTIVEMQ_BASE%/conf/jmx.password \
    -Dcom.sun.management.jmxremote.access.file=%ACTIVEMQ_BASE%/conf/jmx.access

2. Unix

  SUNJMX="-Dcom.sun.management.jmxremote.port=1616 -Dcom.sun.management.jmxremote.ssl=false \
    -Dcom.sun.management.jmxremote.password.file=${ACTIVEMQ_BASE}/conf/jmx.password \
    -Dcom.sun.management.jmxremote.access.file=${ACTIVEMQ_BASE}/conf/jmx.access"

4.重启服务

二.编写JAVA代码

1.链接至activeMQ的JMX服务

RemoteJMXBrokerFacade createConnector = new RemoteJMXBrokerFacade();
//填写链接属性
System.setProperty("webconsole.jmx.url","service:jmx:rmi:///jndi/rmi://127.0.0.1:1099/jmxrmi");
System.setProperty("webconsole.jmx.user", "admin");
System.setProperty("webconsole.jmx.password", "activemq");
//创建配置
SystemPropertiesConfiguration configuration = new SystemPropertiesConfiguration();
//创建链接
createConnector.setConfiguration(configuration);

2.对Broker的监控

BrokerViewMBean brokerAdmin = createConnector.getBrokerAdmin();
System.out.println("==========Connection =================");
System.out.println("BrokerName =" + brokerAdmin.getBrokerName());
System.out.println("TotalMessageCount =" + brokerAdmin.getTotalMessageCount());
System.out.println("TotalConsumerCount =" + brokerAdmin.getTotalConsumerCount());
System.out.println("TotalDequeueCount =" + brokerAdmin.getTotalDequeueCount());
System.out.println("TotalEnqueueCount =" + brokerAdmin.getTotalEnqueueCount());

3.对Topic的监控 

System.out.println("=============Topic =================");
Collection topicViewList = createConnector.getTopics();
for (TopicViewMBean topicViewMBean : topicViewList) {
	System.out.println("beanName =" + topicViewMBean.getName());
	System.out.println("ConsumerCount ="+ topicViewMBean.getConsumerCount());
	System.out.println("DequeueCount ="+ topicViewMBean.getDequeueCount());
	System.out.println("EnqueueCount ="+ topicViewMBean.getEnqueueCount());
	System.out.println("DispatchCount ="+ topicViewMBean.getDispatchCount());
	System.out.println("ExpiredCount ="+ topicViewMBean.getExpiredCount());
	System.out.println("MaxEnqueueTime ="+ topicViewMBean.getMaxEnqueueTime());
	System.out.println("ProducerCount ="+ topicViewMBean.getProducerCount());
	System.out.println("MemoryPercentUsage ="+ topicViewMBean.getMemoryPercentUsage());
	System.out.println("MemoryLimit =" + topicViewMBean.getMemoryLimit());
}

4.对Queue的监控

System.out.println("============Queue===================");
Collection queueViewList = createConnector.getQueues();
for (QueueViewMBean queueViewMBean : queueViewList) {
	System.out.println(" queue beanName =" + queueViewMBean.getName());
	System.out.println("ConsumerCount ="+ queueViewMBean.getConsumerCount());
	System.out.println("DequeueCount ="	+ queueViewMBean.getDequeueCount());
	System.out.println("EnqueueCount ="	+ queueViewMBean.getEnqueueCount());
	System.out.println("DispatchCount ="	+ queueViewMBean.getDispatchCount());
	System.out.println("ExpiredCount =" + queueViewMBean.getExpiredCount());
	System.out.println("MaxEnqueueTime ="+ queueViewMBean.getMaxEnqueueTime());
	System.out.println("ProducerCount ="	+ queueViewMBean.getProducerCount());
	System.out.println("MemoryPercentUsage ="+ queueViewMBean.getMemoryPercentUsage());
	System.out.println("MemoryLimit =" + queueViewMBean.getMemoryLimit());
}


 

你可能感兴趣的:(Java技术,ActiveMQ)