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 VerboseGC { 51 private MBeanServerConnection server; 52 private JMXConnector jmxc; 53 public VerboseGC(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(long interval, long samples) { 63 try { 64 PrintGCStat pstat = new PrintGCStat(server); 65 for (int i = 0; i < samples; i++) { 66 pstat.printVerboseGc(); 67 try { 68 Thread.sleep(interval); 69 } catch (InterruptedException e) { 70 System.exit(1); 71 } 72 } 73 } catch (IOException e) { 74 System.err.println("\nCommunication error: " + e.getMessage()); 75 System.exit(1); 76 } 77 } 78 79 82 private void connect(String urlPath) { 83 try { 84 JMXServiceURL url = new JMXServiceURL("rmi", "", 0, urlPath); 85 this.jmxc = JMXConnectorFactory.connect(url); 86 this.server = jmxc.getMBeanServerConnection(); 87 } catch (MalformedURLException e) { 88 } catch (IOException e) { 90 System.err.println("\nCommunication error: " + e.getMessage()); 91 System.exit(1); 92 } 93 } 94 95 public static void main(String [] args) { 96 if (args.length < 1) { 97 usage(); 98 } 99 100 String hostname = ""; 101 int port = -1; 102 long interval = 5000; long mins = 5; 104 for (int argIndex = 0; argIndex < args.length; argIndex++) { 105 String arg = args[argIndex]; 106 if (args[argIndex].startsWith("-")) { 107 if (arg.equals("-h") || 108 arg.equals("-help") || 109 arg.equals("-?")) { 110 usage(); 111 } else if (arg.startsWith("-interval=")) { 112 try { 113 interval = Integer.parseInt(arg.substring(10)) * 1000; 114 } catch (NumberFormatException ex) { 115 usage(); 116 } 117 } else if (arg.startsWith("-duration=")) { 118 try { 119 mins = Integer.parseInt(arg.substring(10)); 120 } catch (NumberFormatException ex) { 121 usage(); 122 } 123 } else { 124 System.err.println("Unrecognized option: " + arg); 126 usage(); 127 } 128 } else { 129 String [] arg2 = arg.split(":"); 130 if (arg2.length != 2) { 131 usage(); 132 } 133 hostname = arg2[0]; 134 try { 135 port = Integer.parseInt(arg2[1]); 136 } catch (NumberFormatException x) { 137 usage(); 138 } 139 if (port < 0) { 140 usage(); 141 } 142 } 143 } 144 145 VerboseGC vgc = new VerboseGC(hostname, port); 147 long samples = (mins * 60 * 1000) / interval; 148 vgc.dump(interval, samples); 149 150 } 151 152 private static void usage() { 153 System.out.print("Usage: java VerboseGC <hostname>:<port> "); 154 System.out.println(" [-interval=seconds] [-duration=minutes]"); 155 } 156 } 157
| Popular Tags
|