Java JMX Client


import java.io.IOException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class JMXClient {
public static void main(String[] args) {
try {
HashMap env = new HashMap();

String[] credentials = new String[] { "userid", "password" };
env.put("jmx.remote.credentials", credentials);

JMXServiceURL url = new JMXServiceURL(
"service:jmx:rmi:///jndi/rmi://server:1234/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
String domains[] = mbsc.getDomains();
for (int i = 0; i < domains.length; i++) {
System.out.println("Domain[" + i + "] = " + domains[i]);
}

listMBeans(mbsc, "ABC:type=TYPENAME,name=name2");

jmxc.close();
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* List all MBeans and their attributes.
*/
public static void listMBeans(MBeanServerConnection server, String path)
throws Exception {
javax.management.ObjectName on = new javax.management.ObjectName(path);
final Set names = server.queryNames(on, null);
for (final Iterator i = names.iterator(); i.hasNext();) {
ObjectName name = (ObjectName) i.next();
System.out.println("Got MBean: " + name);

try {
MBeanInfo info = server.getMBeanInfo((ObjectName) name);
MBeanAttributeInfo[] attrs = info.getAttributes();
if (attrs == null)
continue;
for (int j = 0; j < attrs.length; j++) {
if (attrs[j].isReadable()) {
try {

Object o = server.getAttribute(name, attrs[j]
.getName());
System.out.println("==>" + attrs[j].getName()
+ " = " + o);
} catch (Exception x) {
System.err.println("JmxClient failed to get "
+ attrs[j].getName());
}
}
}
} catch (Exception x) {
System.err.println("JmxClient failed to get MBeanInfo: " + x);
// x.printStackTrace(System.err);
}
}
}

public static void listMBeanAttributes_all(MBeanServerConnection server)
throws IOException {
final Set names = server.queryNames(null, null);
for (final Iterator i = names.iterator(); i.hasNext();) {
ObjectName name = (ObjectName) i.next();
System.out.println("Got MBean: " + name);

try {
MBeanInfo info = server.getMBeanInfo((ObjectName) name);
MBeanAttributeInfo[] attrs = info.getAttributes();
if (attrs == null)
continue;
for (int j = 0; j < attrs.length; j++) {
if (attrs[j].isReadable()) {
try {

Object o = server.getAttribute(name, attrs[j]
.getName());
System.out.println("==>" + attrs[j].getName()
+ " = " + o);
} catch (Exception x) {
System.err.println("JmxClient failed to get "
+ attrs[j].getName());
// x.printStackTrace(System.err);
}
}
}
} catch (Exception x) {
System.err.println("JmxClient failed to get MBeanInfo: " + x);
x.printStackTrace(System.err);
}
}
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s