1. Getting the Service URL
When the local management agent is started, it publishes the service URL (which also contains the serialized stub for comm Unication–take a look at the URL, it's really quite funny) in a ok known location. This can is retrieved by using the Sun.management.ConnectorAddressLink (this class can is found in Java basic Librt.jar) c Lass like this:
private static Jmxserviceurl getlocalstubserviceurlfrompid (int pid)
throws IOException {
String address = Connectoraddresslink.importfrom (PID);
if (address!= null) {return to
new Jmxserviceurl (address)
;
} return null;
}
If you are didn ' t get any service URL, then the local management agent is not started. 2. Starting the local Management Agent
Starting the local management agent can do in various ways. In late versions to the JDK this is quite easily done. I am only going for the simple way today. In older your might need to resort to the attach command to load Java agents (virtualmachine VM = Virtualmachine.attach (PID ). Loadagent (<path to Management-agent.jar>, "Com.sun.management.jmxremote")).
To start the local agent, we simply execute the diagnostic command for starting it:
... Executecommandforpid (VM, PID, "managementagent.start_local");
... private static void Executecommandforpid (Virtualmachine vm, String pid,
string cmd) throws IOException {
Hotspotvirtualmachine HSVM = (hotspotvirtualmachine) VM;
Hsvm.executejcmd (cmd);
3. Opening the Mbeanserver Connection
This, as the as-is as a doing the mbeany things you want-do once your have the connection, the IS-done just as for you would normally D O It:
Jmxconnector jmxc = jmxconnectorfactory.connect (URL, null);
Mbeanserverconnection connection = Jmxc.getmbeanserverconnection ();
Getting attributes:
Connection.getattribute ("Java.lang:type=memory", "heapmemoryusage");