Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/management/FullThreadDump/FullThreadDump.java
38829 views
/*1* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/383940/*41*/4243import javax.management.*;44import javax.management.remote.*;45import java.io.IOException;46import java.net.MalformedURLException;4748/**49* This FullThreadDump class demonstrates the capability to get50* a full thread dump and also detect deadlock remotely.51*/52public class FullThreadDump {53private MBeanServerConnection server;54private JMXConnector jmxc;55public FullThreadDump(String hostname, int port) {56System.out.println("Connecting to " + hostname + ":" + port);5758// Create an RMI connector client and connect it to59// the RMI connector server60String urlPath = "/jndi/rmi://" + hostname + ":" + port + "/jmxrmi";61connect(urlPath);62}6364public void dump() {65try {66ThreadMonitor monitor = new ThreadMonitor(server);67monitor.threadDump();68if (!monitor.findDeadlock()) {69System.out.println("No deadlock found.");70}71} catch (IOException e) {72System.err.println("\nCommunication error: " + e.getMessage());73System.exit(1);74}75}7677/**78* Connect to a JMX agent of a given URL.79*/80private void connect(String urlPath) {81try {82JMXServiceURL url = new JMXServiceURL("rmi", "", 0, urlPath);83this.jmxc = JMXConnectorFactory.connect(url);84this.server = jmxc.getMBeanServerConnection();85} catch (MalformedURLException e) {86// should not reach here87} catch (IOException e) {88System.err.println("\nCommunication error: " + e.getMessage());89System.exit(1);90}91}9293public static void main(String[] args) {94if (args.length != 1) {95usage();96}9798String[] arg2 = args[0].split(":");99if (arg2.length != 2) {100usage();101}102String hostname = arg2[0];103int port = -1;104try {105port = Integer.parseInt(arg2[1]);106} catch (NumberFormatException x) {107usage();108}109if (port < 0) {110usage();111}112113// get full thread dump and perform deadlock detection114FullThreadDump ftd = new FullThreadDump(hostname, port);115ftd.dump();116}117118private static void usage() {119System.out.println("Usage: java FullThreadDump <hostname>:<port>");120}121}122123124