Simple JMX MBean Example | Simple standard MBean with JMX MBean naming convention examples

In this article we will create simple Java Management Extensions i.e. JMX MBean & register them so that it can be viewed through JMX Console.

Example:

  • Create a simple infinite running main program which keep incrementing counter periodically.
  • Register JMX MBean which makes counter value available to JMX console.

Create MBean class

A standard MBean class MUST follow certain rules as given below.

  • There must be a public interface with name <some-name>MBean ex: ApplicationInfoMBean
  • This interface must be implemented by concrete class with <some-name> ex: ApplicationInfo




Here is our MBean interface & concrete class class.

Possible errors/mistakes

Failing to follow naming convention will cause NotCompliantMBeanException. For ex: If we name interface as ApplicationInfoBean (‘M’ missing) or if we name concrete class as AppInfo instead of ApplicationInfo then you might see this error.

If you don’t keep interface public (like inner interface or interface in same file etc.) then you might see this error.



Register MBean

Verification

Now run this program through command line or through eclipse. It will register 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.





Naming syntax examples

<domain>:<key-1>=<value-1>,<key-1>=<value-1>,…

This is the naming syntax for JMX MBeans which helps in properly categorizing MBeans & also make them searchable.  For detailed rules & constraints like what is allowed & not allowed etc. refer Oracle documentation.

Lets says, we want to utilize above counter MBean program to count animals & trees of different kinds & want to categorize them properly then this might be a way to do it through MBean naming convention.

We will categorize counters as –

  • type=animalCounter
    • category=waterAnimals
      • subcategory=seaAnimals
      • subcategory=lakeAnimals
    • category=groundAnimals
    • category=flyingAnimals
  • type=treeCounter
    • category=fruitTrees
    • category=flowerTrees
      • subcategory=plants

Here is the code with actual naming convention. Notice names of all MBeans & the key value pairs in the names.



When you run this program & verify using JCONSOLE UI, here is how it will look. All the counter JMX MBeans are properly categorized.

Further reading

Simple JMX MBean Examples | Dynamic MBean



Leave a Reply

Your email address will not be published.