bea.com | products | dev2dev | support | askBEA
 Download Docs   Site Map   Glossary 
Search

Programming WebLogic Management Services with JMX

 Previous Next Contents Index View as PDF  

Using WebLogic Server MBean Notifications

To report changes in configuration and runtime information, all WebLogic Server MBeans emit JMX notifications. A notification is a JMX object that describes a state change or some other specific condition that has occurred in an underlying resource.

You can create Java classes called listeners that listen for these notifications. For example, your application can include a listener that receives notifications when applications are deployed, undeployed, or redeployed.

This topic includes the following sections:

 


WebLogic Server Notification Types

WebLogic Server MBeans implement the javax.management.NotificationBroadcaster interface to emit different types of notification objects depending on the type of event that occurs:

In addition, when MBeans have been registered or unregistered, the WebLogic Server JMX services emit notifications of type javax.management.MBeanServerNotification.

For more information about the javax.management notification types, refer to the JMX 1.0 API documentation, which you can download from http://jcp.org/aboutJava/communityprocess/final/jsr003/index.html. The archive that you download includes the API documentation..

For more information about the weblogic.management notification types, refer to the Javadoc for AttributeAddNotification and AttributeRemoveNotification.

WebLogic Server Log Notifications

When a WebLogic Server resource generates a log message, its MBeans emit a notification of type weblogic.management.WebLogicLogNotification. You can use the WebLogicLogNotification API to extract parts of the log message, including the transaction ID, user ID, and version number associated with the message.

For more information about log notifications, refer to the Using WebLogic Logging Services guide.

 


Listening for Notifications: Main Steps

To listen for the notifications that MBeans emit, do the following:

  1. Create a listener class in your application.
  2. Register the class with the MBeans whose notifications you want to receive.
  3. Optionally implement and register a NotificationFilter class, which provides additional control over which notifications the listener receives.

Figure 4-1 shows a basic system in which a JSP contains a NotificationListener that is registered with an MBean's implementation of the NotificationBroadcaster interface.

Figure 4-1 Monitoring Notifications from a JSP


 

This section contains the following subsections:

For a complete explanation of JMX notifications and how they work, download the JMX 1.0 specification from http://jcp.org/aboutJava/communityprocess/final/jsr003/index.html.

Creating a Notification Listener

To create a notification listener for a client that runs within the same JVM as WebLogic Server, create a class that implements javax.management.NotificationListener. Your implementation must include the NotificationListener.handleNotification() method.

For more information on NotificationListener, refer to the javax.management.Notification Javadoc in the JMX 1.0 API documentation, which you can download from http://jcp.org/aboutJava/communityprocess/final/jsr003/index.html. The archive that you download includes the API documentation.

To create a notification listener for a client that runs in a JVM that is separate from WebLogic Server, create a class that implements weblogic.management.RemoteNotificationListener. RemoteNotificationListener extends javax.management.NotificationListener and java.rmi.Remote, making MBean notifications available to external clients via RMI. Your implementation must include the RemoteNotificationListener.handleNotification() method. For more information, refer to RemoteNotificationListener Javadoc.

After you implement RemoteNotificationListener, you register your listener with MBeans whose notifications you want to receive. (See Figure 4-2.)

Figure 4-2 Monitoring Notifications from a Separate JVM


 

The following example creates a remote listener that prints output when NotificationBroadcaster broadcasts a WebLogicLogNotification message that indicates an application has been deployed or undeployed.

Listing 4-1 Notification Listener

import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.Notification.*;
import weblogic.management.RemoteNotificationListener;
import weblogic.management.logging.WebLogicLogNotification;
public class myListener implements
RemoteNotificationListener {
public void handleNotification(Notification notification, Object obj) {
        WebLogicLogNotification wln = (WebLogicLogNotification)notification;
        /*
* These are all the attributes you can get on a
* WebLogicLogNotification
*/
        System.out.println("\n\nmessage id = " + wln.getMessageId());
System.out.println("server name = " + wln.getServername());
System.out.println("machine name = " + wln.getMachineName());
System.out.println("severity = " + wln.getSeverity());
System.out.println("type = " + wln.getType());
System.out.println("timestamp = " + wln.getTimeStamp());
System.out.println("message = " + wln.getMessage());
System.out.println("thread id = " + wln.getThreadId());
System.out.println("user id = " + wln.getUserId());
System.out.println("transaction id = " + wln.getTransactionId());
System.out.println("version = " + wln.getVersion());
      int messageId = wln.getMessageId();
        /* These are the messageIDs of the messages broadcast when an
* application is deployed/undeployed/redeployed
* 160004 is for undeployment
* 160003 is for deployment
*/
        if(messageId == 160004)
System.out.println(\n\nwln.getMessage());
else if (messageId == 160003)
System.out.println(wln.getMessage());
else;
    }
}

Registering a Notification Listener

Because all WebLogic Server MBeans implement the javax.management.NotificationBroadcaster interface, you can register a NotificationListener with any MBean.

Registering a NotificationListener can be accomplished by calling the MBean's addNotificationListener() method. However, in most cases it is preferable to use the addNotificationListener() method of the MBeanServer interface, which saves the trouble of looking up a particular MBean simply for registration purposes.

The following example uses MBeanServer.addNotificationListener() to register the listener from Listing 4-1 with the LogBroadcasterRuntimeMBean.

Listing 4-2 Registering a Listener

import java.util.Set;
import java.util.Iterator;
import java.rmi.RemoteException;
import javax.naming.*;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import javax.management.ObjectName;
import weblogic.management.WebLogicMBean;
import weblogic.management.configuration.ServerMBean;
import weblogic.management.runtime.LogBroadcasterRuntimeMBean;
import weblogic.management.WebLogicObjectName;
import javax.management.*;
import javax.management.Notification;
import weblogic.management.RemoteMBeanServer;
/* This class is to be registered as a startup class with the server that
* receives the Log Notifications
*/
public class logger {
  public static void main(String[] args) {
   MBeanHome home = null;
LogBroadcasterRuntimeMBean logBroadcaster = null;
RemoteMBeanServer rmbs = null;
   //domain variables
String serverName = "MyServer";
String domainName = "myDomain";
   try {
Context ctx = new InitialContext();
   //Get a local MBeanHome
home = (MBeanHome) ctx.lookup("weblogic.management.home." + serverName);
} catch (Exception e) {
System.out.println("Exception caught: " + e);
}
   //Use MBeanHome to get MBeanServer
try {
rmbs = home.getMBeanServer();
} catch(Exception e) {
System.out.println("Caught exception: " + e);
}
   try {
/* The LogBroadcasterRuntimeMBean is only responsible for emitting
* notifications for log messages. All notifications generated are
* of the type WebLogicLogNotification. There is only one
* LogBroadcasterRuntimeMBean per server.
*/
     WebLogicObjectName oname = new WebLogicObjectName(domainName +      "
:Name=TheLogBroadcaster,Type=LogBroadcasterRuntime,Location="
+ serverName);
myListener listener = new myListener();
     rmbs.addNotificationListener(oname, listener, null, null);
System.out.println("\n[myListener]: Listener registered
for the LogBroadcasterRuntimeMBean ...");
} catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}

 

Back to Top Previous Next