网元监控之-activemq

activeMQ监控

1. JMX连接器-rmi

1.1 定义

  • JMX API定义了一个标准连接器 - RMI Connector,它支持通过RMI远程访问一个MBeanServer

1.2 RMI连接器服务器

  • 地址示例如下:

      service:jmx:rmi://localhost:1099/jndi/rmi://localhost:8899/myname
  • 在这个JMXServiceURL中:
    1. 第一个rmi指的是rmi连接器,表示用连接器使用RMI传输协议,第二个rmi指定RMI注册RMI连服务接器存储存根
    2. localhost:1099: 这个是connector server的IP和端口,该部分是一个可选项,可以被省略掉。如果省略的话,则connector server会随机任意选择一个可用的端口
    3. /jndi/rmi://localhost:8899/myname: 这个是connector server的路径,表示Connector server的stud是使用JNDI API绑定在rmi://localhost:8899/myname这个地址上

1.3 创建一个RMI connector server

  • 通常是提供一个RMI connector server的连接器地址,用JMXConnectorServerFactory.newJMXConnectorServer方法来创建RMI connector cerver
  • 参考 : JMX API

1.4 example

    package com.uxsino.ActiveMQTest;
    
    import java.io.IOException;
    import java.util.Arrays;
    import javax.management.InstanceNotFoundException;
    import javax.management.IntrospectionException;
    import javax.management.MBeanServerConnection;
    import javax.management.MBeanServerInvocationHandler;
    import javax.management.MalformedObjectNameException;
    import javax.management.ObjectName;
    import javax.management.ReflectionException;
    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;
    import org.apache.activemq.broker.jmx.TopicViewMBean;
    
    public class ActiveMQTest {
        public static void main(String[] args) throws IOException, MalformedObjectNameException, InstanceNotFoundException,
            IntrospectionException, ReflectionException {
            String url = "service:jmx:rmi:///jndi/rmi://192.168.5.90:11099/jmxrmi";
            JMXServiceURL urls = new JMXServiceURL(url);
            JMXConnector connector;
            MBeanServerConnection conn;
            BrokerViewMBean mBean;
            try {
                connector = JMXConnectorFactory.connect(urls, null);
                System.out.println("=========" + connector.getConnectionId() + "========");
                // connector.connect();
                conn = connector.getMBeanServerConnection();
            } catch (Exception e) {
                System.out.println("连接失败");
                return;
            }
            ObjectName name = new ObjectName("org.apache.activemq:brokerName=localhost,type=Broker");
            mBean = (BrokerViewMBean) MBeanServerInvocationHandler.newProxyInstance(conn, name, BrokerViewMBean.class,
                true);
            // System.out.println("++++=====" + mBean.getTotalConsumerCount() + "======++");
    
            System.out.println("++++++++++++++" + mBean.getTotalEnqueueCount() + "+++++++++++");
            System.out.println("++++++++++++++" + mBean.getTotalDequeueCount() + "+++++++++++");
            System.out.println("++++++++++++++" + mBean.getTotalConsumerCount() + "+++++++++++");
            System.out.println("++++++++++++++" + mBean.getTotalMessageCount() + "+++++++++++");
            System.out.println("++++++++++++++" + mBean.getTotalConsumerCount() + "+++++++++++");
    
            /* System.out.println("Domains:---------------");
            String domains[] = conn.getDomains();
            for (int i = 0; i < domains.length; i++) {
                System.out.println("\tDomain[" + i + "] = " + domains[i]);
            }*/
    
            /*System.out.println("all ObjectName:---------------");
            Set<ObjectInstance> set = conn.queryMBeans(null, null);
            for (Iterator<ObjectInstance> it = set.iterator(); it.hasNext();) {
                ObjectInstance oi = (ObjectInstance) it.next();
                System.out.println("\t" + oi.getObjectName());
                System.out.println("\t" + oi.getClassName());
            }*/
    
            /*System.out.println("org.apache.activemq:brokerName=localhost,type=Broker:---------------");
            ObjectName mbeanName = new ObjectName("org.apache.activemq:brokerName=localhost,type=Broker");
            MBeanInfo info = conn.getMBeanInfo(mbeanName);
            System.out.println("Class: " + info.getClassName());
            if (info.getAttributes().length > 0) {
                for (MBeanAttributeInfo m : info.getAttributes())
                    System.out.println("\t ==> Attriber:" + m.getName());
            }
            if (info.getOperations().length > 0) {
                for (MBeanOperationInfo m : info.getOperations())
                    System.out.println("\t ==> Operation:" + m.getName());
            }*/
    
            // 这里brokerName的b要小些,大写会报错
            // ObjectName name = new ObjectName("org.apache.activemq:brokerName=localhost,type=Broker");
    
            ObjectName[] os = mBean.getQueues();
            System.out.println(mBean.getQueueSubscribers());
            for (ObjectName na : os) {// 获取点对点的队列 mBean.getTopics() 获取订阅模式的队列
                QueueViewMBean queueBean = (QueueViewMBean) MBeanServerInvocationHandler.newProxyInstance(conn, na,
                    QueueViewMBean.class, true);
                StringBuffer sb = new StringBuffer("");
                sb.append("name" + "" + "queueSize" + "" + "name" + "");
    
                System.out.println("******************************");
                System.out.println("队列的名称:" + queueBean.getName());
                System.out.println("队列中剩余的消息数:" + queueBean.getQueueSize());
                System.out.println("消费者数:" + queueBean.getConsumerCount());
                System.out.println("出队列的数量:" + queueBean.getDequeueCount());
                System.out.println("内存使用百分比:" + queueBean.getMemoryPercentUsage());
                System.out.println("内存限制:" + queueBean.getMemoryLimit());
                System.out.println("内存限制:" + queueBean.getQueueSize());
            }
    
            System.out.println("===============================================");
            ObjectName[] oo = mBean.getTopics();
            System.out.println(Arrays.toString(oo));
            for (ObjectName na : oo) {
                TopicViewMBean tb = (TopicViewMBean) MBeanServerInvocationHandler.newProxyInstance(conn, na,
                    TopicViewMBean.class, true);
                System.out.println("**********-----------***********");
                System.out.println("队列的名称:" + tb.getName());
                System.out.println("队列中剩余的消息数:" + tb.getQueueSize());
                System.out.println("消费者数:" + tb.getConsumerCount());
                System.out.println("出队列的数量:" + tb.getDequeueCount());
                System.out.println("内存使用百分比:" + tb.getMemoryPercentUsage());
            }
            connector.close();
        }
    }

转载于:https://www.cnblogs.com/yikesaiting/p/8405798.html

  • 0
    点赞
  • 0
    收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值