Java tutorial How to use JMX to connect Java programs running on the local JVM-Tanzhou Java

Source: Internet
Author: User
Tags jconsole

Issues to be addressed:

Want to develop a JMX client to configure a local Java program. The specific requirements are as follows:

Please do not recommend the use of jconsole! Jconsole is inappropriate because jconsole is a generic JMX client that has a negative impact on the performance of the main program.

There is an example on the Oracle website that uses Rmiconnector and "host: Port number" as a parameter, but I do not know where to set the port number of JMX?

Jconsole can select the PID to connect to the Java process. But in the JMX API, I can't find any way to use PID as a parameter.


We use some programs like the following to connect to a JMX server. However, you need to specify the following parameters when you run the server:

In order to bind a specific address, you need to add the following VM parameters:


In this way, you can connect to your server like the following JMX client code:

String host = "localhost"; or some a.b.c.d

int port = 1234;

String url = "service:jmx:rmi:///jndi/rmi://" + Host + ":" + port + "/jmxrmi";

Jmxserviceurl serviceurl = new Jmxserviceurl (URL);

Jmxconnector jmxconnector = jmxconnectorfactory.connect (serviceurl, NULL);

try {

Mbeanserverconnection mbeanconn = Jmxconnector.getmbeanserverconnection ();

Now query to get the beans or whatever

set<objectname> beanset = mbeanconn.querynames (null, NULL);


} finally {

Jmxconnector.close ();


We can also not use the VM parameters, the code itself can be programmed to publish at the specified port number. But I think this is more complicated than your needs.

If you want to connect according to the PID, as far as I know, you need to use more than Java version 6. I have not used the following code, but it will work as expected:

list<virtualmachinedescriptor> VMS = Virtualmachine.list ();

for (Virtualmachinedescriptor Desc:vms) {

Virtualmachine VMS;

try {

VM = Virtualmachine.attach (DESC);

} catch (Attachnotsupportedexception e) {



Properties props = Vm.getagentproperties ();

String connectoraddress =

Props.getproperty ("");

if (connectoraddress = = null) {



Jmxserviceurl url = new Jmxserviceurl (connectoraddress);

Jmxconnector connector = jmxconnectorfactory.connect (URL);

try {

Mbeanserverconnection mbeanconn = Connector.getmbeanserverconnection ();

set<objectname> beanset = mbeanconn.querynames (null, NULL);


} finally {

Jmxconnector.close ();



I have released a new SIMPLEJMX package that can help to start a JMX service very simply and send beans to a remote client.

Create a new server and listen on port 8000

Jmxserver jmxserver = new Jmxserver (8000);

Start the server

Jmxserver.start ();

Register the Lookupcache object defined below

Jmxserver.register (Lookupcache);

Jmxserver.register (Someotherobject);

Stop Service

Jmxserver.stop ();

The package does have a client interface, but no one currently has a mechanism to find the process through the PID, only the host/port combination is supported.

PS: If there is any problem, please put it directly in group 457036818.

This article is from the Java Learning Video tutorial blog, so be sure to keep this source

Java tutorial How to use JMX to connect Java programs running on the local JVM-Tanzhou Java

Related Article

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: 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.