test/javax/management/monitor/ReflectionExceptionTest.java

Print this page




  70     public interface ObservedObjectMBean {
  71         public Integer getIntegerAttribute();
  72         public void setIntegerAttribute(Integer i);
  73         public String getStringAttribute();
  74         public void setStringAttribute(String s);
  75     }
  76 
  77     // Notification handler
  78     public void handleNotification(Notification notification, Object handback) {
  79         echo(">>> Received notification: " + notification);
  80         if (notification instanceof MonitorNotification) {
  81             String type = notification.getType();
  82             if (type.equals(MonitorNotification.RUNTIME_ERROR)) {
  83                 MonitorNotification mn = (MonitorNotification) notification;
  84                 echo("\tType: " + mn.getType());
  85                 echo("\tTimeStamp: " + mn.getTimeStamp());
  86                 echo("\tObservedObject: " + mn.getObservedObject());
  87                 echo("\tObservedAttribute: " + mn.getObservedAttribute());
  88                 echo("\tDerivedGauge: " + mn.getDerivedGauge());
  89                 echo("\tTrigger: " + mn.getTrigger());


  90                 messageReceived = true;


  91             }
  92         }
  93     }
  94 
  95     /**
  96      * Update the counter and check for notifications
  97      */
  98     public int counterMonitorNotification() throws Exception {
  99 
 100         CounterMonitor counterMonitor = new CounterMonitor();
 101         try {
 102             // Create a new CounterMonitor MBean and add it to the MBeanServer.
 103             //
 104             echo(">>> CREATE a new CounterMonitor MBean");
 105             ObjectName counterMonitorName = new ObjectName(
 106                             domain + ":type=" + CounterMonitor.class.getName());
 107             server.registerMBean(counterMonitor, counterMonitorName);
 108 
 109             echo(">>> ADD a listener to the CounterMonitor");
 110             counterMonitor.addNotificationListener(this, null, null);


 118             counterMonitor.addObservedObject(obsObjName);
 119             echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
 120 
 121             counterMonitor.setObservedAttribute("IntegerAttribute");
 122             echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
 123 
 124             counterMonitor.setNotify(false);
 125             echo("\tATTRIBUTE \"NotifyFlag\"        = false");
 126 
 127             Integer threshold = 2;
 128             counterMonitor.setInitThreshold(threshold);
 129             echo("\tATTRIBUTE \"Threshold\"         = " + threshold);
 130 
 131             int granularityperiod = 500;
 132             counterMonitor.setGranularityPeriod(granularityperiod);
 133             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
 134 
 135             echo(">>> START the CounterMonitor");
 136             counterMonitor.start();
 137 
 138             // Wait for granularity period (multiplied by 2 for sure)
 139             //
 140             Thread.sleep(granularityperiod * 2);
 141 
 142             // Check if notification was received
 143             //

 144             if (messageReceived) {
 145                 echo("\tOK: CounterMonitor got RUNTIME_ERROR notification!");
 146             } else {
 147                 echo("\tKO: CounterMonitor did not get " +
 148                      "RUNTIME_ERROR notification!");
 149                 return 1;
 150             }
 151         } finally {
 152             messageReceived = false;
 153             if (counterMonitor != null)
 154                 counterMonitor.stop();
 155         }
 156 
 157         return 0;
 158     }
 159 
 160     /**
 161      * Update the gauge and check for notifications
 162      */
 163     public int gaugeMonitorNotification() throws Exception {


 186             gaugeMonitor.setObservedAttribute("IntegerAttribute");
 187             echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
 188 
 189             gaugeMonitor.setNotifyLow(false);
 190             gaugeMonitor.setNotifyHigh(false);
 191             echo("\tATTRIBUTE \"Notify Low  Flag\"  = false");
 192             echo("\tATTRIBUTE \"Notify High Flag\"  = false");
 193 
 194             Integer highThreshold = 3, lowThreshold = 2;
 195             gaugeMonitor.setThresholds(highThreshold, lowThreshold);
 196             echo("\tATTRIBUTE \"Low  Threshold\"    = " + lowThreshold);
 197             echo("\tATTRIBUTE \"High Threshold\"    = " + highThreshold);
 198 
 199             int granularityperiod = 500;
 200             gaugeMonitor.setGranularityPeriod(granularityperiod);
 201             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
 202 
 203             echo(">>> START the GaugeMonitor");
 204             gaugeMonitor.start();
 205 
 206             // Wait for granularity period (multiplied by 2 for sure)
 207             //
 208             Thread.sleep(granularityperiod * 2);
 209 
 210             // Check if notification was received
 211             //

 212             if (messageReceived) {
 213                 echo("\tOK: GaugeMonitor got RUNTIME_ERROR notification!");
 214             } else {
 215                 echo("\tKO: GaugeMonitor did not get " +
 216                      "RUNTIME_ERROR notification!");
 217                 return 1;
 218             }
 219         } finally {
 220             messageReceived = false;
 221             if (gaugeMonitor != null)
 222                 gaugeMonitor.stop();
 223         }
 224 
 225         return 0;
 226     }
 227 
 228     /**
 229      * Update the string and check for notifications
 230      */
 231     public int stringMonitorNotification() throws Exception {


 253 
 254             stringMonitor.setObservedAttribute("StringAttribute");
 255             echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");
 256 
 257             stringMonitor.setNotifyMatch(false);
 258             echo("\tATTRIBUTE \"NotifyMatch\"       = false");
 259 
 260             stringMonitor.setNotifyDiffer(false);
 261             echo("\tATTRIBUTE \"NotifyDiffer\"      = false");
 262 
 263             stringMonitor.setStringToCompare("dummy");
 264             echo("\tATTRIBUTE \"StringToCompare\"   = \"dummy\"");
 265 
 266             int granularityperiod = 500;
 267             stringMonitor.setGranularityPeriod(granularityperiod);
 268             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
 269 
 270             echo(">>> START the StringMonitor");
 271             stringMonitor.start();
 272 
 273             // Wait for granularity period (multiplied by 2 for sure)
 274             //
 275             Thread.sleep(granularityperiod * 2);
 276 
 277             // Check if notification was received
 278             //

 279             if (messageReceived) {
 280                 echo("\tOK: StringMonitor got RUNTIME_ERROR notification!");
 281             } else {
 282                 echo("\tKO: StringMonitor did not get " +
 283                      "RUNTIME_ERROR notification!");
 284                 return 1;
 285             }
 286         } finally {
 287             messageReceived = false;
 288             if (stringMonitor != null)
 289                 stringMonitor.stop();
 290         }
 291 
 292         return 0;
 293     }
 294 
 295     /**
 296      * Test the monitor notifications.
 297      */
 298     public int monitorNotifications() throws Exception {


 332     /*
 333      * Standalone entry point.
 334      *
 335      * Run the test and report to stdout.
 336      */
 337     public static void main (String args[]) throws Exception {
 338         System.setProperty("javax.management.builder.initial",
 339                            MBeanServerBuilderImpl.class.getName());
 340         ReflectionExceptionTest test = new ReflectionExceptionTest();
 341         int error = test.monitorNotifications();
 342         if (error > 0) {
 343             echo(">>> Unhappy Bye, Bye!");
 344             throw new IllegalStateException("Test FAILED: Didn't get all " +
 345                                             "the notifications that were " +
 346                                             "expected by the test!");
 347         } else {
 348             echo(">>> Happy Bye, Bye!");
 349         }
 350     }
 351 















 352     // Flag to notify that a message has been received
 353     private boolean messageReceived = false;
 354 
 355     private MBeanServer server;
 356     private ObjectName obsObjName;
 357     private String domain;
 358 }


  70     public interface ObservedObjectMBean {
  71         public Integer getIntegerAttribute();
  72         public void setIntegerAttribute(Integer i);
  73         public String getStringAttribute();
  74         public void setStringAttribute(String s);
  75     }
  76 
  77     // Notification handler
  78     public void handleNotification(Notification notification, Object handback) {
  79         echo(">>> Received notification: " + notification);
  80         if (notification instanceof MonitorNotification) {
  81             String type = notification.getType();
  82             if (type.equals(MonitorNotification.RUNTIME_ERROR)) {
  83                 MonitorNotification mn = (MonitorNotification) notification;
  84                 echo("\tType: " + mn.getType());
  85                 echo("\tTimeStamp: " + mn.getTimeStamp());
  86                 echo("\tObservedObject: " + mn.getObservedObject());
  87                 echo("\tObservedAttribute: " + mn.getObservedAttribute());
  88                 echo("\tDerivedGauge: " + mn.getDerivedGauge());
  89                 echo("\tTrigger: " + mn.getTrigger());
  90                 
  91                 synchronized (this) {
  92                     messageReceived = true;
  93                     notifyAll();
  94                 }
  95             }
  96         }
  97     }
  98 
  99     /**
 100      * Update the counter and check for notifications
 101      */
 102     public int counterMonitorNotification() throws Exception {
 103 
 104         CounterMonitor counterMonitor = new CounterMonitor();
 105         try {
 106             // Create a new CounterMonitor MBean and add it to the MBeanServer.
 107             //
 108             echo(">>> CREATE a new CounterMonitor MBean");
 109             ObjectName counterMonitorName = new ObjectName(
 110                             domain + ":type=" + CounterMonitor.class.getName());
 111             server.registerMBean(counterMonitor, counterMonitorName);
 112 
 113             echo(">>> ADD a listener to the CounterMonitor");
 114             counterMonitor.addNotificationListener(this, null, null);


 122             counterMonitor.addObservedObject(obsObjName);
 123             echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
 124 
 125             counterMonitor.setObservedAttribute("IntegerAttribute");
 126             echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
 127 
 128             counterMonitor.setNotify(false);
 129             echo("\tATTRIBUTE \"NotifyFlag\"        = false");
 130 
 131             Integer threshold = 2;
 132             counterMonitor.setInitThreshold(threshold);
 133             echo("\tATTRIBUTE \"Threshold\"         = " + threshold);
 134 
 135             int granularityperiod = 500;
 136             counterMonitor.setGranularityPeriod(granularityperiod);
 137             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
 138 
 139             echo(">>> START the CounterMonitor");
 140             counterMonitor.start();
 141 




 142             // Check if notification was received
 143             //
 144             doWait();
 145             if (messageReceived) {
 146                 echo("\tOK: CounterMonitor got RUNTIME_ERROR notification!");
 147             } else {
 148                 echo("\tKO: CounterMonitor did not get " +
 149                      "RUNTIME_ERROR notification!");
 150                 return 1;
 151             }
 152         } finally {
 153             messageReceived = false;
 154             if (counterMonitor != null)
 155                 counterMonitor.stop();
 156         }
 157 
 158         return 0;
 159     }
 160 
 161     /**
 162      * Update the gauge and check for notifications
 163      */
 164     public int gaugeMonitorNotification() throws Exception {


 187             gaugeMonitor.setObservedAttribute("IntegerAttribute");
 188             echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
 189 
 190             gaugeMonitor.setNotifyLow(false);
 191             gaugeMonitor.setNotifyHigh(false);
 192             echo("\tATTRIBUTE \"Notify Low  Flag\"  = false");
 193             echo("\tATTRIBUTE \"Notify High Flag\"  = false");
 194 
 195             Integer highThreshold = 3, lowThreshold = 2;
 196             gaugeMonitor.setThresholds(highThreshold, lowThreshold);
 197             echo("\tATTRIBUTE \"Low  Threshold\"    = " + lowThreshold);
 198             echo("\tATTRIBUTE \"High Threshold\"    = " + highThreshold);
 199 
 200             int granularityperiod = 500;
 201             gaugeMonitor.setGranularityPeriod(granularityperiod);
 202             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
 203 
 204             echo(">>> START the GaugeMonitor");
 205             gaugeMonitor.start();
 206 




 207             // Check if notification was received
 208             //
 209             doWait();
 210             if (messageReceived) {
 211                 echo("\tOK: GaugeMonitor got RUNTIME_ERROR notification!");
 212             } else {
 213                 echo("\tKO: GaugeMonitor did not get " +
 214                      "RUNTIME_ERROR notification!");
 215                 return 1;
 216             }
 217         } finally {
 218             messageReceived = false;
 219             if (gaugeMonitor != null)
 220                 gaugeMonitor.stop();
 221         }
 222 
 223         return 0;
 224     }
 225 
 226     /**
 227      * Update the string and check for notifications
 228      */
 229     public int stringMonitorNotification() throws Exception {


 251 
 252             stringMonitor.setObservedAttribute("StringAttribute");
 253             echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");
 254 
 255             stringMonitor.setNotifyMatch(false);
 256             echo("\tATTRIBUTE \"NotifyMatch\"       = false");
 257 
 258             stringMonitor.setNotifyDiffer(false);
 259             echo("\tATTRIBUTE \"NotifyDiffer\"      = false");
 260 
 261             stringMonitor.setStringToCompare("dummy");
 262             echo("\tATTRIBUTE \"StringToCompare\"   = \"dummy\"");
 263 
 264             int granularityperiod = 500;
 265             stringMonitor.setGranularityPeriod(granularityperiod);
 266             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
 267 
 268             echo(">>> START the StringMonitor");
 269             stringMonitor.start();
 270 




 271             // Check if notification was received
 272             //
 273             doWait();
 274             if (messageReceived) {
 275                 echo("\tOK: StringMonitor got RUNTIME_ERROR notification!");
 276             } else {
 277                 echo("\tKO: StringMonitor did not get " +
 278                      "RUNTIME_ERROR notification!");
 279                 return 1;
 280             }
 281         } finally {
 282             messageReceived = false;
 283             if (stringMonitor != null)
 284                 stringMonitor.stop();
 285         }
 286 
 287         return 0;
 288     }
 289 
 290     /**
 291      * Test the monitor notifications.
 292      */
 293     public int monitorNotifications() throws Exception {


 327     /*
 328      * Standalone entry point.
 329      *
 330      * Run the test and report to stdout.
 331      */
 332     public static void main (String args[]) throws Exception {
 333         System.setProperty("javax.management.builder.initial",
 334                            MBeanServerBuilderImpl.class.getName());
 335         ReflectionExceptionTest test = new ReflectionExceptionTest();
 336         int error = test.monitorNotifications();
 337         if (error > 0) {
 338             echo(">>> Unhappy Bye, Bye!");
 339             throw new IllegalStateException("Test FAILED: Didn't get all " +
 340                                             "the notifications that were " +
 341                                             "expected by the test!");
 342         } else {
 343             echo(">>> Happy Bye, Bye!");
 344         }
 345     }
 346     
 347     /*
 348      * Wait messageReceived to be true 
 349      */
 350     synchronized void doWait() {
 351         while (!messageReceived) {
 352             try {
 353                 wait();
 354             } catch (InterruptedException e) {
 355                 System.err.println("Got unexpected exception: " + e);
 356                 e.printStackTrace();
 357                 break;
 358             }
 359         }
 360     }
 361 
 362     // Flag to notify that a message has been received
 363     private volatile boolean messageReceived = false;
 364 
 365     private MBeanServer server;
 366     private ObjectName obsObjName;
 367     private String domain;
 368 }