ActiveMQ远程JMX监控队列消息条数,进行预警 Myth丶恋晨 2023-02-14 10:43 2阅读 0赞 ### 前言 ### > ActiveMQ最近发现消息积压比较严重,消息最多的时候导致ActiveMQ直接内存溢出了,然后想着监控一个ActiveMQ队列消息大小,当积压的数量达到一定阈值的时候预警。 #### 参考链接 #### [ActiveMQ JMX使用][ActiveMQ JMX] [JMX远程监控ActiveMQ设置][JMX_ActiveMQ] [ActiveMQ 远程监控JMX设置][ActiveMQ _JMX] [activemq读取剩余消息队列中消息的数量][activemq] [ActiveMQ监控][ActiveMQ] [activemq的jmx监控以及死消息的处理][activemq_jmx] [https://activemq.apache.org/jmx][https_activemq.apache.org_jmx] ### 配置JMX ### #### 修改activemq的activemq.xml 配置文件 #### 找到activemq的conf目录 /home/zookeeper/apache-activemq-5.15.12/conf 修改配置activemq.xml配置文件 找到`<broker xmlns="http://activemq.apache.org/schema/core"` <broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" useJmx="true" dataDirectory="${activemq.data}"> 添加`useJmx="true"` 开启JMX 找到managementContext节点,设置`createConnector` `connectorHost` `connectorPort` `connectorPath` `jmxDomainName`这几个属性就可以了,这里一定要设置`connectorHost`,要不然会遇到另外一个错误。 <managementContext> <managementContext createConnector="true" connectorHost="192.168.1.134" connectorPort="1099" connectorPath="/jmxrmi" jmxDomainName="org.apache.activemq"/> </managementContext> 设置好之后重启ActiveMQ就可以,然后使用下面的代码进行获取队列的消息,也可以用`purge()`方法来清空消息。 ### 代码 ### 这里的代码没有使用用户名和密码, public class MonitorActiveMQ { public static void main(String[] args) throws Exception { String url = "service:jmx:rmi:///jndi/rmi://192.168.1.134:1099/jmxrmi"; JMXServiceURL urls = new JMXServiceURL(url); JMXConnector connector = JMXConnectorFactory.connect(urls, null); connector.connect(); MBeanServerConnection conn = connector.getMBeanServerConnection(); ObjectName name = new ObjectName("org.apache.activemq:brokerName=localhost,type=Broker"); BrokerViewMBean mBean = (BrokerViewMBean) MBeanServerInvocationHandler.newProxyInstance(conn, name, BrokerViewMBean.class, true); for (ObjectName na : mBean.getQueues()) { QueueViewMBean queueBean = (QueueViewMBean) MBeanServerInvocationHandler.newProxyInstance(conn, na, QueueViewMBean.class, true); System.out.println("******************************"); System.out.println("队列的名称:" + queueBean.getName()); System.out.println("队列中剩余的消息数:" + queueBean.getQueueSize()); System.out.println("消费者数:" + queueBean.getConsumerCount()); System.out.println("出队列的数量:" + queueBean.getDequeueCount()); queueBean.purge(); } connector.close(); } } ### 有密码的 ### 但是上面的配置没有设置用户名和密码,所以这里配置了也不生效。 public static void main(String[] args) throws Exception { String url = "service:jmx:rmi:///jndi/rmi://192.168.1.134:1099/jmxrmi"; JMXServiceURL urls = new JMXServiceURL(url); Map<String, String[]> env = new HashMap<>(); String[] credentials = { "admin", "activemq"}; env.put(JMXConnector.CREDENTIALS, credentials); JMXConnector connector = JMXConnectorFactory.connect(urls, env); connector.connect(); MBeanServerConnection conn = connector.getMBeanServerConnection(); ObjectName name = new ObjectName("org.apache.activemq:brokerName=localhost,type=Broker"); BrokerViewMBean mBean = (BrokerViewMBean) MBeanServerInvocationHandler.newProxyInstance(conn, name, BrokerViewMBean.class, true); for (ObjectName na : mBean.getQueues()) { QueueViewMBean queueBean = (QueueViewMBean) MBeanServerInvocationHandler.newProxyInstance(conn, na, QueueViewMBean.class, true); System.out.println("******************************"); System.out.println("队列的名称:" + queueBean.getName()); System.out.println("队列中剩余的消息数:" + queueBean.getQueueSize()); System.out.println("消费者数:" + queueBean.getConsumerCount()); System.out.println("出队列的数量:" + queueBean.getDequeueCount()); queueBean.purge(); } connector.close(); } [ActiveMQ JMX]: https://www.cnblogs.com/eric-fang/p/11431104.html [JMX_ActiveMQ]: https://www.jianshu.com/p/68f9d54d6ff6 [ActiveMQ _JMX]: https://blog.csdn.net/hjxgood/article/details/18706759 [activemq]: https://blog.csdn.net/cainiaofeitian/article/details/52065755 [ActiveMQ]: http://blog.sina.com.cn/s/blog_6795e13e0102whos.html [activemq_jmx]: https://segmentfault.com/a/1190000011368962 [https_activemq.apache.org_jmx]: https://activemq.apache.org/jmx
还没有评论,来说两句吧...