Using Notifications and Monitor MBeans 7-7
■
getType
■
getUserData See Notification in the J2SE 6.0 API Specification at
http:download.oracle.comjavase6docsapijavaxmanagement Notification.html
. Most notification types provide additional methods for retrieving data that is
specific to the notification. For example, javax.management.AttributeChangeNotification
provides getNewValue
and getOldValue, which you can use to determine how the attribute value has changed.
Example 7–1 is a simple listener that uses AttributeChangeNotification
methods to retrieve the name of an attribute with a changed value, and the old and new values.
Example 7–1 Notification Listener
import javax.management.Notification; import javax.management.NotificationFilter;
import javax.management.NotificationListener; import javax.management.AttributeChangeNotification;
public class MyListener implements NotificationListener { public void handleNotificationNotification notification, Object obj {
ifnotification instanceof AttributeChangeNotification { AttributeChangeNotification attributeChange =
AttributeChangeNotification notification; System.out.printlnThis notification is an
AttributeChangeNotification; System.out.printlnObserved Attribute: +
attributeChange.getAttributeName ; System.out.printlnOld Value: + attributeChange.getOldValue ;
System.out.printlnNew Value: + attributeChange.getNewValue ; }
} }
7.4.1.1 Listening from a Remote JVM
As of JMX 1.2, there are no special requirements for programming a listener that runs in a different JVM from the MBean to which it is listening.
Once you establish a connection to the remote JMX agent using javax.management.MBeanServerConnection
, JMX takes care of sharing data between the JVMs. See
Section 7.4.3, Registering a Notification Listener and Filter, for
instructions on establishing a connection from a remote JVM.
7.4.1.2 Best Practices: Creating a Notification Listener
Consider the following recommendations while creating your NotificationListener
class:
■
Unless you use a notification filter, your listener receives all notifications of all notification types from the MBeans with which it is registered.
Instead of using one listener for all possible notifications that an MBean emits, the best practice is to use a combination of filters and listeners. While having multiple
7-8 Developing Custom Management Utilities With JMX for Oracle WebLogic Server
listeners adds to the amount of time for initializing the JVM, the trade-off is ease of code maintenance.
■
If your WebLogic Server environment contains multiple instances of MBean types that you want to monitor, you can create one notification listener and then create
as many registration classes as MBean instances that you want to monitor.
For example, if your WebLogic Server domain contains three JDBC data sources, you can create one listener class that listens for
AttributeChangeNotifications . Then, you create three registration classes.
Each registration class registers the listener with a specific instance of JDBCDataSourceRuntimeMBean
.
■
While the handleNotification method signature includes an argument for a handback object, your listener does not need to retrieve data from or otherwise
manipulate the handback object. It is an opaque object that helps the listener to associate information regarding the MBean emitter.
■
Your implementation of the handleNotification method should return as soon as possible to avoid blocking its notification broadcaster.
■
If you invoke a method from a specialized notification type, wrap the method calls in an if statement to prevent your listener from invoking the method on
notification objects of all types.
7.4.2 Configuring a Notification Filter