Skip to content
Permalink
Browse files

Utility class to invoke HotSpot diagnostic commands

  • Loading branch information
apangin committed Oct 17, 2019
1 parent 388eef6 commit e8fe3609305af038844904d046fa17a68f57e7d1
Showing with 35 additions and 9 deletions.
  1. +1 −0 .gitignore
  2. +32 −0 src/one/nio/mgt/DiagnosticCommand.java
  3. +2 −9 src/one/nio/mgt/ThreadDumper.java
@@ -4,6 +4,7 @@
*.ear

/build
/out
/lib
/target

@@ -0,0 +1,32 @@
/*
* Copyright 2019 Odnoklassniki Ltd, Mail.Ru Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package one.nio.mgt;

import javax.management.JMException;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;

public class DiagnosticCommand {

public static String execute(String command, String... args) throws JMException {
return (String) ManagementFactory.getPlatformMBeanServer().invoke(
new ObjectName("com.sun.management:type=DiagnosticCommand"),
command,
new Object[]{args},
new String[]{"[Ljava.lang.String;"});
}
}
@@ -20,25 +20,18 @@
import org.apache.commons.logging.LogFactory;

import javax.management.JMException;
import javax.management.ObjectName;
import java.io.OutputStream;
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();

public static void dump(OutputStream out) {
Object threadDump;
String threadDump;
try {
threadDump = ManagementFactory.getPlatformMBeanServer().invoke(
new ObjectName("com.sun.management:type=DiagnosticCommand"),
"threadPrint",
new Object[]{null},
new String[]{"[Ljava.lang.String;"}
);
threadDump = DiagnosticCommand.execute("threadPrint");
} catch (JMException e) {
log.warn("Failed to get threads dump: " + e);
return;

0 comments on commit e8fe360

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