Apache Tomcat

How to expose and dump all JMX beans for Tomcat

Often it is necessary to dump all the JMX beans and their values in a file. This helps to understand the JMX tree and various attributes available. Those attributes can be used to monitor various aspects of the JVM e.g heap, threads, System Properties, JVM attributes, Connection Pools etc. You can compile and include the below class as a Listener in Tomcat JVM to easily dump all the Mbeans available:

PropertiesExporter.java

package com.middlewareworld.tcserver.properties;

import java.io.*;
import java.lang.management.ManagementFactory;
import java.util.*;

import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.digester.Digester;
import org.apache.tomcat.util.IntrospectionUtils.PropertySource;
import org.apache.tomcat.util.IntrospectionUtils;

import org.apache.catalina.mbeans.*;
import javax.management.*;
import org.apache.catalina.*;
import org.apache.catalina.realm.*;
import org.apache.catalina.core.*;
// import org.apache.tomcat.util.modeler.*;

public class PropertiesExporter implements LifecycleListener {

MBeanServer mbsc = null;
PrintStream fileOut = null;

String xmlEscapeText(String t) {
if (t == null) {
return “”;
}
StringBuilder sb = new StringBuilder();
for(int i = 0; i < t.length(); i++){
char c = t.charAt(i);
switch(c){
case ‘<‘:
sb.append(“&lt;”);
break;
case ‘>’:
sb.append(“&gt;”);
break;
case ‘\”‘:
sb.append(“&quot;”);
break;
case ‘&’:
sb.append(“&amp;”);
break;
case ‘\”:
sb.append(“&apos;”);
break;
case ‘\n’:
sb.append(“&#10;”);
break;
case ‘\r’:
break;
default:
if(c > 0x7e) {
sb.append(“&#”+((int)c)+”;”);
} else {
sb.append(c);
}
}
}
return sb.toString();
}

public PropertiesExporter() {
}

public void lifecycleEvent(LifecycleEvent event) {
StringBuilder sb = new StringBuilder(2048);
if ( log.isDebugEnabled() ) {
log.debug((new StringBuilder(“Processing event:[“)).append(event.getType()).append(“]”).toString());
}

//Extracting Code …………………………
final List<MBeanServer> servers = new LinkedList<MBeanServer>();
servers.add(ManagementFactory.getPlatformMBeanServer());
servers.addAll(MBeanServerFactory.findMBeanServer(null));
for (final MBeanServer server : servers) {
// os.println(” <tr><td colspan=’4′>&nbsp;</td></tr>”);
// os.println(” <tr><td>Server:</td><td colspan=’3′>”
// + server.getClass().getName() + “</td></tr>”);
System.out.println(server.getClass().getName());

final Set<ObjectName> mbeans = new HashSet<ObjectName>();
mbeans.addAll(server.queryNames(null, null));
for (final ObjectName mbean : mbeans) {
// os.println(” <tr><td colspan=’4′>&nbsp;</td></tr>”);
// os.println(” <tr><td>MBean:</td><td colspan=’3′>” + mbean
// + “</td></tr>”);
System.out.println(“========================================================================”);
System.out.println(mbean);
System.out.println(“========================================================================”);

final MBeanAttributeInfo[] attributes = server.getMBeanInfo(
mbean).getAttributes();
for (final MBeanAttributeInfo attribute : attributes) {
// os.print(” <tr><td>&nbsp;</td><td>” + attribute.getName()
// + “</td><td>” + attribute.getType() + “</td><td>”);
System.out.println(“Attribute Name = “+attribute.getName());

try {
final Object value = server.getAttribute(mbean,
attribute.getName());
if (value == null) {
// os.print(“<font color=’#660000′>null</font>”);
System.out.println(“value == “+null);
} else {
// os.print(value.toString());
System.out.println(“value = “+value.toString());
}
} catch (Exception e) {
// os.print(“<font color=’#990000′>” + e.getMessage()
// + “</font>”);
System.out.println(“e.getMessage() == “+e.getMessage());
}

// os.println(“</td></tr>”);
}
}
}

//END Extracting Code …………………………

} catch (Exception e) {
log.error(“Error writing effectiveProperties: ” + fileoutName, e);
}
}
}

}

One thought on “How to expose and dump all JMX beans for Tomcat

Leave a Reply

Your email address will not be published. Required fields are marked *