Skip to content

Commit

Permalink
Utility class to invoke HotSpot diagnostic commands
Browse files Browse the repository at this point in the history
  • Loading branch information
apangin committed Oct 17, 2019
1 parent 388eef6 commit e8fe360
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,6 +4,7 @@
*.ear

/build
/out
/lib
/target

Expand Down
32 changes: 32 additions & 0 deletions src/one/nio/mgt/DiagnosticCommand.java
@@ -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;"});
}
}
11 changes: 2 additions & 9 deletions src/one/nio/mgt/ThreadDumper.java
Expand Up @@ -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;
Expand Down

0 comments on commit e8fe360

Please sign in to comment.