activemq的jmx监控以及死消息的处理

方新德 2019-06-25

在我使用activemq过程中,遇到两个问题,一个就是有与消费者挂掉,导致磁盘存储满了,使得activemq不能提供服务,第二个是由于消费者异常,导致死消息太多

一、针对第一个问题,我们通过jmx进行监控,监控他的内存使用率,储存使用率,未消费的消息数量
1、在win上开启jmx

修改conf/activemq.xml
<managementContext>  
 <managementContext createConnector="true" connectorPort="1099" 
 connectorPath="/jmxrmi" jmxDomainName="org.apache.activemq"/>
 </managementContext>  
这样就开启了jmx
还有设置用户名密码

2、linux上开启jmx(自己没有试验过)
3、监控代码

public class StateMonitor {
 private static final String connectorPort = "1099";
 private static final String connectorPath = "/jmxrmi";  
 public static void main(String[] args) throws Exception {  
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + connectorPort + connectorPath); 
    JMXConnector connector = JMXConnectorFactory.connect(url);  
    connector.connect();  
    MBeanServerConnection connection = connector.getMBeanServerConnection();  
    ObjectName name = new ObjectName("org.apache.activemq:brokerName=localhost,type=Broker");  //这里面的localhost就是节点名称
    BrokerViewMBean mBean = MBeanServerInvocationHandler.newProxyInstance(connection, name, BrokerViewMBean.class, true);  

    for (ObjectName queueName : mBean.getQueues()) {  
        QueueViewMBean queueMBean = MBeanServerInvocationHandler.newProxyInstance(connection, queueName, QueueViewMBean.class, true);  

        System.out.println("\n------------------------------\n");  

        // 消息队列名称  
        System.out.println("Queue Name --- " + queueMBean.getName());  

        // 队列中剩余的消息数  
        System.out.println("Queue Size --- " + queueMBean.getQueueSize());  

        // 消费者数  
        System.out.println("Number of Consumers --- " + queueMBean.getConsumerCount());  

        // 出队数  
        System.out.println("Number of Dequeue ---" + queueMBean.getDequeueCount());  
    }  
} 
}

相关推荐

xinglun / 0评论 2020-06-14