1 /* 2 * Copyright (c) 2005, 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 6333528 27 * @summary Check that the initial threshold is properly used by the observed 28 * objects added before the counter monitor is started as well as by 29 * the observed objects which are added once the monitor is started. 30 * @author Luis-Miguel Alventosa 31 * @run clean CounterMonitorInitThresholdTest 32 * @run build CounterMonitorInitThresholdTest 33 * @run main CounterMonitorInitThresholdTest 34 */ 35 36 import java.lang.management.ManagementFactory; 37 import javax.management.MBeanServer; 38 import javax.management.MBeanServerInvocationHandler; 39 import javax.management.Notification; 40 import javax.management.NotificationEmitter; 41 import javax.management.NotificationListener; 42 import javax.management.ObjectName; 43 import javax.management.monitor.CounterMonitor; 44 import javax.management.monitor.CounterMonitorMBean; 45 import javax.management.monitor.MonitorNotification; 46 47 public class CounterMonitorInitThresholdTest { 48 49 public interface TestMBean { 50 public int getCounter(); 51 public void setCounter(int count); 52 } 53 54 public static class Test implements TestMBean { 55 public int getCounter() { 56 return count; 57 } 58 public void setCounter(int count) { 59 this.count = count; 60 } 61 private int count = 0; 62 } 63 64 public static class Listener implements NotificationListener { 65 public void handleNotification(Notification n, Object hb) { 66 System.out.println("\tReceived notification: " + n.getType()); 67 if (n instanceof MonitorNotification) { 68 MonitorNotification mn = (MonitorNotification) n; 69 System.out.println("\tSource: " + 70 mn.getSource()); 71 System.out.println("\tType: " + 72 mn.getType()); 73 System.out.println("\tTimeStamp: " + 74 mn.getTimeStamp()); 75 System.out.println("\tObservedObject: " + 76 mn.getObservedObject()); 77 System.out.println("\tObservedAttribute: " + 78 mn.getObservedAttribute()); 79 System.out.println("\tDerivedGauge: " + 80 mn.getDerivedGauge()); 81 System.out.println("\tTrigger: " + 82 mn.getTrigger()); 83 } 84 } 85 } 86 87 public static void runTest() throws Exception { 88 // Retrieve the platform MBean server 89 // 90 System.out.println("\nRetrieve the platform MBean server"); 91 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 92 String domain = mbs.getDefaultDomain(); 93 94 // Create and register TestMBeans 95 // 96 ObjectName name1 = 97 new ObjectName(domain + 98 ":type=" + Test.class.getName() + 99 ",name=1"); 100 mbs.createMBean(Test.class.getName(), name1); 101 TestMBean mbean1 = (TestMBean) 102 MBeanServerInvocationHandler.newProxyInstance( 103 mbs, name1, TestMBean.class, false); 104 ObjectName name2 = 105 new ObjectName(domain + 106 ":type=" + Test.class.getName() + 107 ",name=2"); 108 mbs.createMBean(Test.class.getName(), name2); 109 TestMBean mbean2 = (TestMBean) 110 MBeanServerInvocationHandler.newProxyInstance( 111 mbs, name2, TestMBean.class, false); 112 113 // Create and register CounterMonitorMBean 114 // 115 ObjectName cmn = 116 new ObjectName(domain + 117 ":type=" + CounterMonitor.class.getName()); 118 CounterMonitor m = new CounterMonitor(); 119 mbs.registerMBean(m, cmn); 120 CounterMonitorMBean cm = (CounterMonitorMBean) 121 MBeanServerInvocationHandler.newProxyInstance( 122 mbs, cmn, CounterMonitorMBean.class, true); 123 ((NotificationEmitter) cm).addNotificationListener( 124 new Listener(), null, null); 125 cm.setObservedAttribute("Counter"); 126 cm.setGranularityPeriod(100); 127 cm.setInitThreshold(3); 128 cm.setNotify(true); 129 130 // Add observed object name1 131 // 132 System.out.println("\nObservedObject \"" + name1 + 133 "\" registered before starting the monitor"); 134 cm.addObservedObject(name1); 135 136 // Start the monitor 137 // 138 System.out.println("\nStart monitoring..."); 139 cm.start(); 140 141 // Play with counter for name1 142 // 143 System.out.println("\nTest ObservedObject \"" + name1 + "\""); 144 for (int i = 0; i < 4; i++) { 145 mbean1.setCounter(i); 146 System.out.println("\nCounter = " + mbean1.getCounter()); 147 Thread.sleep(300); 148 Number thresholdValue = cm.getThreshold(name1); 149 System.out.println("Threshold = " + thresholdValue); 150 if (thresholdValue.intValue() != 3) { 151 System.out.println("Wrong threshold! Current value = " + 152 thresholdValue + " Expected value = 3"); 153 System.out.println("\nStop monitoring..."); 154 cm.stop(); 155 throw new IllegalArgumentException("wrong threshold"); 156 } 157 Thread.sleep(300); 158 } 159 160 // Add observed object name2 161 // 162 System.out.println("\nObservedObject \"" + name2 + 163 "\" registered after starting the monitor"); 164 cm.addObservedObject(name2); 165 166 // Play with counter for name2 167 // 168 System.out.println("\nTest ObservedObject \"" + name2 + "\""); 169 for (int i = 0; i < 4; i++) { 170 mbean2.setCounter(i); 171 System.out.println("\nCounter = " + mbean2.getCounter()); 172 Thread.sleep(300); 173 Number thresholdValue = cm.getThreshold(name2); 174 System.out.println("Threshold = " + thresholdValue); 175 if (thresholdValue.intValue() != 3) { 176 System.out.println("Wrong threshold! Current value = " + 177 thresholdValue + " Expected value = 3"); 178 System.out.println("\nStop monitoring..."); 179 cm.stop(); 180 throw new IllegalArgumentException("wrong threshold"); 181 } 182 Thread.sleep(300); 183 } 184 185 // Stop the monitor 186 // 187 System.out.println("\nStop monitoring..."); 188 cm.stop(); 189 } 190 191 public static void main(String[] args) throws Exception { 192 runTest(); 193 } 194 }