There are some frequently used Mbeans in the JVM that can write a code to get:
public class Test {public static void Main (string[] args) throws Exception { Mbeanserver server = Managementfacto Ry.getplatformmbeanserver (); Set instances = Server.querymbeans (null, NULL); Iterator Iterator = Instances.iterator (); while (Iterator.hasnext ()) { ObjectInstance instance = (objectinstance) iterator.next (); System.out.println ("MBean Found:"); System.out.println ("Class name:\t" + instance.getclassname ()); System.out.println ("Object name:\t" + instance.getobjectname ()); System.out.println ("****************************************");}}}
Based on the list, we can obtain the desired information through an mbean, for example, the size of the code cache:
public class Testmbean {final static String pid = "6228"; Final static String connector_address = "com.sun.management.jmxremote.localConnectorAddress"; public static void Main (string[] args) throws Exception {virtualmachine vm = Virtualmachine.attach (PID); Jmxconnector connector = null; String connectoraddress = Vm.getagentproperties (). GetProperty (connector_address); String Agent = Vm.getsystemproperties (). GetProperty ("java.home") + File.separator + "Lib" + File.separator + "Management-agent.jar"; Vm.loadagent (agent); Connectoraddress = Vm.getagentproperties (). GetProperty (connector_address); Jmxserviceurl url = new Jmxserviceurl (connectoraddress); Connector = jmxconnectorfactory.connect (URL); Mbeanserverconnection mbeanconn = Connector.getmbeanserverconnection (); ObjectName name = new ObjectName ("Java.lang:type=memorypool,name=code Cache"); System.out.println (MBEANCONN.GEtattribute (name, "Usage")); if (connector! = null) {connector.close (); } Vm.detach (); }}
The classes actually obtained by ObjectName ("Java.lang:type=memorypool,name=code Cache") are:
Sun.management.MemoryPoolImpl
In the GetAttribute (name, "Usage") The corresponding method is called:
Public Java.lang.management.MemoryUsage Getusage () {}
The use of other Mbean is similar to this.
---------- ---------- ---------- ---------- ----------style line------------------ -- ---------- ---------- ----------
Assuming that the system's Mbean does not satisfy the need, then we can customize the Mbean, such as the standard Mbean such as the following:
The standard Mbean has several limitations: 1, must implement Interface 2, must have a public type of constructor 3, naming rules public class My implements Mymbean {public String Gethello () {
return "Hello World"; }} Public interface Mymbean {public String Gethello ();}
You can then add this mbean to Mbeanserver:
public class Test {public static final void main (string[] args) throws Exception { Mbeanserver mbeanserver = Mana Gementfactory.getplatformmbeanserver (); ObjectName ObjectName = new ObjectName ("Com.wszt:type=mymbean"); Mbeanserver.creatembean ("My", ObjectName);} }
attach& usage is the same as above, do not repeat. In addition to the standard Mbean, there are dynamic Mbean, Model Bean, Open Mbean, and similar usage.
---------- ------------------------------ ----------style line---------- -------------------------- ---- ----------
A simple look at JMX is a JVM's management framework, the Mbean is the encapsulation of resources, and then after the agent to be packaged, and finally the interface to provide external to facilitate the use, see:
[Java] Jmx