Monitoring application logging using JMX notifications

Java developers can create programs to monitor application server logs using JMX notifications.

About this task

The most common log message listeners are written in Java, and connect to the deployment manager or an application server using SOAP. Use this topic to build a Java client that listens for log events.
Note: This topic references one or more of the application server log files. As a recommended alternative, you can configure the server to use the High Performance Extensible Logging (HPEL) log and trace infrastructure instead of using SystemOut.log , SystemErr.log, trace.log, and activity.log files on distributed and IBM® i systems. You can also use HPEL in conjunction with your native z/OS® logging facilities. If you are using HPEL, you can access all of your log and trace information using the LogViewer command-line tool from your server profile bin directory. See the information about using HPEL to troubleshoot applications for more information on using HPEL.
Avoid trouble: Be careful when adding listeners to servers with high logging volume as JMX notifications can slow down your server.

Procedure

  1. Import the necessary packages.
    You will typically need the following import statements at the beginning of your Java program:
    import javax.management.Notification;
    import javax.management.NotificationListener;
    import javax.management.ObjectName;
    import javax.management.InstanceNotFoundException;
    import javax.management.MalformedObjectNameException;
    
    import com.ibm.websphere.management.AdminClient;
    import com.ibm.websphere.management.AdminClientFactory;
    import com.ibm.websphere.management.exception.ConnectorException;
    
    Additionally, to handle the messages, and the types returned from the calls in subsequent steps you will need the following import statements.
    import java.util.Iterator;
    import java.util.Properties;
    import java.util.Set;
    import com.ibm.websphere.ras.RasMessage; 
    
  2. Create a Java class that implements the NotificationListener interface.
  3. Implement the handleNotification method.
    The following example is a sample that writes the message text to the Java console:
    public void handleNotification(Notification notification, Object handback) {
            RasMessage rasMessage = (RasMessage)notification.getUserData() ;
            System.out.println("Localized message: " + rasMessage.getLocalizedMessage(null));
    }
    
  4. Connect to the SOAP port of the server whose JMX MBeans you want to monitor.
    The following code creates a SOAP-connected AdminClient object with a specified host and a specified port:
            AdminClient adminClient = null ;
            String hostName = "someHostName";
            String soapPort = "8880";
    
            Properties connectProps = new Properties();
            connectProps.setProperty(AdminClient.CONNECTOR_TYPE, "SOAP");
            connectProps.setProperty(AdminClient.CONNECTOR_HOST, hostName);
            connectProps.setProperty(AdminClient.CONNECTOR_PORT, soapPort);
    
            try {
                adminClient = AdminClientFactory.createAdminClient(connectProps);
            } catch (ConnectorException e) {
                // error handling code
            } 
  5. Retrieve the MBean object name for the RasLoggingService MBean.
    The following code retrieves the RasLoggingService MBean object name:
            String queryString = "WebSphere:cell="+cellName+",node="+nodeName+",process="+serverName+",
    type=RasLoggingService,*" ;
            Set<ObjectName> objectMBeans = null;
            try {
                ObjectName queryName = new ObjectName(queryString);
                objectMBeans = (Set<ObjectName>)adminClient.queryNames(queryName, null);
                if (objectMBeans.size() > 1) {
                    // error handling code to deal with the case where we get more than one name returned.
                }
            } catch (MalformedObjectNameException e) {
                // error handling code
            } catch (ConnectorException e) {
                // error handling code
            }
    
            if (objectMBeans.isEmpty()) {
                // error handling code to deal with the case where the MBean is not found
            }
    
            Iterator<ObjectName> objectNames = objectMBeans.iterator() ;
            ObjectName objectName = objectNames.next() ; 
    
  6. Add the notification listener.
    This sample code adds a notification listener, waits for 60 seconds while it processes notifications, then removes the notification listener. A listener can stay connected as long as needed.
            try {
                adminClient.addNotificationListener(objectName, this, null, null);
                Thread.sleep(60 * 1000) ;
                adminClient.removeNotificationListener(objectName, this) ;
            } catch (InstanceNotFoundException e) {
                // error handling code
            } catch (ConnectorException e) {
                // error handling code
            } catch (Exception e) {
                // error handling code
            }
    
  7. Add the necessary jar to your classpath.
    Add the admin client jar file to your classpath to be able to compile and run your code. The admin client jar file is in the <install_root>/runtimes directory.

Results

You have created a Java program that can listen to, and take actions as a result of log event notifications from an application server.