Java Reference
In-Depth Information
2.6 Using MBean notifications
After creating and registering your own MBean in the previous section, you
already have enough knowledge to start working with
JMX
. You have learned
how to create a Standard MBean, how to add it to a simple
JMX
agent, and how
to manage that agent by using the
HTML
adapter. However, you are still missing
a key ingredient: notifications.
JMX
notifications are Java objects used to send information from MBeans and
agents to other objects that have registered to receive them (see figure 2.9).
Objects interested in receiving events are notification listeners—they implement
the
javax.management.NotificationListener
interface.
Notifications are an important piece of
JMX
because they allow for the trans-
mission of events.
JMX
events can be anything from the changing of an MBean
attribute to the registration of a new MBean on an MBean server.
To give you a quick introduction to notifications, you'll add them to the
HelloWorld
MBean in this section. In chapter 6, we'll cover the notification
model in depth.
2.6.1
Adding notification code to the HelloWorld MBean
Fo r t h e
HelloWorld
MBean to send notifications, it needs to allow objects inter-
ested in receiving notifications to register for them.
JMX
supports two mecha-
nisms for MBeans to provide listeners to register for notifications:
■
Implement the
javax.management.NotificationBroadcaster
interface
■
Extend the
javax.management.NotificationBroadcasterSupport
class (which
in turn implements the
NotificationBroadcaster
interface)
The advantage of implementing the interface is that it frees your class from being
tied to a particular super class. The advantage of extending the broadcaster
Figure 2.9
Notification being sent
to a registered listener
from an MBean