Roman
Roman

Reputation: 171

get mbean-attribute values?

I'm stuck here:

I need to get the values of

org.jboss.system.server.ServerInfo

With the code here i'm reading the mbean-attributes, but instaed of values i can only find .hashvalues!

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

After two days of searching I ask for help!

Thanks a lot, Roman.

Upvotes: 2

Views: 8402

Answers (3)

gracie
gracie

Reputation: 61

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;

}

Upvotes: 5

Roman
Roman

Reputation: 171

this solved my problem, getting the server info:

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);
    }

Upvotes: 2

Nicholas
Nicholas

Reputation: 16056

Not sure what you mean by .hashcodes. Can you provide some output as an example and show us all the relevant code ?

Upvotes: 0

Related Questions