Getting warnings and listening for new system messages
Use the SystemMonitorMXBean API to get a list of warning messages that are collected by a runtime server, as well as to clear the collected warnings on the server, or to set a listener to get notified whenever a warning message is collected.
Before you begin
About this task
The SystemMonitorMXBean returns only the warning messages of the runtime server to which the MBean connection is established for.
Procedure
- To connect to the MBean server programmatically, enable the connection by using the following
code:
private static MBeanServerConnection getConnection(String hostName, int port, String adminUsername, String adminPassword, String trustStorePath, String trustStorePassword) throws MalformedURLException, IOException { System.setProperty("javax.net.ssl.trustStore", trustStorePath); System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword); System.setProperty("https.protocols", "TLSv1.2"); HashMap<String, Object> environment = new HashMap<String, Object>(); environment.put("jmx.remote.protocol.provider.pkgs", "com.ibm.ws.jmx.connector.client"); environment.put(JMXConnector.CREDENTIALS, new String[] { adminUsername, adminPassword }); environment.put("com.ibm.ws.jmx.connector.client.disableURLHostnameVerification", Boolean.TRUE); String jmxURL = "service:jmx:rest://" + hostName + ":" + port + "/IBMJMXConnectorREST"; JMXServiceURL url = new JMXServiceURL(jmxURL); JMXConnector connector = JMXConnectorFactory.newJMXConnector(url, environment); connector.connect(); MBeanServerConnection mbsc = connector.getMBeanServerConnection(); return mbsc;} - To connect to the MBean server by using a JMX client:
- Add the restConnector.jar and ia-management.jar to
your client classpath.
For example:
set CLASSPATH=%CLASSPATH%;C:\IBM\ODMInsights890\runtime\wlp\clients\restConnector.jar;C:\IBM\ODMInsights890\runtime\ia\dev\api\ibm\ia-management.jarWhere the location of the restConnector.jar file is <InstallDir>/runtime/wlp/clients/restConnector.jar and the location of the ia-management.jar file is <InstallDir>/runtime/ia/dev/api/ibm/ia-management.jar.
- Connect to the SystemMonitorMXBean and prints the warning message of the
runtime server. The following example shows how to get a list of warnings that fall within a specific time period.
ObjectName objectName = new ObjectName(SystemMonitorMXBean.MXBEAN_OBJECT_NAME); Object list = mbsc.invoke(objectName, "getWarnings", new Object[]{start.toEpochSecond(), end.toEpochSecond()}, new String[]{Long.class.getName(), Long.class.getName()}); - Add a notification listener on the client to get notified when a warning message is captured.
Create a listener and add it to the MBean server.
private static class MyListener implements NotificationListener { @Override public void handleNotification(Notification notification, Object handback) { LogEntry entry = (LogEntry) notification.getUserData(); if (entry != null) { System.out.println("added: " + entry.getType() + ": " + entry.getLocalizedMessage()); } } } MyListener listener = new MyListener(); ObjectName objectName = new ObjectName(SystemMonitorMXBean.MXBEAN_OBJECT_NAME); mbsc.addNotificationListener(objectName, listener, null, null); - Reset the system monitor logging cache by starting the
SystemMonitorMXBean.
ObjectName objectName = new ObjectName(SystemMonitorMXBean.MXBEAN_OBJECT_NAME); mbsc.invoke(objectName, “reset”, null, null);
- Add the restConnector.jar and ia-management.jar to
your client classpath.
Parent topic: Monitoring server activity