简体   繁体   中英

Error accessing Cassandra MBeans from Java

I'm trying to access the messaging metrics provided by Cassandra from Java using JMX. I get the correct results when I use the following query with swiss java knife

java -jar sjk-plus-0.4.2.jar mx -s localhost:7100 -mg -all -b org.apache.cassandra.metrics:type=Messaging,name=* -f Mean
org.apache.cassandra.metrics:type=Messaging,name=CrossNodeLatency
1331.0469921040174
org.apache.cassandra.metrics:type=Messaging,name=datacenter1-Latency
1331.1071897694487

However, with the following Java code I get a javax.management.InstanceNotFoundException: org.apache.cassandra.metrics:type=Messaging exception.

JMXServiceURL url = null;
        try {
            url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://127.0.0.1:7100/jmxrmi");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        JMXConnector mConnector = null;
        try {
            mConnector = JMXConnectorFactory.connect(url);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        MBeanServerConnection mMBSC = null;
        try {
            mMBSC = mConnector.getMBeanServerConnection();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ObjectName mObjectName = null;
        try {
            mObjectName = new ObjectName("org.apache.cassandra.metrics:type=Messaging");
        } catch (MalformedObjectNameException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Set<ObjectName> myMbean = null;
        try {
            myMbean = mMBSC.queryNames(mObjectName, null);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            System.out.println((mMBSC.getAttribute(mObjectName, "*")).toString());
        } catch (AttributeNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstanceNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MBeanException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ReflectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Could someone please explain where am I making a mistake?

Swiss Java Knife seems to accept an ObjectName wildcard (or pattern) as:

org.apache.cassandra.metrics:type=Messaging,name=*

but your code is simply looking up a non-pattern MBean as:

org.apache.cassandra.metrics:type=Messaging

Change your code to:

  1. Use the pattern
  2. queryNames will return a Set of matching ObjectNames, so iterate through the set and query each one.
  3. You need to specify a string array of actual MBean attribute names, not "*" as in your code. (It would be nice if that was supported)

Something like:

mObjectName = new ObjectName("org.apache.cassandra.metrics:type=Messaging,name=*");
Set<ObjectName> names = mMBSC.queryNames(mObjectName, null);
for(ObjectName on: names) {
  System.out.println(on + "\n" + mMBSC.getAttribute(on, "Mean").toString());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM