2

我被困在这里:

我需要得到的值

org.jboss.system.server.ServerInfo

使用此处的代码,我正在阅读 mbean 属性,但我只能找到 .hashvalues 的值!

final MBeanAttributeInfo[] attributes = server.getMBeanInfo(mbean).getAttributes();
for (final MBeanAttributeInfo attribute : attributes) {
                    String name = attribute.getName();                            
}

经过两天的搜索,我请求帮助!

非常感谢,罗曼。

4

3 回答 3

3
public static Map<String, Object> getAllAttributes(String host, int port, String mbeanName) throws MalformedObjectNameException, IOException, InstanceNotFoundException, IntrospectionException, ReflectionException, AttributeNotFoundException, MBeanException {
    // Get JMX connector and get MBean server connection
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi");
    JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
    MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();       

    // Query all attributes and values
    ObjectName name = new ObjectName(mbeanName);
    MBeanInfo info = mbsc.getMBeanInfo(name);
    MBeanAttributeInfo[] attrInfo = info.getAttributes();
    Map<String, Object> map = new HashMap<>();
    for (MBeanAttributeInfo attr : attrInfo) {
        if (attr.isReadable()) {
            //System.out.println("\t" + attr.getName() + " = " + mbsc.getAttribute(name, attr.getName()));
            map.put(attr.getName(), mbsc.getAttribute(name, attr.getName()));
            }
        }
    jmxc.close();
    return map;

}
于 2016-06-13T05:30:16.810 回答
2

这解决了我的问题,获取服务器信息:

MBeanServer server = getMBeanServer("jboss");
    ObjectName mbeanname = getMBeanName(server, "server.location", "service",
            "ServerName");
    MBeanInfo mbeanInfo = server.getMBeanInfo(mbeanname);
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    for (int i = 0; i < mbeanInfo.getAttributes().length; i++) {
        Map<String, String> attributeMap = new HashMap<String, String>();
        String attributeName = mbeanInfo.getAttributes()[i].getName();
        attributeMap.put("name", attributeName);
        String attributeValue = server.getAttribute(mbeanname, attributeName).toString();
        attributeMap.put(attributeName, attributeValue);
        attributeMap.put("value", attributeValue);
        list.add(attributeMap);
    }
于 2012-06-20T11:00:37.043 回答
0

不确定.hashcodes是什么意思。您能否提供一些输出作为示例并向我们展示所有相关代码?

于 2012-05-07T18:36:18.463 回答