---
title: "jmx的ObjectName中key对应的value的获取"
author: "chainhou"
date: "2013-07-11T14:12:36Z"
tags: []
categories: ["编程语言"]
slug: "jmx-objectname-key-value"
draft: false
lastmod: "2013-07-11T14:12:36Z"
id: 1903805
url: "https://chainhou.iteye.com/blog/1903805"
---

之前开发中用到JMX的地方，如果需要根据ObjectName中某个特定值进行判断，都是把ObjectName执行toString()后，再截取字符串，今天发现原来ObjectName中有个方法

```java

/**
     * Obtains the value associated with a key in a key property.
     *
     * @param property The property whose value is to be obtained.
     *
     * @return The value of the property, or null if there is no such
     * property in this ObjectName.
     *
     * @exception NullPointerException If <code>property</code> is null.
     */
    public String getKeyProperty(String property) throws NullPointerException {
        return _getKeyPropertyList().get(property);
    }

```

该方法只要把相应的key传进来就可以获取值，而不用再在整个String中查找。
比如

```java

ObjectName oname = new ObjectName("com.abc:name=threadpool.thread-pool-1,type=thread-pool,category=monitor,server=server");
要获取name的值，只需要oname.getKeyProperty("name");即可把后面的一串拿到。

```

 :wink: