{"id":207,"date":"2012-04-24T15:02:23","date_gmt":"2012-04-24T14:02:23","guid":{"rendered":"http:\/\/hirt.se\/blog\/?p=207"},"modified":"2012-04-24T15:03:25","modified_gmt":"2012-04-24T14:03:25","slug":"remote-jrcmd-for-jrockit-r28-x","status":"publish","type":"post","link":"https:\/\/hirt.se\/blog\/?p=207","title":{"rendered":"Remote JRCMD for JRockit R28.x"},"content":{"rendered":"<p>On popular demand, here is how write a remote JRCMD for R28.x:<\/p>\n<pre>import java.lang.management.ManagementFactory;\r\nimport java.net.MalformedURLException;\r\nimport java.util.HashMap;\r\nimport java.util.Iterator;\r\nimport java.util.Map;\r\n\r\nimport javax.management.Attribute;\r\nimport javax.management.InstanceNotFoundException;\r\nimport javax.management.MBeanAttributeInfo;\r\nimport javax.management.MBeanServerConnection;\r\nimport javax.management.ObjectName;\r\nimport javax.management.remote.JMXConnector;\r\nimport javax.management.remote.JMXConnectorFactory;\r\nimport javax.management.remote.JMXServiceURL;\r\n\r\n\/**\r\n * Simple code example on how to execute\r\n * ctrl-break handlers remotely.\r\n * \r\n * Usage:\r\n * RemoteJRCMD -host -port -user -pass -command []\r\n * \r\n * All arguments are optional. If no command is\r\n * specified, all performance counters and their\r\n * current values are listed.\r\n * \r\n * @author Marcus Hirt\r\n *\/\r\npublic final class RemoteJRCMD {\r\n\tprivate final static String KEY_CREDENTIALS = \r\n\t\t&quot;jmx.remote.credentials&quot;;\r\n\tprivate final static String JROCKIT_PERFCOUNTER_MBEAN_NAME = \r\n\t\t&quot;oracle.jrockit.management:type=PerfCounters&quot;;\r\n\tprivate final static String JROCKIT_CONSOLE_MBEAN_NAME = \r\n\t\t&quot;oracle.jrockit.management:type=JRockitConsole&quot;;\r\n\tprivate final static String[] SIGNATURE = \r\n\t\tnew String[] {&quot;java.lang.String&quot;};\r\n\tprivate final static String DIAGNOSTIC_COMMAND_MBEAN_NAME = \r\n\t\t&quot;oracle.jrockit.management:type=DiagnosticCommand&quot;;\r\n\r\n\tpublic static void main(String[] args)\r\n\t\t\tthrows Exception {\r\n\t\tHashMap commandMap = \r\n\t\t\tparseArguments(args);\r\n\t\texecuteCommand(\r\n\t\t\t\tcommandMap.get(&quot;-host&quot;),\r\n\t\t\t\tInteger.parseInt(commandMap.get(&quot;-port&quot;)),\r\n\t\t\t\tcommandMap.get(&quot;-user&quot;),\r\n\t\t\t\tcommandMap.get(&quot;-password&quot;),\r\n\t\t\t\tcommandMap.get(&quot;-command&quot;));\r\n\t}\r\n\r\n\tprivate static HashMap parseArguments(\r\n\t\t\tString[] args) {\r\n\t\tHashMap commandMap = \r\n\t\t\tnew HashMap();\r\n\t\tcommandMap.put(&quot;-host&quot;, &quot;localhost&quot;);\r\n\t\tcommandMap.put(&quot;-port&quot;, &quot;7091&quot;);\r\n\t\tfor (int i = 0; i &lt; args.length; i++) {\r\n\t\t\tif (args[i].startsWith(&quot;-&quot;)) {\r\n\t\t\t\tStringBuilder buf = new StringBuilder();\r\n\t\t\t\tint j = i + 1;\r\n\t\t\t\twhile (j &lt; args.length\r\n\t\t\t\t\t\t&amp;&amp; !args[j].startsWith(&quot;-&quot;)) {\r\n\t\t\t\t\tbuf.append(&quot; &quot;);\r\n\t\t\t\t\tbuf.append(args[j++]);\r\n\t\t\t\t}\r\n\t\t\t\tcommandMap.put(args[i],\r\n\t\t\t\t\t\tbuf.toString().trim());\r\n\t\t\t\ti = j - 1;\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn commandMap;\r\n\t}\r\n\r\n\t@SuppressWarnings(&quot;unchecked&quot;)\r\n\tpublic static void executeCommand(\r\n\t\t\tString host, int port, String user,\r\n\t\t\tString password, String command)\r\n\t\t\tthrows Exception {\r\n\t\tMBeanServerConnection server = null;\r\n\t\tJMXConnector jmxc = null;\r\n\t\tMap map = null;\r\n\t\tif (user != null || password != null) {\r\n\t\t\tmap = new HashMap();\r\n\t\t\tfinal String[] credentials = new String[2];\r\n\t\t\tcredentials[0] = user;\r\n\t\t\tcredentials[1] = password;\r\n\t\t\tmap.put(KEY_CREDENTIALS, credentials);\r\n\t\t}\r\n\t\t\/\/ Use same convention as Sun. localhost:0 means\r\n\t\t\/\/ &quot;VM, monitor thyself!&quot;\r\n\t\tif (host.equals(&quot;localhost&quot;) &amp;&amp; port == 0) {\r\n\t\t\tserver = ManagementFactory.getPlatformMBeanServer();\r\n\t\t} else {\r\n\t\t\tjmxc = JMXConnectorFactory.newJMXConnector(\r\n\t\t\t\t\tcreateConnectionURL(host,\r\n\t\t\t\t\t\t\tport), map);\r\n\t\t\tjmxc.connect();\r\n\t\t\tserver = jmxc.getMBeanServerConnection();\r\n\t\t}\r\n\r\n\t\tSystem.out.println(&quot;Connected to &quot; + host\r\n\t\t\t\t+ &quot;:&quot; + port);\r\n\r\n\t\ttry {\r\n\t\t\tserver.getMBeanInfo(new ObjectName(\r\n\t\t\t\t\tJROCKIT_CONSOLE_MBEAN_NAME));\r\n\t\t} catch (InstanceNotFoundException e1) {\r\n\t\t\tserver.createMBean(\r\n\t\t\t\t\t&quot;oracle.jrockit.management.JRockitConsole&quot;,\r\n\t\t\t\t\tnull);\r\n\t\t}\r\n\r\n\t\tif (command == null) {\r\n\t\t\tObjectName perfCounterObjectName = new ObjectName(\r\n\t\t\t\t\tJROCKIT_PERFCOUNTER_MBEAN_NAME);\r\n\t\t\tSystem.out.println(&quot;Listing all counters...&quot;);\r\n\t\t\tMBeanAttributeInfo[] attributes = server.getMBeanInfo(\r\n\t\t\t\t\tperfCounterObjectName).getAttributes();\r\n\t\t\tSystem.out.println(&quot;Counter\\tValue\\n=======\\t====&quot;);\r\n\r\n\t\t\tString[] attributeNames = new String[attributes.length];\r\n\t\t\tfor (int i = 0; i &lt; attributes.length; i++) {\r\n\t\t\t\tattributeNames[i] = attributes[i].getName();\r\n\t\t\t}\r\n\t\t\tIterator valueIter = server.getAttributes(\r\n\t\t\t\t\tperfCounterObjectName,\r\n\t\t\t\t\tattributeNames).iterator();\r\n\t\t\twhile (valueIter.hasNext()) {\r\n\t\t\t\tAttribute attr = (Attribute) valueIter.next();\r\n\t\t\t\tSystem.out.println(attr.getName()\r\n\t\t\t\t\t\t+ &quot;\\t=\\t&quot;\r\n\t\t\t\t\t\t+ attr.getValue());\r\n\t\t\t}\r\n\t\t} else {\r\n\t\t\tSystem.out.println(&quot;Invoking the ctrl-break command '&quot;\r\n\t\t\t\t\t+ command + &quot;'...&quot;);\r\n\t\t\tObjectName consoleObjectName = new ObjectName(\r\n\t\t\t\t\tDIAGNOSTIC_COMMAND_MBEAN_NAME);\r\n\t\t\tObject[] params = new Object[1];\r\n\t\t\tparams[0] = command;\r\n\t\t\tSystem.out.println(&quot;The CtrlBreakCommand returned: \\n&quot;\r\n\t\t\t\t\t+ server.invoke(\r\n\t\t\t\t\t\t\tconsoleObjectName,\r\n\t\t\t\t\t\t\t&quot;execute&quot;, params,\r\n\t\t\t\t\t\t\tSIGNATURE));\r\n\t\t}\r\n\r\n\t\tif (jmxc != null) {\r\n\t\t\tjmxc.close();\r\n\t\t}\r\n\t}\r\n\r\n\tprivate static JMXServiceURL createConnectionURL(\r\n\t\t\tString host, int port)\r\n\t\t\tthrows MalformedURLException {\r\n\t\treturn new JMXServiceURL(&quot;rmi&quot;, &quot;&quot;, 0,\r\n\t\t\t\t&quot;\/jndi\/rmi:\/\/&quot; + host + &quot;:&quot;\r\n\t\t\t\t\t\t+ port + &quot;\/jmxrmi&quot;);\r\n\t}\r\n}<\/pre>\n<p>(This example can also be found in the <a href=\"http:\/\/www.amazon.com\/Oracle-JRockit-The-Definitive-Guide\/dp\/1847198066\/ref=ntt_at_ep_dpt_1\">JRockit book<\/a>, at the end of chapter 11.)<\/p>\n<p>The following example would list all the performance counters and their values on the JVM running the RemoteJRCMD itself: <\/p>\n<blockquote>\n<p><code>java RemoteJRCMD \u2013port 0<\/code><\/p>\n<\/blockquote>\n<p>The following example would list all the available commands in a JVM with a management agent open on the default port (7091) on localhost: <\/p>\n<blockquote>\n<p><code>java RemoteJRCMD \u2013command help<\/code><\/p>\n<\/blockquote>\n<p>The following example would print the help on the command \u201cprint_threads\u201d, which performs a thread stack dump: <\/p>\n<blockquote>\n<p><code>java RemoteJRCMD \u2013command help print_threads<\/code><\/p>\n<\/blockquote>\n<p>The following example would print a heap histogram per class for the JVM running on port 4711 on the host adastra: <\/p>\n<blockquote>\n<p><code>java RemoteJRCMD \u2013port 4711 \u2013host adastra \u2013command print_object_summary<\/code><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>On popular demand, here is how write a remote JRCMD for R28.x: import java.lang.management.ManagementFactory; import java.net.MalformedURLException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import javax.management.Attribute; import javax.management.InstanceNotFoundException; import javax.management.MBeanAttributeInfo; import javax.management.MBeanServerConnection; import javax.management.ObjectName; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; \/** * Simple code example on how to execute * ctrl-break handlers remotely. * * Usage: * [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-207","post","type-post","status-publish","format-standard","hentry","category-java-mission-control-fair-dj"],"_links":{"self":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=207"}],"version-history":[{"count":2,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/207\/revisions"}],"predecessor-version":[{"id":209,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/207\/revisions\/209"}],"wp:attachment":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}