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: 這個主題參照一或多個應用程式伺服器日誌檔。 此外,在分散式和 IBM® i 系統上,另外也建議您可以配置伺服器來使用「高效能可延伸記載 (HPEL)」日誌和追蹤基礎架構,而不使用 SystemOut.logSystemErr.log, trace.logactivity.log 檔案。HPEL 與原生 z/OS® 記載機能也可以一起使用。如果您使用 HPEL,則可以從伺服器設定檔 bin 目錄,利用 LogViewer 指令行工具來存取您所有的日誌和追蹤資訊。請參閱有關利用 HPEL 疑難排解應用程式的資訊,以取得更多使用 HPEL 的相關資訊。
避免困難 避免困難: Be careful when adding listeners to servers with high logging volume as JMX notifications can slow down your server. gotcha

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.

指出主題類型的圖示 作業主題



時間戳記圖示 前次更新: July 9, 2016 11:17
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=ttrb_HPELNotification
檔名:ttrb_HPELNotification.html