Monitor application logging using JMX notifications
Java developers can create programs to monitor application server logs using JMX notifications.
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.
As a recommended alternative, we can configure the server to use the High Performance Extensible Logging (HPEL) log and trace infrastructure SystemErr.log, trace.log, and activity.log files on distributed and IBM i systems. We view HPEL log and trace information using the logViewer . See the information about using HPEL to troubleshoot applications for more information on using HPEL.
Be careful when adding listeners to servers with high logging volume as JMX notifications can slow down the server..
Tasks
- Import the required packages. You will typically need the following import statements at the beginning of our 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 we will need the following import statements.
import java.util.Iterator; import java.util.Properties; import java.util.Set; import com.ibm.websphere.ras.RasMessage;- Create a Java class that implements the NotificationListener interface.
- 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)); }- Connect to the SOAP port of the server whose JMX MBeans we want to monitor. Create 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 }- Retrieve the MBean object name for the RasLoggingService Bean. Retrieve the RasLoggingService Bean 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() ;- 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 }- Add the necessary jar to the classpath. Add the admin client jar file to the classpath to be able to compile and run your code. The admin client jar file is in the <install_root>/runtimes directory.
We have created a Java program that can listen to, and take actions as a result of log event notifications from an application server.
Related:
High Performance Extensible Logging (HPEL)