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.
Be careful when adding listeners to servers with high logging volume as JMX notifications can slow down the server. gotcha
- Import the required packages.
Add the following import statements at the beginning of the 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;To handle the messages, and the types returned from the calls in subsequent steps add 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.
Write 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 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 MBean.
Retrieve 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() ;
- 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.
Results
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 concepts:
HPEL