监听activeMQ的状态

用JMX监控activeMQ的状态。什么未处理啊,已处理啊,balabala...
我的诉求比较简单,监控activeMQ的状态,没有什么密码的需要。所以,只需要修改xml的两个地方就行了。
activeMQ版本:apache-activemq-5.11.1
修改conf文件夹下的activemq.xml文件
1、找到broker标签。
原来是这样的:

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

添加一个属性(顺便修改了一个属性值)后是这样的:

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

2、修改managementContext标签
原来是这样的:

<managementContext>
	<managementContext createConnector="false"/>
</managementContext>


把createConnector修改为true后,是这样的:

<managementContext>
	<managementContext createConnector="true"/>
</managementContext>
然后,启动activeMQ就可以在java里面读取到activeMQ的状态了。
检测是否ok的办法是,运行java里面的jconsole软件,输入“service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi”,能连上,就ok了。


那么,java里面的代码可以这样写:

import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.activemq.broker.jmx.BrokerViewMBean;
import org.apache.activemq.broker.jmx.QueueViewMBean;

public class StateTest {
	private BrokerViewMBean mBean = null;
	private MBeanServerConnection connection = null;

	public StateTest() throws Exception {
		JMXServiceURL url = new JMXServiceURL(
				"service:jmx:rmi:///jndi/rmi://10.0.0.232:1099/jmxrmi");
		JMXConnector connector = JMXConnectorFactory.connect(url, null);
		connector.connect();
		connection = connector.getMBeanServerConnection();

		//必须和刚才配置的名称相同
		ObjectName name = new ObjectName(
				"org.apache.activemq:brokerName=BROKER1,type=Broker");
		mBean = (BrokerViewMBean) MBeanServerInvocationHandler
				.newProxyInstance(connection, name, BrokerViewMBean.class, true);
	}

	public void read() {
		ObjectName[] ObjectNames = mBean.getQueues();

		for (ObjectName queueName : ObjectNames) {
			QueueViewMBean queueMBean = (QueueViewMBean) MBeanServerInvocationHandler
					.newProxyInstance(connection, queueName,
							QueueViewMBean.class, true);
			// 消息队列名称
			System.out.println("queueName = " + queueMBean.getName());
			// 队列中剩余的消息数
			System.out.println("待消费消息数  = " + queueMBean.getQueueSize());
			// 消费者数
			System.out.println("当前消费者(监听者)= " + queueMBean.getConsumerCount());
			// 出队数
			System.out.println("消息出队数 = " + queueMBean.getDequeueCount());
			// 消息总数
			System.out.println("消息总数 =" + queueMBean.getEnqueueCount());
		}
	}

	public static void main(String[] args) throws Exception {
		StateTest t = new StateTest();
		t.read();

	}
}



参考网址:
1、官网。它的例子,相当简单。而事实是,就是这么简单。
http://activemq.apache.org/jmx.html
2、别人的博客
http://blog.csdn.net/kimmking/article/details/9170563
3、配置里面的大小写的问题,多亏这个提问,不然我不知道要多崩溃
http://www.oschina.net/question/778726_132870?fromerr=gzp8t1p4


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值