{"id":630,"date":"2015-01-29T12:29:14","date_gmt":"2015-01-29T11:29:14","guid":{"rendered":"http:\/\/hirt.se\/blog\/?p=630"},"modified":"2015-01-29T12:29:14","modified_gmt":"2015-01-29T11:29:14","slug":"reading-jmx-metrics","status":"publish","type":"post","link":"https:\/\/hirt.se\/blog\/?p=630","title":{"rendered":"Reading JMX Metrics"},"content":{"rendered":"<p>I found this question on Stack Overflow about how to connect to a locally running process and read JMX metrics. I thought I\u2019d explain a little bit more in detail here on the blog. The steps involved are:<\/p>\n<ol>\n<ol>\n<li>Try to get the JMX Service URL for the local JMX agent.<\/li>\n<li>If you couldn\u2019t, try to start the local JMX agent.<\/li>\n<li>Open an MBeanServerConnection to the local JMX agent.<\/li>\n<li>Do whatever MBean-ish stuff you were planning on doing, for example reading attributes.<\/li>\n<\/ol>\n<\/ol>\n<p>This is pretty much as simple as it sounds. Let\u2019s do them in order.<\/p>\n<h2>Getting the Service URL<\/h2>\n<p>When the local management agent is started, it publishes the service URL (which also contains the serialized stub for communication \u2013 take a look at the URL, it\u2019s really quite funny) in a well known location. This can be retrieved by using the sun.management.ConnectorAddressLink class like this:<\/p>\n<blockquote>\n<p>private static JMXServiceURL getLocalStubServiceURLFromPID(int pid)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throws IOException {<br \/>&nbsp;&nbsp;&nbsp; String address = ConnectorAddressLink.importFrom(pid);<br \/>&nbsp;&nbsp;&nbsp; if (address != null) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new JMXServiceURL(address);<br \/>&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp; return null;<br \/>}<\/p>\n<\/blockquote>\n<p>If you didn\u2019t get any service url, then the local management agent was not started. <\/p>\n<h2>Starting the Local Management Agent<\/h2>\n<p>Starting the local management agent can be done in various ways. In late versions of the JDK this is quite easily done. I am only going to show you the simple way today. In older you might need to resort to the attach command to load java agents (VirtualMachine vm = VirtualMachine.attach(pid).loadAgent(&lt;path to management-agent.jar&gt;,\u201dcom.sun.management.jmxremote\u201d)).<\/p>\n<p>To start the local agent, we simply execute the diagnostic command for starting it:<\/p>\n<blockquote>\n<p>\u2026<\/p>\n<p>executeCommandForPID(vm, pid, &#8220;ManagementAgent.start_local&#8221;);  <\/p>\n<p>\u2026 <\/p>\n<p>private static void executeCommandForPID(VirtualMachine vm, String pid,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String cmd) throws IOException {<br \/>&nbsp;&nbsp;&nbsp; HotSpotVirtualMachine hsvm = (HotSpotVirtualMachine) vm;<br \/>&nbsp;&nbsp;&nbsp; hsvm.executeJCmd(cmd);<br \/>}<\/p>\n<\/blockquote>\n<p>&nbsp; <\/p>\n<h2>Opening the MBeanServer Connection<\/h2>\n<p>This, as well as doing the MBeany things you want to do once you have the connection, is done just as you would normally do it:<\/p>\n<blockquote>\n<p>JMXConnector jmxc = JMXConnectorFactory.connect(url, null);<br \/>MBeanServerConnection connection = jmxc.getMBeanServerConnection();<\/p>\n<\/blockquote>\n<p>Getting attributes: <\/p>\n<blockquote>\n<p>connection.getAttribute(&#8220;java.lang:type=Memory&#8221;, &#8220;HeapMemoryUsage&#8221;);<\/p>\n<\/blockquote>\n<p>&nbsp; <\/p>\n<h2><\/h2>\n<h2>Summary<\/h2>\n<ul>\n<ul>\n<li>Hooking up to the local management agent is quite easy.<\/li>\n<li>There are valuable metrics that can be collected.<\/li>\n<li><a href=\"http:\/\/hirt.se\/downloads\/software\/javasnippets\/GetJMXMetricsExample.java\">Here<\/a> is a full example getting the JVM CPU load as well as used heap.<\/li>\n<\/ul>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I found this question on Stack Overflow about how to connect to a locally running process and read JMX metrics. I thought I\u2019d explain a little bit more in detail here on the blog. The steps involved are: Try to get the JMX Service URL for the local JMX agent. If you couldn\u2019t, try to [&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-630","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\/630","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=630"}],"version-history":[{"count":1,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/630\/revisions"}],"predecessor-version":[{"id":631,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/630\/revisions\/631"}],"wp:attachment":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}