How to connect to a java program on localhost jvm using JMX?

后端 未结 5 1645
孤独总比滥情好
孤独总比滥情好 2020-12-04 08:23

I should connect to a java program on localhost jvm using JMX. In other words I want to develop a JMX client to config a java program on localhost.

  • Don\'t r

5条回答
  •  情书的邮戳
    2020-12-04 09:06

    Simplest means:

    import javax.management.Attribute;
    import javax.management.AttributeList;
    import java.lang.management.ManagementFactory;
    import javax.management.MBeanServer;
    import javax.management.ObjectName;
    
    // set a self JMX connection
    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    // set the object name(s) you are willing to query, here a CAMEL JMX object
    ObjectName objn = new ObjectName("org.apache.camel:context=*,type=routes,name=\"route*\"");
    Set objectInstanceNames = mBeanServer.queryNames(objn, null);
    for (ObjectName on : objectInstanceNames) {
        // query a number of attributes at once
        AttributeList attrs = mBeanServer.getAttributes(on, new String[] {"ExchangesCompleted","ExchangesFailed"});
        // process attribute values (beware of nulls...)
        // ... attrs.get(0) ... attrs.get(1) ...
    }
    

提交回复
热议问题