如何获取String类型的ObjectName及其属性

问题背景:最近在做应用监控系统,使用jolokia+prometheus+grafana,其中jolokia负责获取JMX端点信息。

一、通过java.lang.management.ManagementFactory类获取

java.lang.management.ManagementFactory的成员变量是各种MXBean,我们可以直接获取起ObjectName,如下图:

/**
 * String representation of the
 * <tt>ObjectName</tt> for the {@link ClassLoadingMXBean}.
 */
public final static String CLASS_LOADING_MXBEAN_NAME =
    "java.lang:type=ClassLoading";

/**
 * String representation of the
 * <tt>ObjectName</tt> for the {@link CompilationMXBean}.
 */
public final static String COMPILATION_MXBEAN_NAME =
    "java.lang:type=Compilation";

/**
 * String representation of the
 * <tt>ObjectName</tt> for the {@link MemoryMXBean}.
 */
public final static String MEMORY_MXBEAN_NAME =
    "java.lang:type=Memory";

/**
 * String representation of the
 * <tt>ObjectName</tt> for the {@link OperatingSystemMXBean}.
 */
public final static String OPERATING_SYSTEM_MXBEAN_NAME =
    "java.lang:type=OperatingSystem";

/**
 * String representation of the
 * <tt>ObjectName</tt> for the {@link RuntimeMXBean}.
 */
public final static String RUNTIME_MXBEAN_NAME =
    "java.lang:type=Runtime";

/**
 * String representation of the
 * <tt>ObjectName</tt> for the {@link ThreadMXBean}.
 */
public final static String THREAD_MXBEAN_NAME =
    "java.lang:type=Threading";

/**
 * The domain name and the type key property in
 * the <tt>ObjectName</tt> for a {@link GarbageCollectorMXBean}.
 * The unique <tt>ObjectName</tt> for a <tt>GarbageCollectorMXBean</tt>
 * can be formed by appending this string with
 * "<tt>,name=</tt><i>collector's name</i>".
 */
public final static String GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE =
    "java.lang:type=GarbageCollector";

/**
 * The domain name and the type key property in
 * the <tt>ObjectName</tt> for a {@link MemoryManagerMXBean}.
 * The unique <tt>ObjectName</tt> for a <tt>MemoryManagerMXBean</tt>
 * can be formed by appending this string with
 * "<tt>,name=</tt><i>manager's name</i>".
 */
public final static String MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE=
    "java.lang:type=MemoryManager";

/**
 * The domain name and the type key property in
 * the <tt>ObjectName</tt> for a {@link MemoryPoolMXBean}.
 * The unique <tt>ObjectName</tt> for a <tt>MemoryPoolMXBean</tt>
 * can be formed by appending this string with
 * <tt>,name=</tt><i>pool's name</i>.
 */
public final static String MEMORY_POOL_MXBEAN_DOMAIN_TYPE=
    "java.lang:type=MemoryPool";

每个成员变量上面的@link注释都指向该MXBean,我们想获得具体的度量指标,可以点进去查看其成员变量,里面有get方法,再点进去看,就可以看到其具体属性值。例如查找已使用堆内存

ManagementFactory——>MemoryMXBean——>getHeapMemoryUsage()——>MemoryUsage.used

附1:堆内存参数的关系,详情见java.lang.management.MemoryUsage的源码

* Below is a picture showing an example of a memory pool:
*
* <pre>
*        +----------------------------------------------+
*        +           |                  +
*        +           |                  +
*        +----------------------------------------------+
*
*        |--------|
*           init
*        |---------------|
*               used
*        |---------------------------|
*                  committed
*        |----------------------------------------------|
*                            max
* </pre>

附2:使用jolokia获取JMX端点信息的方式(SpringBoot需要引入jolokia-core使jolokia端点生效):

private void setPromethues(URI uri, String serviceId) throws MalformedObjectNameException, J4pException {
    J4pClient j4pClient = new J4pClient(uri.toString() + "/jolokia");
    // 获取String
    J4pReadRequest request = new J4pReadRequest("java.lang:type=Memory", "HeapMemoryUsage");
    J4pReadResponse response = j4pClient.execute(request);
    Map<String, Long> values = response.getValue();
    logger.info("response values : {}", JSON.toJSONString(values));
    Long used = values.get("used");
}
二、通过jconsole工具获取

有时候为了更直观的获取ObjectName或者说验证我们之前获取的数据,我们可以使用jconsole工具获取。终端直接输入jconsole,选择目标进程,直接连接:

VM概要展示了应用的基本信息,比较直观:

在这里插入图片描述

MBean显示了我们监控的具体信息,选择java.lang包,可以查看各种ObjectName,查看属性可以获取具体属性值。

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值