test/javax/management/monitor/RuntimeExceptionTest.java

Print this page

        

@@ -84,11 +84,15 @@
                 echo("\tTimeStamp: " + mn.getTimeStamp());
                 echo("\tObservedObject: " + mn.getObservedObject());
                 echo("\tObservedAttribute: " + mn.getObservedAttribute());
                 echo("\tDerivedGauge: " + mn.getDerivedGauge());
                 echo("\tTrigger: " + mn.getTrigger());
+                
+                synchronized (this) {
                 messageReceived = true;
+                    notifyAll();
+                }
             }
         }
     }
 
     /**

@@ -132,16 +136,13 @@
             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
 
             echo(">>> START the CounterMonitor");
             counterMonitor.start();
 
-            // Wait for granularity period (multiplied by 2 for sure)
-            //
-            Thread.sleep(granularityperiod * 2);
-
             // Check if notification was received
             //
+            doWait();
             if (messageReceived) {
                 echo("\tOK: CounterMonitor got RUNTIME_ERROR notification!");
             } else {
                 echo("\tKO: CounterMonitor did not get " +
                      "RUNTIME_ERROR notification!");

@@ -200,16 +201,13 @@
             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
 
             echo(">>> START the GaugeMonitor");
             gaugeMonitor.start();
 
-            // Wait for granularity period (multiplied by 2 for sure)
-            //
-            Thread.sleep(granularityperiod * 2);
-
             // Check if notification was received
             //
+            doWait();
             if (messageReceived) {
                 echo("\tOK: GaugeMonitor got RUNTIME_ERROR notification!");
             } else {
                 echo("\tKO: GaugeMonitor did not get " +
                      "RUNTIME_ERROR notification!");

@@ -267,16 +265,13 @@
             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
 
             echo(">>> START the StringMonitor");
             stringMonitor.start();
 
-            // Wait for granularity period (multiplied by 2 for sure)
-            //
-            Thread.sleep(granularityperiod * 2);
-
             // Check if notification was received
             //
+            doWait();
             if (messageReceived) {
                 echo("\tOK: StringMonitor got RUNTIME_ERROR notification!");
             } else {
                 echo("\tKO: StringMonitor did not get " +
                      "RUNTIME_ERROR notification!");

@@ -345,12 +340,27 @@
         } else {
             echo(">>> Happy Bye, Bye!");
         }
     }
 
+    /*
+     * Wait messageReceived to be true 
+     */
+    synchronized void doWait() {
+        while (!messageReceived) {
+            try {
+                wait();
+            } catch (InterruptedException e) {
+                System.err.println("Got unexpected exception: " + e);
+                e.printStackTrace();
+                break;
+            }
+        }
+    }
+    
     // Flag to notify that a message has been received
-    private boolean messageReceived = false;
+    private volatile boolean messageReceived = false;
 
     private MBeanServer server;
     private ObjectName obsObjName;
     private String domain;
 }