JMX Notifications


This tutorial will show how beans can send JMX notifications, and how they communicate with the JConsole and other JMX listeners.

The MXBean interface

Let’s start defining a simple MXBean interface to help us explain how JMX notifications work.

An MBean sending notifications

An MBean that sends notifications needs to implement the  interface.  A convenience class is provided containing an implementation of the notification dispatch model.

This tutorial explains how to use NotificationBroadcasterSupport.

Our MBean below subclasses from  NotificationBroadcasterSupport, although we could have also used composition.  Notifications are instances of the  class, and are broadcasted using the sendNotification  method of NotificationBroadcasterSupport .  Each notification has a notification type, sequence number, and message.

This example MBean broadcasts a notification each time an item has been added to the resource.  The notification type is always “example.resource.addItem” except when the item name added is “Secret”.  For that item, it will use the notification type “example.resource.addSecretItem”.

Receiving MBean notifications

To receive notifications, a class must implement the  interface, and get registered with the JMX Agent as a listener.

Listeners can choose what notifications they want to receive using the NotificationFilter  interface.  A convenience implementation is provided by the NotificationFilterSupport  class.

Below is the implementation of our example ResourceListener.  An instance of our ResourceListener listens to only one notification type with the use of filters.

Registering the MBean and Listeners

Finally, the code below registers the MBean with the JMX Agent, and sets up the listeners and listener filters.

Two ResourceListeners  are created listening to different notification types: “example.resource.addItem” and “example.resource.addSecretItem”.

Running the Example

First start up MXBeanMain and the JConsole.

As the MBean has been registered as a NotificationEmitter  bean, you will see a “Notifications” section in the JConsole.

Next, make the JConsole another listener of the MXBean by pressing the “Subscribe” button.

Subscribing to an MBean with the JConsole
Subscribing to an MBean with the JConsole

Next, let’s add some items to the MXBean.  By using the addItem operation, add “Item 1”, “Item 2”, “Secret” and “Item 3”.

JConsole receiving notifications from MBean
JConsole receiving notifications from MBean

You will see the Notifications section highlighted in bold and informing there are 4 new notifications.

MBean notifications in the JConsole
MBean notifications in the JConsole

Finally, you can confirm the filter worked by looking at the Console output.  The “Secret” item was picked up by the “example.resource.addSecretItem” listener only.

MBean notification filters
MBean notification filters


The following two tabs change content below.

Eduard Manas

Eduard is a senior IT consultant with over 15 years in the financial sector. He is an experienced developer in Java, C#, Python, Wordpress, Tibco EMS/RV, Oracle, Sybase and MySQL.Outside of work, he likes spending time with family, friends, and watching football.

Latest posts by Eduard Manas (see all)

Leave a Reply