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

后端 未结 5 1648
孤独总比滥情好
孤独总比滥情好 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:01

    List vm = new ArrayList();
            jvmList = new JVMListManager();
    
            vm = jvmList.listActiveVM();
    
            for (VirtualMachineDescriptor vmD : vm) 
            {
                try
                {
    
                //importFrom is taking a process ID and returning a service url in a String Format
                String ServiceUrl = ConnectorAddressLink.importFrom(Integer.parseInt(vmD.id().trim()));
                JMXServiceURL jmxServiceUrl = new JMXServiceURL(ServiceUrl);
    
                jmxConnector = JMXConnectorFactory.connect(jmxServiceUrl, null);
                con = jmxConnector.getMBeanServerConnection();
                CompilationMXBean compMXBean = ManagementFactory.newPlatformMXBeanProxy(con
                       , ManagementFactory.COMPILATION_MXBEAN_NAME
                       , CompilationMXBean.class);
                }catch(Exception e)
                {
                //Do Something  
                }
            }
    
    
    protected List listActiveVM() {
        List vm = VirtualMachine.list();
    
        return vm;
    }
    

    This requires you to use the jmxremote argument at JVM startup for the process you are trying to read. TO be able to do it without having to pass a jmxremote argument at startup. You will have to use the attach api(only applicable for Programs using Java 6 and higher.

提交回复
热议问题