views:

343

answers:

2

I want to be able to log all JMX data accessible via jconsole. Is there a way to do this programmatically? I'm building a form of logging of the system, and I want to create intervaled data viewable with some tool similar to jconsole.

How would I go about doing this?

+3  A: 

java.lang.management.ManagementFactory gives you access to JMX data.

i.g.

List<MemoryPoolMXBean> memPoolBeans = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean mpb : memPoolBeans) {
    System.out.println("Memory Pool: " + mpb.getName());
}

Some samples are available at SO query: [java]+managementfactory

A good reading: http://www.ibm.com/developerworks/java/library/j-mxbeans/

For full implementation connecting to a remote VM:

Map<String,String[]> env = new HashMap<String, String[]>();
env.put( JMXConnector.CREDENTIALS, new String[]{"user","pass"} );
JMXServiceURL address = new JMXServiceURL("service:rmi:///jndi/rmi://host:port/jmxrmi");
JMXConnector connector = JMXConnectorFactory.connect(address,env);
MBeanServerConnection mbs = connector.getMBeanServerConnection();

//get all mbeans
Set<ObjectInstance> beans = mbs.queryMBeans(null,null);

for( ObjectInstance instance : beans )
{
    MBeanInfo info = mbs.getMBeanInfo( instance.getObjectName() );
}

From the info, you can query object names and attributes as desired.

stacker
This works, but is there an easy way to get all values from all objectnames? Right now, it seems as if I have to get na MBeanServerConnection, then query each value individually with mbs.getAttributes(..). Is there a better way?
Stefan Kendall
Okay, I can get all the ObjectNames with queryMBeans, but I'm still unable to automatically get all attributes.
Stefan Kendall
:P. Now, I can query all attributes, but the way you do it is extraordinarily circuitous. I'm going to edit your answer and make it a little more complete, then accept it.
Stefan Kendall
+2  A: 

I used this command line JMX client as a starting point when I wanted to do the same thing.

Mark
It looks like I really want 10.4, for the "Attributes" command, but the link ont he page is broken. Any ideas where I might find a mirror?
Stefan Kendall
I think that is the version I have. You can get it here: http://drop.io/jmxclient
Mark