Java Reference
In-Depth Information
The
NotificationListener
interface contains a single method:
handleNotifica-
tion()
. It takes two arguments: an instance of
Notification
and an instance of
Object
. Notification broadcasters invoke this method when they are ready to
deliver a notification to the listener. The instance of
Notification
is the notifica-
tion being sent, and the
Object
instance is the handback object registered by the
listener. Recall that when a listener registers to receive notifications using the
add-
NotificationListener()
method, it passes in an
Object
instance named
handback
.
When implementing the
handleNotification()
method from the
NotificationListener
interface, you should have it return as soon as
possible. Notification broadcasters invoke this method synchronously,
meaning it will block until the method invocation is complete. Depend-
ing on the implementation and purpose of an MBean, this behavior
could present a problem. You can avoid it by implementing the method
that sends notifications (discussed later), which invoked the
handleNo-
tification()
method asynchronously.
NOTE
The only component remaining is the notification filter. Because MBeans can
emit an infinite number of notification types, listeners can use a filter to ensure
they receive only the specific notification types in which they are interested.
Remember that listeners register once to receive all notifications from an MBean,
and therefore need to be able to filter out notifications that are unwanted.
6.2.5
Filtering notifications
As stated earlier, notification filters give notification listeners a way to sort
through a potential barrage of notifications to receive only those notifications
that are important to them. To be more accurate, notification broadcasters use a
registered listener's filter to determine whether to send a notification to a lis-
tener. Recall from section 6.2.1 that one of the arguments passed to the
addNoti-
ficationListener()
method is an implementation of the
javax.management.
NotificationFilter
interface.
The
NotificationFilter
interface declares only one method:
isNotification-
Enabled()
. This method accepts a
Notification
object that is about to be sent and
returns a
boolean
value indicating whether the listener associated with this filter
wants to receive the notification. Implementers are free to choose any way of fil-
tering through notifications, but probably the best way to filter notifications is by
their type.
JMX
contains a class called
NotificationFilterSupport
that does just