Skip to content

Instantly share code, notes, and snippets.

@lhotari
Created May 6, 2015 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lhotari/69056c6e257050fb1ecc to your computer and use it in GitHub Desktop.
Save lhotari/69056c6e257050fb1ecc to your computer and use it in GitHub Desktop.
dumping threads in code on Java8+
import javax.management.DynamicMBean;
import javax.management.MBeanException;
import javax.management.ReflectionException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class DiagnosticCommandMBeanHelper {
public static String threadPrint() throws ReflectionException, MBeanException {
try {
return callDiagnosticsMethod("threadPrint");
} catch (ClassNotFoundException e) {
throw new ReflectionException(e);
} catch (NoSuchMethodException e) {
throw new ReflectionException(e);
} catch (InvocationTargetException e) {
throw new ReflectionException(e);
} catch (IllegalAccessException e) {
throw new ReflectionException(e);
}
}
private static String callDiagnosticsMethod(String actionName) throws MBeanException, ReflectionException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
String[] emptyStringArgs = {};
Object[] dcmdArgs = {emptyStringArgs};
String[] signature = {String[].class.getName()};
Class<?> clazz = Class.forName("sun.management.ManagementFactoryHelper");
Method method = clazz.getMethod("getDiagnosticCommandMBean");
DynamicMBean dcmd = (DynamicMBean)method.invoke(null);
return (String)dcmd.invoke(actionName, dcmdArgs, signature);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment