{"id":335,"date":"2009-12-05T11:56:38","date_gmt":"2009-12-05T10:56:38","guid":{"rendered":"http:\/\/www.gennard.net\/blog\/?p=335"},"modified":"2009-12-05T11:56:38","modified_gmt":"2009-12-05T10:56:38","slug":"using-sun-jvmstat-monitor-to-see-active-java-processes","status":"publish","type":"post","link":"http:\/\/www.gennard.net\/blog\/2009\/12\/using-sun-jvmstat-monitor-to-see-active-java-processes\/","title":{"rendered":"Using sun.jvmstat.monitor to see active Java processes"},"content":{"rendered":"<p>Monitor&#8217;ing Java processes can be achieved using the jvmstat monitor classes provided in the JVM.   The documentation is a bit sketchy but with a little experimenting it can be done.<\/p>\n<p>Below is a little example that shows you how to get a list of active Java processes.. which of course can then be used for other things \ud83d\ude42<\/p>\n<p>Here&#8217;s the code&#8230;<\/p>\n<p><code lang=\"java\" width=\"800\" lines=\"-1\" nowrap=\"0\"><br \/>\nimport java.net.URISyntaxException;<br \/>\nimport java.util.Set;<\/p>\n<p>import sun.jvmstat.monitor.*;<\/p>\n<p>public class sjps<br \/>\n{<br \/>\n   public static void main(String[] args)<br \/>\n          throws MonitorException, URISyntaxException<br \/>\n   {<br \/>\n      MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(\"localhost\");<br \/>\n      Set<Integer> activeVms = monitoredHost.activeVms();<br \/>\n      for (int psId : activeVms)<br \/>\n      {<br \/>\n         MonitoredVm monitoredVm = monitoredHost.getMonitoredVm<br \/>\n                   (new VmIdentifier(String.valueOf(psId)));<br \/>\n         String mainClass = MonitoredVmUtil.mainClass(monitoredVm, false);<br \/>\n         String vmVersion = MonitoredVmUtil.vmVersion(monitoredVm);<br \/>\n         String commandLine = MonitoredVmUtil.commandLine(monitoredVm);<br \/>\n      \t System.out.println(mainClass + \" [\" + psId + \"]\" +<br \/>\n                                       \" using : \"+vmVersion);<br \/>\n\t System.out.println(\" -> \"+commandLine);<br \/>\n      }<br \/>\n   }<\/p>\n<p>}<br \/>\n<\/code><\/p>\n<p>And to see the code running..<\/p>\n<p><code lang=\"text\" width=\"600\"><br \/>\n$ java sjps<br \/>\nsjps [865] using : 14.1-b02-90<br \/>\n -> sjps<br \/>\nJConsole [863] using : 14.1-b02-90<br \/>\n -> sun.tools.jconsole.JConsole<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Monitor&#8217;ing Java processes can be achieved using the jvmstat monitor classes provided in the JVM. The documentation is a bit sketchy but with a little experimenting it can be done. Below is a little example that shows you how to &hellip; <a href=\"http:\/\/www.gennard.net\/blog\/2009\/12\/using-sun-jvmstat-monitor-to-see-active-java-processes\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,17],"tags":[239,121,141],"_links":{"self":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts\/335"}],"collection":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/comments?post=335"}],"version-history":[{"count":0,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts\/335\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/media?parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/categories?post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/tags?post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}