LATEST VERSION: 8.2.5 - CHANGELOG
Pivotal GemFire® v8.2

System Alerts JMX Example

System Alerts JMX Example

Demonstrates using a JMX client to receive notifications from the JMX Manager that is connected to a GemFire cluster.

This example demonstrates a JMX client that connects to a JMX manager and listens for and filters notifications. The client class uses only standard JMX classes and does not require GemFire classes.

The SystemAlertManager class creates a GemFire cache that also configures and starts a JMX manager on the node:
Cache cache = new CacheFactory()
      .set("name", "SystemAlertManager")
      .set("jmx-manager", "true")
      .set("jmx-manager-start", "true")
      .set("jmx-manager-port", port)
      .set("http-service-port", "0") // No need to start an HTTP server for this case
      .create();
The SystemAlertManger class then logs a notification:
 cache.getLogger().severe("SystemAlertManager: A simple Alert.");
The SystemAlertClient class registers a notification listener that listens for notifications from the SystemAlertManager class:
 private static void addNotificationListener(MBeanServerConnection mbsc) {
    try {
      ObjectName distributedSystemObjectName = new ObjectName(DISTRIBUTED_SYSTEM_OBJECT_NAME);

      NotificationFilter notificationFilter = new NotificationFilter() {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isNotificationEnabled(Notification notification) {
          return notification.getType().equals(GEMFIRE_SYSTEM_ALERT_NOTIFICATION_TYPE);
        }
      };

      NotificationListener listener = new NotificationListener() {

        @Override
        public void handleNotification(Notification notification, Object handback) {
          System.out.println("Handling JMX Notification...");
          
          @SuppressWarnings("unchecked")
          Map<String, String> userData = (Map<String, String>) notification.getUserData();
          
          //JMXNotificationUserData Section of Notification
          String alertLevel = userData.get(ALERT_LEVEL);
          writeToStdout(ALERT_LEVEL + ": " + alertLevel);
          
          String member = userData.get(MEMBER);
          writeToStdout(MEMBER + ": " + member);
          
          String thread = userData.get(THREAD);          
          writeToStdout(THREAD + ": " + thread);
          
          String message = (String)notification.getMessage();
          writeToStdout("Notification Message: " + message);
          
          String source = (String)notification.getSource();
          writeToStdout("Notification Source: " + source);
          
          long alertTime = notification.getTimeStamp();
          writeToStdout("Notification Time: " + alertTime);
        }
      };

      mbsc.addNotificationListener(distributedSystemObjectName, listener, notificationFilter, null);

    } catch (MalformedObjectNameException e1) {
      e1.printStackTrace();
    } catch (NullPointerException e1) {
      e1.printStackTrace();
    } catch (InstanceNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

Running the Example

The SystemAlertManager class creates a GemFire cache and logs a notification. The SystemAlertClient class listens for and displays the notifications.

Note: To run this example, you must have terminal sessions configured for the QuickStart examples, as described in Setting Up Your Environment for Running the Examples.
  1. In one session, start the GemFire manager node:

    $ java -cp "$GEMFIRE/SampleCode/quickstart/classes:$GEMFIRE/lib/server-dependencies.jar" quickstart.SystemAlertManager
    

    The manager application starts, creates a cache, and sends a notification.

  2. When the output tells you to do so, start the client application:
    $ java -cp "$GEMFIRE/SampleCode/quickstart/classes:$GEMFIRE/lib/server-dependencies.jar" quickstart.SystemAlertClient
    
  3. After starting the client application, press Enter in the manager session.

    The manager node logs the notification and the client receives the notification and displays its message. in the client session.

Example Source Files

Table 1. Java program files, located in SampleCode/quickstart/quickstart
SystemAlertManager.java The SystemAlertManager class creates a GemFire cache that also configures and starts a JMX manager on the node.
SystemAlertClient.java The SystemAlertClient class registers a notification listener that listens for notifications from the SystemAlertManager class.

Related Javadocs

See also:
  • com.gemstone.gemfire.cache.CacheFactory
  • com.gemstone.gemfire.cache.Cache