Sun GlassFish Message Queue 4.4 Developer's Guide for JMX Clients

Receiving MBean Notifications

To receive notifications from an MBean, you must register a notification listener with the MBean server. This is an object implementing the JMX interface NotificationListener, which consists of the single method handleNotification. In registering the listener with the MBean server (using the MBeanServerConnection method addNotificationListener), you supply the object name of the MBean from which you wish to receive notifications, along with a notification filter specifying which types of notification you wish to receive. (You can also provide an optional handback object to be passed to your listener whenever it is invoked, and which you can use for any purpose convenient to your application.) The MBean server will then call your listener's handleNotification method whenever the designated MBean broadcasts a notification satisfying the filter you specified.

The notification listener's handleNotification method receives two parameters: a notification object (belonging to the JMX class Notification) describing the notification being raised, along with the handback object, if any, that you supplied when you registered the listener. The notification object provides methods for retrieving various pieces of information about the notification, such as its type, the MBean raising it, its time stamp, and an MBean-dependent user data object and message string further describing the notification. The notifications raised by Message Queue MBeans belong to Message Queue–specific subclasses of Notification, such as BrokerNotification, ServiceNotification, and DestinationNotification, which add further information retrieval methods specific to each particular type of notification; see the relevant sections of Chapter 3, Message Queue MBean Reference for details.

Example 2–12 shows a notification listener for responding to Message Queue service notifications, issued by a service manager monitor MBean. On receiving a notification belonging to the Message Queue class ServiceNotification, the listener simply prints an informational message containing the notification's type and the name of the connection service affected.

Example 2–12 Notification Listener


public class  ServiceNotificationListener implements NotificationListener
    public void  handleNotification (Notification  notification,
                                     Object        handback)
        if ( notification instanceOf ServiceNotification )
          { ServiceNotification  n = (ServiceNotification)notification;
          { System.err.println( "Wrong type of notification for listener" );
        System.out.println( "\nReceived service notification: " );
        System.out.println( "\tNotification type: " + n.getType() );
        System.out.println( "\tService name: " + n.getServiceName() );
        System.out.println( "" );

Example 2–13 shows how to register the notification listener from Example 2–12, using the MBeanServerConnection method addNotificationListener. The notification filter is an object of the standard JMX class NotificationFilterSupport; the calls to this object's enableType method specify that the listener should be invoked whenever a connection service is paused or resumed. The listener itself is an instance of class ServiceNotificationListener, as defined in Example 2–12.

Example 2–13 Registering a Notification Listener

import com.sun.messaging.AdminConnectionFactory;

public class  NotificationService
    public static void  main (String[]  args)
          { //  Create admin connection factory
                AdminConnectionFactory  acf = new AdminConnectionFactory();
            //  Get JMX connector, supplying user name and password
                JMXConnector  jmxc = acf.createConnection("AliBaba", "sesame");
            //  Get MBean server connection
                MBeanServerConnection  mbsc = jmxc.getMBeanServerConnection();
            //  Create object name for service manager monitor MBean
                ObjectName  svcMgrMonitorName
                    = new ObjectName( MQObjectName.SERVICE_MANAGER_MONITOR_MBEAN_NAME );
            //  Create notification filter
                NotificationFilterSupport  myFilter = new NotificationFilterSupport();
            //  Create notification listener
                ServiceNotificationListener  myListener = new ServiceNotificationListener();
                mbsc.addNotificationListener(svcMgrMonitorName, myListener, myFilter, null);
        catch (Exception  e)
          { System.out.println( "Exception occurred: " + e.toString() );
          { if ( jmxc != null )
              { try
                  { jmxc.close();
                catch (IOException ioe)
                  { System.out.println( "I/O exception occurred: " + ioe.toString() );