1 36 37 40 41 import javax.management.*; 42 import javax.management.remote.*; 43 import java.io.IOException ; 44 import java.net.MalformedURLException ; 45 46 50 public class FullThreadDump { 51 private MBeanServerConnection server; 52 private JMXConnector jmxc; 53 public FullThreadDump(String hostname, int port) { 54 System.out.println("Connecting to " + hostname + ":" + port); 55 56 String 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 e) { 70 System.err.println("\nCommunication error: " + e.getMessage()); 71 System.exit(1); 72 } 73 } 74 75 78 private void connect(String 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 e) { 84 } catch (IOException e) { 86 System.err.println("\nCommunication error: " + e.getMessage()); 87 System.exit(1); 88 } 89 } 90 91 public static void main(String [] args) { 92 if (args.length != 1) { 93 usage(); 94 } 95 96 String [] arg2 = args[0].split(":"); 97 if (arg2.length != 2) { 98 usage(); 99 } 100 String hostname = arg2[0]; 101 int port = -1; 102 try { 103 port = Integer.parseInt(arg2[1]); 104 } catch (NumberFormatException x) { 105 usage(); 106 } 107 if (port < 0) { 108 usage(); 109 } 110 111 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 |