To obtain the JMX properties of a local Java application based on the process ID

Source: Internet
Author: User


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");




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.