ActiveMQ_JMX监测ActiveMQ中队列的状态

JMX监测ActiveMQ中队列的状态

 

ref: 

http://activemq.apache.org/jmx.html

http://my.oschina.net/jinghaichao/blog/57318

http://blog.csdn.net/hjxgood/article/details/18706759

 

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

}

       

  }

}

好的,可以尝试使用pymqi和stomp.py这两个库来实现。首先安装这两个库:pip install pymqi stomp.py 然后使用pymqi连接ActiveMQ,在连接成功后,使用stomp.py订阅消息,可以按照以下代码示例来实现: ```python import stomp import pymqi host = 'localhost' port = '61613' channel = 'SYSTEM.ADMIN.SVRCONN' queue_manager = 'QM' queue_name = 'QUEUE.NAME' class MQListener(stomp.ConnectionListener): def on_message(self, headers, message): print('Received message: {}'.format(message)) def on_error(self, headers, message): print('Error received: {}'.format(message)) conn = stomp.Connection(host_and_ports=[(host, port)]) conn.set_listener('', MQListener()) queue_manager = pymqi.connect(queue_manager, channel, '{}({})'.format(host, port)) queue = pymqi.Queue(queue_manager, queue_name) queue.get() pymqi.disconnect(queue_manager) conn.subscribe(destination='/queue/{}'.format(queue_name), id=1, ack='auto') conn.start() conn.connect() while True: pass ``` 这段代码,我们首先定义了一个MQListener类,用于接收到消息后打印消息内容。然后创建了一个Connection对象,使用set_listener()方法将MQListener对象加入进去。使用pymqi连接ActiveMQ,通过Queue.get()方法获取一条消息后,执行pymqi.disconnect()方法退出连接。接着使用stomp.py的subscribe()方法订阅消息,使用start()方法启动连接,然后进入死循环等待消息的到来,当有消息到来时就会进入on_message()方法,处理消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值