Simple JMX MBean Examples | Dynamic MBean

In this article we will create a dynamic JMX MBean, then register & verify it. In earlier article we created simple standard JMX MBean to publish counter. We will improve that to make it dynamic.

Simple JMX MBean Example | Simple standard MBean

Example:

  • Create a simple infinite running main program which creates n number of threads (n=5 for our test).
  • Each thread has a counter which is periodically incremented.
  • Register a dynamic JMX MBean which makes all thread counters available as attributes of MBean to JMX console.

Earlier article example had a predefined class with exact 1 attribute which was exposed as JMX MBean attributes. In this example, we have non-defined number of therads i.e. n threads. So we have to create n attributes at runtime depending on number of threads.

Create JMX Mbean class

To achieve this, we have to implement a special interface provided by java i.e. javax.management.DynamicMBean. In this implementation, we can override certain methods which define structure of our MBean & also provide dynamic values/operations. For ex: we can override getMBeanInfo() which provides list of attributes for this MBean which we can dynamically populate.

Here is the actual code. This has counter per thread which are incremented by different threads. For each thread there will be one attribute added to MBean which will show value of counter for that thread.




Register MBean & test

Verification

Now run this program through command line or through eclipse. It will register dynamic JMX MBean & keep program running so that we can verify.




Then go to JDK bin directory in your machine & run jconsole.exe. For ex: “C:\Program Files\Java\jdk-11.0.0\bin\jconsole.exe”. This will open JCONSOLE UI. Here select our application & then it will show MBeans as shown below.

As you can see it created 5 attributes for 5 thread & all of them are showing counts for that thread.




Leave a Reply

Your email address will not be published.