JMX监控ActiveMQ

一、通过JMX控制台监控

在activemq的配置文件中增加JMX配置

配置文件位置:${ActiveMQ.Base}/conf/activemq.xml

broker节点添加属性useJmx

<broker useJmx="true" brokerName="BROKER1">
...
</broker>

运行jconsole.exe,位置为${JAVA_HOME}/bin/jconsole.exe

url填写:

service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi

 二、JMXAPI开发

配置文件位置:${ActiveMQ.Base}/conf/activemq.xml

broker节点添加属性useJmx,添加managementContext节点,配置连接端口

 <broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}" useJmx="true">

      <managementContext>

         <managementContext connectorPort="2011" jmxDomainName="my-broker"/>

      </managementContext>

</broker>

 

通过URLservice:jmx:rmi:///jndi/rmi://localhost:2011/jmxrmi初始化JMXConnector

通过NAMEmy-broker:BrokerName=localhost,Type=Broker获取BrokerViewMBean,可以获取Broker的相关属性,可以通过BrokerViewMBean获取QueueViewMBeanTopicViewMBean等,从而得到QueueTopic的属性。

具体实现参考JMX API

 

public static void main(String[] args) throws Exception {
		JMXServiceURL url = new JMXServiceURL(
				"service:jmx:rmi:///jndi/rmi://localhost:2011/jmxrmi");
		JMXConnector connector = JMXConnectorFactory.connect(url, null);
		connector.connect();
		MBeanServerConnection connection = connector.getMBeanServerConnection();
		ObjectName name = new ObjectName(
				"my-broker:BrokerName=localhost,Type=Broker");
		BrokerViewMBean mbean = (BrokerViewMBean) MBeanServerInvocationHandler
				.newProxyInstance(connection, name, BrokerViewMBean.class, true);
		log("**************");
		log("getBrokerId:" + mbean.getBrokerId());
		log("getBrokerName:" + mbean.getBrokerName());
		log("getTotalMessageCount:" + mbean.getTotalMessageCount());
		log("getTotalEnqueueCount:" + mbean.getTotalEnqueueCount());
		log("getTotalDequeueCount:" + mbean.getTotalDequeueCount());
		log("**************");
		log("==============");
		ObjectName[] queueNames = mbean.getQueues();
		log("getQueues count:" + queueNames.length);
		for (ObjectName queueName : queueNames) {
			
			QueueViewMBean queueMbean = (QueueViewMBean) MBeanServerInvocationHandler
					.newProxyInstance(connection, queueName,
							QueueViewMBean.class, true);
			log("------------");
			log("queueName:" + queueMbean.getName());
			log("queuesize:" + queueMbean.getQueueSize());
			log("getEnqueueCount:" + queueMbean.getEnqueueCount());
			log("getDequeueCount:" + queueMbean.getDequeueCount());
		}
		log("==============");
		log("++++++++++++++");
		ObjectName[] topicNames = mbean.getTopics();
		log("topic count:" + topicNames.length);
		for (ObjectName topicName : topicNames) {
			TopicViewMBean topicMBean = (TopicViewMBean) MBeanServerInvocationHandler
					.newProxyInstance(connection, topicName,
							TopicViewMBean.class, true);
			log("------------");
			log("topivName:"+topicMBean.getName());
			log("topivSize:"+topicMBean.getQueueSize());
			log("getEnqueueCount:"+topicMBean.getEnqueueCount());
			log("getDequeueCount:"+topicMBean.getDequeueCount());
		}
		log("++++++++++++++");

	}

 

 

 

你可能感兴趣的:(activemq,jmx)