【arthas】Arthas 查看 Mbean 的信息

x33g5p2x  于2022-04-06 转载在 其他  
字(1.5k)|赞(0)|评价(0)|浏览(242)

1.概述

转载:Arthas 查看 Mbean 的信息

2.开篇

arthas提供Mbean命令查看或监控 Mbean 的属性信息。

3.原理介绍

MBeanServer是一个包含所有注册MBean的仓库.它是JMX代理层的核心.JMX1.0规范提供一个接口叫 javax.management.MBeanServer. 所有管理的在MBean操作通过MBeanServer执行.使用MBeanServer实例,你能够管理所有MBean.每一个MBean具有一个唯一标志,叫ObjectName.

3.源码分析

private Set<ObjectName> queryObjectNames() {
        MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
        Set<ObjectName> objectNames = new HashSet<ObjectName>();
        try {
            if (StringUtils.isEmpty(name)) {
                name = "*:*";
            }
            objectNames = platformMBeanServer.queryNames(new ObjectName(name), null);
        } catch (MalformedObjectNameException e) {
            logger.warn("queryObjectNames error", e);
        }
        return objectNames;
    }

    private void listMetaData(CommandProcess process) {
        Set<ObjectName> objectNames = queryObjectNames();
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        try {
            MBeanModel mbeanModel = new MBeanModel();
            Map<String, MBeanInfo> mbeanMetaData = new LinkedHashMap<String, MBeanInfo>();
            mbeanModel.setMbeanMetadata(mbeanMetaData);
            for (ObjectName objectName : objectNames) {
                MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(objectName);
                mbeanMetaData.put(objectName.toString(), mBeanInfo);
            }
            process.appendResult(mbeanModel);
            process.end();
        } catch (Throwable e) {
            logger.warn("listMetaData error", e);
            process.end(1, "list mbean metadata error");
        }
    }

通过 ManagementFactory.getPlatformMBeanServer()获取MBeanServer。

通过MBeanServer的queryNames获取ObjectName对象。

通过MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(objectName)获取MBeanInfo

相关文章