KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FullThreadDump


1 /*
2  * @(#)FullThreadDump.java 1.5 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)FullThreadDump.java 1.5 05/11/17
39  */

40
41 import javax.management.*;
42 import javax.management.remote.*;
43 import java.io.IOException JavaDoc;
44 import java.net.MalformedURLException JavaDoc;
45
46 /**
47  * This FullThreadDump class demonstrates the capability to get
48  * a full thread dump and also detect deadlock remotely.
49  */

50 public class FullThreadDump {
51     private MBeanServerConnection server;
52     private JMXConnector jmxc;
53     public FullThreadDump(String JavaDoc hostname, int port) {
54         System.out.println("Connecting to " + hostname + ":" + port);
55
56         // Create an RMI connector client and connect it to
57
// the RMI connector server
58
String JavaDoc urlPath = "/jndi/rmi://" + hostname + ":" + port + "/jmxrmi";
59         connect(urlPath);
60    }
61    
62    public void dump() {
63         try {
64             ThreadMonitor monitor = new ThreadMonitor(server);
65             monitor.threadDump();
66             if (!monitor.findDeadlock()) {
67                 System.out.println("No deadlock found.");
68             }
69         } catch (IOException JavaDoc e) {
70             System.err.println("\nCommunication error: " + e.getMessage());
71             System.exit(1);
72         }
73     }
74
75     /**
76      * Connect to a JMX agent of a given URL.
77      */

78     private void connect(String JavaDoc urlPath) {
79         try {
80             JMXServiceURL url = new JMXServiceURL("rmi", "", 0, urlPath);
81             this.jmxc = JMXConnectorFactory.connect(url);
82             this.server = jmxc.getMBeanServerConnection();
83         } catch (MalformedURLException JavaDoc e) {
84             // should not reach here
85
} catch (IOException JavaDoc e) {
86             System.err.println("\nCommunication error: " + e.getMessage());
87             System.exit(1);
88         }
89     }
90
91     public static void main(String JavaDoc[] args) {
92         if (args.length != 1) {
93             usage();
94         }
95
96         String JavaDoc[] arg2 = args[0].split(":");
97         if (arg2.length != 2) {
98             usage();
99         }
100         String JavaDoc hostname = arg2[0];
101         int port = -1;
102         try {
103             port = Integer.parseInt(arg2[1]);
104         } catch (NumberFormatException JavaDoc x) {
105             usage();
106         }
107         if (port < 0) {
108             usage();
109         }
110  
111         // get full thread dump and perform deadlock detection
112
FullThreadDump ftd = new FullThreadDump(hostname, port);
113         ftd.dump();
114     }
115
116     private static void usage() {
117         System.out.println("Usage: java FullThreadDump <hostname>:<port>");
118     }
119 }
120
Popular Tags