Search code examples
javaeclipsejmxjconsole

Retrieve the information of a registered JMX mbean


I am using Eclipse to write two simple Java Programs. The first program creates a simple mbean and register it in an Mbean server. The second program retrieves the information of the registered mbean. To register and retrieve the mbean I used the code below to create the server for both programs:

MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

When I run my second program it doesn't list the created Mbean and the two lines below return false.

ObjectName mbeanName = new ObjectName("HelloAgent:name=helloWorld1");
mbs.isRegistered(mbeanName);

However, I can see it and access it by using JConsole. Have I missed something? Thank you! jconsole snapshot1 jconsole snapshot 1 jconsole snapshot 2 jconsole snapshot 2


Solution

  • As you mentioned that you are running two different Java Programs, which means they both have different MBean Server and are running in different JVM.

    mbs.isRegistered(mbeanName);
    

    The above method will check whether the MBean is registered with the MBean server of the current Java process or not. It cannot query the MBean server of another Java program.

    To access the MBean from another Java program, you need to create a client which will query your first Java program(containing the MBean that you want to manage) using RMI.

    You may find this tutorial helpful to actually understand how JMX monitoring using MBenas work.