Skip to content
Permalink
Browse files

Support ThreadDumper on JDK 11. Get rid of tools.jar dependency.

  • Loading branch information...
apangin committed Apr 12, 2019
1 parent 0352606 commit d62e79fcf8fd865dedb0ab8d3e5f6d0d2f03601c
Showing with 24 additions and 107 deletions.
  1. +1 −2 build.xml
  2. +0 −7 pom.xml
  3. +23 −34 src/one/nio/mgt/ThreadDumper.java
  4. +0 −64 src/one/nio/mgt/ThreadDumperImpl.java
@@ -14,10 +14,9 @@
<available file="${lib.dir}" type="dir" property="lib.dir.exists"/>
<path id="classpath.ref">
<fileset dir="${lib.dir}" includes="*.jar"/>
<fileset file="${java.home}/../lib/tools.jar"/>
</path>
</target>

<target name="retrieve-libs" unless="lib.dir.exists">
<echo message="Downloading external libraries..."/>
<mkdir dir="${lib.dir}"/>
@@ -70,13 +70,6 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.4.2</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>

<properties>
@@ -16,50 +16,39 @@

package one.nio.mgt;

import java.io.File;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.management.JMException;
import javax.management.ObjectName;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.io.PrintStream;
import java.lang.management.ManagementFactory;
import java.util.concurrent.atomic.AtomicLong;

public class ThreadDumper {
private static final Log log = LogFactory.getLog(ThreadDumper.class);
private static final AtomicLong dumpTime = new AtomicLong();
private static Method dumpMethod;

// tools.jar must be in class path when loading ThreadDumper implementation
private static Method getDumpMethod() throws IOException, ClassNotFoundException, NoSuchMethodException {
File javaHome = new File(System.getProperty("java.home"));
String toolsPath = javaHome.getName().equalsIgnoreCase("jre") ? "../lib/tools.jar" : "lib/tools.jar";

URL[] urls = new URL[] {
ThreadDumper.class.getProtectionDomain().getCodeSource().getLocation(),
new File(javaHome, toolsPath).getCanonicalFile().toURI().toURL(),
};

URLClassLoader loader = new URLClassLoader(urls, null);
return loader.loadClass("one.nio.mgt.ThreadDumperImpl").getMethod("dump", OutputStream.class);
}

public static synchronized void dump(OutputStream out) throws IOException {
public static void dump(OutputStream out) {
Object threadDump;
try {
if (dumpMethod == null) {
dumpMethod = getDumpMethod();
}
dumpMethod.invoke(null, out);
} catch (IOException e) {
throw e;
} catch (InvocationTargetException e) {
Throwable target = e.getTargetException();
throw target instanceof IOException ? (IOException) target : new IOException(target);
} catch (Exception e) {
throw new IOException(e);
threadDump = ManagementFactory.getPlatformMBeanServer().invoke(
new ObjectName("com.sun.management:type=DiagnosticCommand"),
"threadPrint",
new Object[]{null},
new String[]{"[Ljava.lang.String;"}
);
} catch (JMException e) {
log.warn("Failed to get threads dump: " + e);
return;
}

PrintStream printStream = out == null ? System.out : new PrintStream(out);
printStream.println(threadDump);
}

public static void dump(OutputStream out, long minDumpInterval) throws IOException {
public static void dump(OutputStream out, long minDumpInterval) {
long currentTime = System.currentTimeMillis();
long lastDumpTime = dumpTime.get();
if (currentTime - lastDumpTime >= minDumpInterval && dumpTime.compareAndSet(lastDumpTime, currentTime)) {

This file was deleted.

0 comments on commit d62e79f

Please sign in to comment.
You can’t perform that action at this time.