Created
May 6, 2015 17:20
-
-
Save lhotari/69056c6e257050fb1ecc to your computer and use it in GitHub Desktop.
dumping threads in code on Java8+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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