Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4981829

CounterMonitor misses events in difference mode

XMLWordPrintable

    • b38
    • generic
    • generic
    • Verified



      Name: poR10007 Date: 01/22/2004



      javax.management.monitor.CounterMonitor (jdk1.5.0-b35) misses
      events when run in difference mode. The attached test expects
      2 notifications from the monitor but receives only one.
      Preliminary analysis shows that eventAlreadyNotified[index] flag
      is not always properly restored after a notification has been
      emitted.

      Please see the test sources and the execution log for details:

      ---Monitor.java---------------------------------------------------------------
      import javax.management.*;
      import javax.management.monitor.*;

      public class Monitor implements NotificationListener{

          private Number threshold = new Integer(2); // threshold number
          private Number modulus = new Integer(7); // modulus number
          private int offset = 0; // sequence of offset numbers
          private boolean differentmode = true; // with different mode or not
          private boolean isnotified = true; // notification object or not
          private int granularityperiod = 500; // granularity period or sample period for the attribute of the MBean
          private int[] values = new int[] {4, 6}; // counter values
          
          private int timeout = 5; // time to wait for notification (in seconds)
          private volatile boolean messageReceived = false; // flag to notify that a message has been received

          /* Notification handler */
          public void handleNotification(Notification notification, Object handback) {
              
              MonitorNotification notif = (MonitorNotification)notification;
      System.out.println("!!! in handleNotification");
              String type = notif.getType();
              try {
                  if (type.equals(MonitorNotification.THRESHOLD_VALUE_EXCEEDED)) {
                      System.out.println("\n\t>> " + notif.getObservedAttribute() + " has reached the threshold\n");
                      messageReceived = true;
                      synchronized (this) {
                          notifyAll();
                      }
                  }
                  else {
                      System.out.println("\n\t>> Skipping event of type: " + type);
                  }
              } catch (Exception e) {
                  System.err.println("Error in handleNotification!");
                  e.printStackTrace(System.err);
              }
          }


          /*
           * Standalone entry point.
           * Run the test(s) and report to stdout/stderr.
           */
          public static void main (String args[]) {
              Monitor test = new Monitor();
              test.ThresholdNotification();
          } // End of method main.


          public void ThresholdNotification() {

              CounterMonitor counterMonitor = new CounterMonitor();;
              
              try{
                  MBeanServer server = MBeanServerFactory.newMBeanServer();

                  String domain = server.getDefaultDomain();
                  // Create a new CounterMonitor MBean and add it to the MBeanServer.
                  System.out.println("\n>>> CREATE a new CounterMonitor MBean:");
                  ObjectName counterMonitorName = new ObjectName(domain + ":name=" +"javax.management.monitor.CounterMonitor");
                  server.registerMBean(counterMonitor, counterMonitorName);

                  System.out.println("\n>>> ADD a listener to the CounterMonitor...");
                  counterMonitor.addNotificationListener(this, null, null);
              
                  // MANAGEMENT OF A STANDARD MBEAN.
                  // Initialize the Standard MBean.
                  System.out.println("\n>>> CREATE a new StdObservedObject MBean:");
                  ObjectName stdObsObjName = new ObjectName(domain + ":name=" + "StdObservedObject");
                  StdObservedObject stdObsObj = new StdObservedObject();
                  server.registerMBean(stdObsObj, stdObsObjName);

                  System.out.println("\n>>> SET the attributes of the CounterMonitor:");

                  counterMonitor.setObservedObject(stdObsObjName);
      System.out.println("\tATTRIBUTE \"ObservedObject\" = " + stdObsObjName);

                  counterMonitor.setObservedAttribute("NbObjects");
      System.out.println("\tATTRIBUTE \"ObservedAttribute\" = NbObjects");

                  counterMonitor.setNotify(isnotified);
      System.out.println("\tATTRIBUTE \"Notify\" = " + isnotified);

                  counterMonitor.setThreshold(threshold);
      System.out.println("\tATTRIBUTE \"Threshold\" = " + threshold);

                  counterMonitor.setGranularityPeriod(granularityperiod);
      System.out.println("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod + " miliseconds");

                  counterMonitor.setModulus(modulus);
      System.out.println("\tATTRIBUTE \"Modulus\" = " + modulus);

                  counterMonitor.setDifferenceMode(differentmode);
      System.out.println("\tATTRIBUTE \"DifferenceMode\" = " + differentmode);

                  System.out.println("\n>>> START the CounterMonitor...");
                  counterMonitor.start();
                  
                  // set initial value
                  Integer data = new Integer(0);
                  System.out.println("set data = "+ data.intValue());

                  Attribute atrib = new Attribute("NbObjects", data);
                  server.setAttribute(stdObsObjName, atrib);
                  
                  // Wait for granularity period (multiplied by 2 for sure)
                  Thread.sleep(granularityperiod * 2);
                  
                  // Loop through the values
                  for (int i = 0; i < values.length; i++) {

                      data = new Integer(values[i]);
      System.out.println("set data = "+ data.intValue());

                      atrib = new Attribute("NbObjects", data);
                      server.setAttribute(stdObsObjName, atrib);

                      System.out.println("doWait in Counter Monitor");
                      doWait();

                      // comparison for the notifications
                      if (messageReceived) {
                          System.out.println("OKAY: Notification received");
                      } else {
                          System.out.println("Error: event notification is missed");
                          return;
                      }
                      messageReceived = false;
                  }
              } catch (Exception e){
                  e.printStackTrace(System.err);
      System.err.println("\n>>> An unexpected error occurred...");
                  return;
              } finally {
      counterMonitor.stop() ;
              }

              System.out.println("PASSED");
              return;
          }// End of method ThresholdNotification.
          
          
          /*
           * Wait until timeout reached
           */
          void doWait() {
              for (int i = 0; i < timeout; i++) {
                  System.out.println("doWait: waiting for " + timeout + " seconds. i = " + i + ", messageReceived = " + messageReceived);
                  if (messageReceived) {
                     break;
                  }
                  try {
                      synchronized (this) {
                          wait(1000);
                      }
                  } catch (InterruptedException e) {
                  }
              }
          }
      }
      ------------------------------------------------------------------------------

      ---StdObservedObject.java-----------------------------------------------------
      public class StdObservedObject implements StdObservedObjectMBean {

          public Object getNbObjects() {
      return count;
          }

          public void setNbObjects(Object n) {
      count = n;
          }

          private Object count= null;
      }
      ------------------------------------------------------------------------------

      ---StdObservedObjectMBean.java------------------------------------------------
      public interface StdObservedObjectMBean {
          public Object getNbObjects();
          public void setNbObjects(Object n);
      }
      ------------------------------------------------------------------------------

      ---log------------------------------------------------------------------------
      $ java -version
      java version "1.5.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b35)
      Java HotSpot(TM) Client VM (build 1.5.0-beta2-b35, mixed mode)
      $ javac -d . Monitor.java StdObservedObject.java StdObservedObjectMBean.java
      Note: Monitor.java uses or overrides a deprecated API.
      Note: Recompile with -Xlint:deprecation for details.
      $ java Monitor

      >>> CREATE a new CounterMonitor MBean:

      >>> ADD a listener to the CounterMonitor...

      >>> CREATE a new StdObservedObject MBean:

      >>> SET the attributes of the CounterMonitor:
              ATTRIBUTE "ObservedObject" = DefaultDomain:name=StdObservedObject
              ATTRIBUTE "ObservedAttribute" = NbObjects
              ATTRIBUTE "Notify" = true
              ATTRIBUTE "Threshold" = 2
              ATTRIBUTE "GranularityPeriod" = 500 miliseconds
              ATTRIBUTE "Modulus" = 7
              ATTRIBUTE "DifferenceMode" = true

      >>> START the CounterMonitor...
      set data = 0
      set data = 4
      doWait in Counter Monitor
      doWait: waiting for 5 seconds. i = 0, messageReceived = false
      !!! in handleNotification

              >> NbObjects has reached the threshold

      doWait: waiting for 5 seconds. i = 1, messageReceived = true
      OKAY: Notification received
      set data = 6
      doWait in Counter Monitor
      doWait: waiting for 5 seconds. i = 0, messageReceived = false
      doWait: waiting for 5 seconds. i = 1, messageReceived = false
      doWait: waiting for 5 seconds. i = 2, messageReceived = false
      doWait: waiting for 5 seconds. i = 3, messageReceived = false
      doWait: waiting for 5 seconds. i = 4, messageReceived = false
      Error: event notification is missed
      $
      ------------------------------------------------------------------------------

      ======================================================================

            lmalvent Luis-Miguel Alventosa (Inactive)
            passunw Pas Pas (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: