Using notifications

In order to receive notifications, a user (at the Agent layer) implements the JMX NotificationListener interface and then invokes the addNotificationListener on the MBeanServer instance:
public void addNotificationListener( ObjectName objName, 
                                     NotificationListener listener,
                                     NotificationFilter filter,
                                     Object handback);
where
  • objectName represents the MBean from which notifications are to be received
  • listener is the user's instance of NotificationListener
  • filter is an optional filter used if only a subset of possible notifications is required (may be null)
  • handback is an object which can be used to hold private data that the handler of the received notification wants to access (may be null)
    Note: There is an alternative addNotificationListener() method on the MBeanServer which passes the ObjectName for the listener rather than the actual NotificationListener instance. This can be used if the NotificationListener instance is itself a registered MBean.

If you have any resources with aliases, and you add listeners for notifications from both a resource and its aliases, you will receive multiple identical notifications. It is a good idea to ensure that object names passed as parameters to the addNotificationListener() method do not contain the property key-value pair "type=alias".

Having called this API, the user's listener will now be added to the broadcaster's table of listeners.

In order to handle received notifications, the user also has to implement the following method:
public void handleNotification ( Notification notification, 
                                 Object handback);
where:
  • notification is the Notification instance sent by the NotificationBroadcaster object
  • handback is an object which can be used to hold private data which the handler of the received notification wishes to access (may be null)
This method is where the received notifications are processed. The Notification class provides several useful methods which may be used to extract information about the notification:
public String getType();     // returns the notification type
public Object getSource();   // returns the source of the notification
public long   getSequence(); // returns the sequence number of
                             //  the notification [1]
public String getMessage();  // returns a text message associated with 
                             //  the notification
public Object getUserData(); // returns the handback object
Note:
  1. The sequence number provides information on the occurrence of the notification but is not set in this MQe JMX implementation so will always have a value of 0.

The following example shows how the agent could set up listeners for certain MQe MBeans. In this example, the user is only interested in receiving notifications from MBeans representing application queues belonging to the local queue manager TestQueueManager:

 /* find all the mbeans and set up listeners for them */ 
ObjectName scope = new 
ObjectName("com.ibm.MQe_TestQueueManager_ApplicationQueues:*");  
Set results = mbeanServer.queryNames(scope,null); 
Iterator iter = results.iterator();
while(iter.hasNext()) { 
   /* for each bean, check that it is not an alias MBean –
    * these beans have type=alias in the ObjectName        */ 
   ObjectName objName = (ObjectName)iter.next();
   String type = objName.getKeyProperty("type");

   if(type == null || !type.equals("alias")) {   
      /*  add a listener */   
      mbeanServer.addNotificationListener(objName,this,null,null);
      }
 }

Terms of use | WebSphere software

(c) Copyright IBM Corporation 2004, 2005. All rights reserved.