Java Reference
In-Depth Information
NotificationFilter filter,
Object handback )
throws IllegalArgumentException;
public MBeanNotificationInfo[] getNotificationInfo();
public void removeNotificationListener(
NotificationListener listener )
throws ListenerNotFoundException;
}
MBeans implementing this interface provide other objects with a mechanism to
register for notifications by using the
addNotificationListener()
method. This
method accepts a
NotificationListener
object, a
NotificationFilter
object,
and a handback object as parameters.
The
NotificationListener
parameter is an object that implements the
Noti-
ficationListener
interface, which specifies a
handleNotification()
method.
This method will be invoked as a callback when a notification needs to be deliv-
ered to a listener.
The
NotificationFilter
parameter is an optional argument that will allow
the MBean to filter which notifications to send to the listener based on the lis-
tener's preferences created in the filter. The handback argument is sent back to
the client each time a notification is delivered.
Notice the similarity between this notification registration and delivery mech-
anism and the Java event model of listener registration.
2.6.2
Changes to the HelloAgent class
In order to send notifications, you need to have something to receive them. For
this small notification example, you'll make your
HelloAgent
class act as a notifi-
cation listener. You need to modify your code in a few ways. First, the
HelloAgent
class needs to implement the
NotificationListener
interface. The
HelloAgent
will still create and register both the
HTML
adapter and the
HelloWorld
MBean.
After it has created the MBeans, it can now register with the
HelloWorld
MBean
as a notification listener interested in receiving notifications. The code changes
for the
HelloAgent
class appear in listing 2.4 in bold.
Listing 2.4
HelloAgent.java
Implement
NotificationListener
interface
B
package jmxbook.ch2;
import javax.management.*;
import com.sun.jdmk.comm.HtmlAdaptorServer;
public class HelloAgent
implements NotificationListener