The Administration, Local Configuration, Security, and Runtime MBeans allow us to view and modify the configuration and runtime statistics of resources being managed by the MBean Server. Notifications and Monitoring go one step further they allow you to react to changes in the runtime state of the underlying managed resources. This is critical if you want to build a management application that is able to react and respond to runtime state changes of WebLogic resources and services.
The JMX specification defines a model that allows MBeans to broadcast management events called notifications. You then can create applications that listen for JMX notifications. Your applications may also filter out unwanted notifications. Each WebLogic MBean directly or indirectly extends the javax.management.NotificationBroadcaster interface, thereby allowing you to add or remove notification listeners.
20.5.1 Creating a Notification Listener
A notification listener is a handler that is triggered when the MBean it is registered with sends one or more JMX notifications. A notification listener is an instance of a class that implements the javax.management.NotificationListener interface. For remote applications, the listener object should instead implement the weblogic.management.RemoteNotificationListener interface, which merely extends the previous interface and java.rmi.Remote as well, making the JMX notifications available to external clients via RMI.
The listener interface is simple it exposes a single method, handleNotification( ), that gets called when a notification is received. Here is an example of a remote notification listener:
public class SimpleListener implements RemoteNotificationListener { public void handleNotification(Notification notification, Object hb) { System.err.println("Received Notification"); System.err.println("Source=" + notification.getSource( )); System.err.println("Message=" + notification.getMessage( )); System.err.println("Type=" + notification.getType( )); } }
20.5.2 Registering a Listener
In order to receive notifications from an MBean, you need to register a listener object with the MBean. If you already hold a reference to the MBean, you simply can invoke the addNotificationListener( ) method to register the listener object. An alternative is to use the addNotificationListener( ) method on the MBeanServer, which takes the name of the MBean and the listener object and registers the listener for you. The following code registers a listener with the JDBCConnectionPoolMBean called My Connection Pool:
SimpleListener sl = new SimpleListener( ); WebLogicObjectName oname = new WebLogicObjectName ("My Connection Pool", "JDBCConnectionPool", "myClusterDomain"); mb.getMBeanServer( ).addNotificationListener(oname, sl, null, null);
If we then change the runtime state of the JDBC connection pool (e.g., modify the initial capacity of the pool), and if the program that registered the listener object is still running, the listener will receive a notification of the change. Here is the output that is generated when the initial capacity was raised from 2 to 3 using the Administration Console:
Received Notification Source=myClusterDomain:Name=My Connection Pool,Type=JDBCConnectionPool Message=WebLogic MBean Attribute change for InitialCapacity from 2 to 3 Type=jmx.attribute.change
The MBean fires notifications whenever attributes are modified, or whenever new attributes are added or existing ones are removed from an MBean. WebLogic provides two subclasses of the standard Notification class that represent notification events that are fired when attributes are added or removed from an MBean:
weblogic.management.AttributeAddNotification
An instance of this class is fired whenever an addXXX( ) method is called on an MBean.
weblogic.management.AttributeRemoveNotification
An instance of this class is fired whenever a removeXXX( ) method is called on an MBean.
Notifications also are used by the Monitor MBeans that we explore in the next section, and by the distributed logging framework covered in Chapter 21.
Introduction
Web Applications
Managing the Web Server
Using JNDI and RMI
JDBC
Transactions
J2EE Connectors
JMS
JavaMail
Using EJBs
Using CMP and EJB QL
Packaging and Deployment
Managing Domains
Clustering
Performance, Monitoring, and Tuning
SSL
Security
XML
Web Services
JMX
Logging and Internationalization
SNMP