1 /* 2 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4981829 27 * @summary Test that the counter monitor, when running in difference mode, 28 * emits a notification every time the threshold is exceeded. 29 * @author Luis-Miguel Alventosa 30 * @run clean CounterMonitorTest 31 * @run build CounterMonitorTest 32 * @run main CounterMonitorTest 33 */ 34 35 import javax.management.*; 36 import javax.management.monitor.*; 37 38 public class CounterMonitorTest implements NotificationListener { 39 40 // threshold number 41 private Number threshold = new Integer(2); 42 43 // modulus number 44 private Number modulus = new Integer(7); 45 46 // difference mode flag 47 private boolean differenceModeFlag = true; 48 49 // notify flag 50 private boolean notifyFlag = true; 51 52 // granularity period 53 private int granularityperiod = 500; 54 55 // counter values 56 private int[] values = new int[] {4, 6, 9, 11}; 57 58 // flag to notify that a message has been received 59 private volatile boolean messageReceived = false; 60 61 // MBean class 62 public class StdObservedObject implements StdObservedObjectMBean { 63 public Object getNbObjects() { 64 return count; 65 } 66 public void setNbObjects(Object n) { 67 count = n; 68 } 69 private Object count= null; 70 } 71 72 // MBean interface 73 public interface StdObservedObjectMBean { 74 public Object getNbObjects(); 75 public void setNbObjects(Object n); 76 } 77 78 // Notification handler 79 public void handleNotification(Notification notification, 80 Object handback) { 81 MonitorNotification n = (MonitorNotification) notification; 82 echo("\tInside handleNotification..."); 83 String type = n.getType(); 84 try { 85 if (type.equals(MonitorNotification.THRESHOLD_VALUE_EXCEEDED)) { 86 echo("\t\t" + n.getObservedAttribute() + 87 " has reached or exceeded the threshold"); 88 echo("\t\tDerived Gauge = " + n.getDerivedGauge()); 89 90 synchronized (this) { 91 messageReceived = true; 92 notifyAll(); 93 } 94 } else { 95 echo("\t\tSkipping notification of type: " + type); 96 } 97 } catch (Exception e) { 98 echo("\tError in handleNotification!"); 99 e.printStackTrace(System.out); 100 } 101 } 102 103 /** 104 * Update the counter and check for notifications 105 */ 106 public void thresholdNotification() throws Exception { 107 108 CounterMonitor counterMonitor = new CounterMonitor(); 109 try { 110 MBeanServer server = MBeanServerFactory.newMBeanServer(); 111 112 String domain = server.getDefaultDomain(); 113 114 // Create a new CounterMonitor MBean and add it to the MBeanServer. 115 // 116 echo(">>> CREATE a new CounterMonitor MBean"); 117 ObjectName counterMonitorName = new ObjectName( 118 domain + ":type=" + CounterMonitor.class.getName()); 119 server.registerMBean(counterMonitor, counterMonitorName); 120 121 echo(">>> ADD a listener to the CounterMonitor"); 122 counterMonitor.addNotificationListener(this, null, null); 123 124 // 125 // MANAGEMENT OF A STANDARD MBEAN 126 // 127 128 echo(">>> CREATE a new StdObservedObject MBean"); 129 130 ObjectName stdObsObjName = 131 new ObjectName(domain + ":type=StdObservedObject"); 132 StdObservedObject stdObsObj = new StdObservedObject(); 133 server.registerMBean(stdObsObj, stdObsObjName); 134 135 echo(">>> SET the attributes of the CounterMonitor:"); 136 137 counterMonitor.addObservedObject(stdObsObjName); 138 echo("\tATTRIBUTE \"ObservedObject\" = " + stdObsObjName); 139 140 counterMonitor.setObservedAttribute("NbObjects"); 141 echo("\tATTRIBUTE \"ObservedAttribute\" = NbObjects"); 142 143 counterMonitor.setNotify(notifyFlag); 144 echo("\tATTRIBUTE \"Notify\" = " + notifyFlag); 145 146 counterMonitor.setInitThreshold(threshold); 147 echo("\tATTRIBUTE \"Threshold\" = " + threshold); 148 149 counterMonitor.setGranularityPeriod(granularityperiod); 150 echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod); 151 152 counterMonitor.setModulus(modulus); 153 echo("\tATTRIBUTE \"Modulus\" = " + modulus); 154 155 counterMonitor.setDifferenceMode(differenceModeFlag); 156 echo("\tATTRIBUTE \"DifferenceMode\" = " + differenceModeFlag); 157 158 echo(">>> START the CounterMonitor"); 159 counterMonitor.start(); 160 161 // Set initial value 162 // 163 Integer data = new Integer(0); 164 echo(">>> Set data = " + data.intValue()); 165 166 Attribute attrib = new Attribute("NbObjects", data); 167 server.setAttribute(stdObsObjName, attrib); 168 169 // Wait for granularity period (multiplied by 2 for sure) 170 // 171 Thread.sleep(granularityperiod * 2); 172 173 // Loop through the values 174 // 175 for (int i = 0; i < values.length; i++) { 176 data = new Integer(values[i]); 177 echo(">>> Set data = " + data.intValue()); 178 179 attrib = new Attribute("NbObjects", data); 180 server.setAttribute(stdObsObjName, attrib); 181 182 echo("\tdoWait in Counter Monitor"); 183 doWait(); 184 185 // Check if notification was received 186 // 187 if (messageReceived) { 188 echo("\tOKAY: Notification received"); 189 } else { 190 echo("\tError: notification missed or not emitted"); 191 throw new IllegalStateException("Notification lost"); 192 } 193 messageReceived = false; 194 } 195 } finally { 196 counterMonitor.stop(); 197 } 198 199 echo(">>> Bye! Bye!"); 200 } 201 202 /* 203 * Wait messageReceived to be true 204 */ 205 synchronized void doWait() { 206 while (!messageReceived) { 207 try { 208 wait(); 209 } catch (InterruptedException e) { 210 System.err.println("Got unexpected exception: " + e); 211 e.printStackTrace(); 212 break; 213 } 214 } 215 } 216 217 /* 218 * Print message 219 */ 220 void echo(String message) { 221 System.out.println(message); 222 } 223 224 /* 225 * Standalone entry point. 226 * 227 * Run the test and report to stdout. 228 */ 229 public static void main (String args[]) throws Exception { 230 CounterMonitorTest test = new CounterMonitorTest(); 231 test.thresholdNotification(); 232 } 233 }