Path: blob/master/test/jdk/javax/management/monitor/CounterMonitorInitThresholdTest.java
41149 views
/*1* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 633352826* @summary Check that the initial threshold is properly used by the observed27* objects added before the counter monitor is started as well as by28* the observed objects which are added once the monitor is started.29* @author Luis-Miguel Alventosa30*31* @run clean CounterMonitorInitThresholdTest32* @run build CounterMonitorInitThresholdTest33* @run main CounterMonitorInitThresholdTest34*/3536import java.lang.management.ManagementFactory;37import javax.management.MBeanServer;38import javax.management.MBeanServerInvocationHandler;39import javax.management.Notification;40import javax.management.NotificationEmitter;41import javax.management.NotificationListener;42import javax.management.ObjectName;43import javax.management.monitor.CounterMonitor;44import javax.management.monitor.CounterMonitorMBean;45import javax.management.monitor.MonitorNotification;4647public class CounterMonitorInitThresholdTest {4849public interface TestMBean {50public int getCounter();51public void setCounter(int count);52}5354public static class Test implements TestMBean {55public int getCounter() {56return count;57}58public void setCounter(int count) {59this.count = count;60}61private int count = 0;62}6364public static class Listener implements NotificationListener {65public void handleNotification(Notification n, Object hb) {66System.out.println("\tReceived notification: " + n.getType());67if (n instanceof MonitorNotification) {68MonitorNotification mn = (MonitorNotification) n;69System.out.println("\tSource: " +70mn.getSource());71System.out.println("\tType: " +72mn.getType());73System.out.println("\tTimeStamp: " +74mn.getTimeStamp());75System.out.println("\tObservedObject: " +76mn.getObservedObject());77System.out.println("\tObservedAttribute: " +78mn.getObservedAttribute());79System.out.println("\tDerivedGauge: " +80mn.getDerivedGauge());81System.out.println("\tTrigger: " +82mn.getTrigger());83}84}85}8687public static void runTest() throws Exception {88// Retrieve the platform MBean server89//90System.out.println("\nRetrieve the platform MBean server");91MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();92String domain = mbs.getDefaultDomain();9394// Create and register TestMBeans95//96ObjectName name1 =97new ObjectName(domain +98":type=" + Test.class.getName() +99",name=1");100mbs.createMBean(Test.class.getName(), name1);101TestMBean mbean1 = (TestMBean)102MBeanServerInvocationHandler.newProxyInstance(103mbs, name1, TestMBean.class, false);104ObjectName name2 =105new ObjectName(domain +106":type=" + Test.class.getName() +107",name=2");108mbs.createMBean(Test.class.getName(), name2);109TestMBean mbean2 = (TestMBean)110MBeanServerInvocationHandler.newProxyInstance(111mbs, name2, TestMBean.class, false);112113// Create and register CounterMonitorMBean114//115ObjectName cmn =116new ObjectName(domain +117":type=" + CounterMonitor.class.getName());118CounterMonitor m = new CounterMonitor();119mbs.registerMBean(m, cmn);120CounterMonitorMBean cm = (CounterMonitorMBean)121MBeanServerInvocationHandler.newProxyInstance(122mbs, cmn, CounterMonitorMBean.class, true);123((NotificationEmitter) cm).addNotificationListener(124new Listener(), null, null);125cm.setObservedAttribute("Counter");126cm.setGranularityPeriod(100);127cm.setInitThreshold(3);128cm.setNotify(true);129130// Add observed object name1131//132System.out.println("\nObservedObject \"" + name1 +133"\" registered before starting the monitor");134cm.addObservedObject(name1);135136// Start the monitor137//138System.out.println("\nStart monitoring...");139cm.start();140141// Play with counter for name1142//143System.out.println("\nTest ObservedObject \"" + name1 + "\"");144for (int i = 0; i < 4; i++) {145mbean1.setCounter(i);146System.out.println("\nCounter = " + mbean1.getCounter());147Thread.sleep(300);148Number thresholdValue = cm.getThreshold(name1);149System.out.println("Threshold = " + thresholdValue);150if (thresholdValue.intValue() != 3) {151System.out.println("Wrong threshold! Current value = " +152thresholdValue + " Expected value = 3");153System.out.println("\nStop monitoring...");154cm.stop();155throw new IllegalArgumentException("wrong threshold");156}157Thread.sleep(300);158}159160// Add observed object name2161//162System.out.println("\nObservedObject \"" + name2 +163"\" registered after starting the monitor");164cm.addObservedObject(name2);165166// Play with counter for name2167//168System.out.println("\nTest ObservedObject \"" + name2 + "\"");169for (int i = 0; i < 4; i++) {170mbean2.setCounter(i);171System.out.println("\nCounter = " + mbean2.getCounter());172Thread.sleep(300);173Number thresholdValue = cm.getThreshold(name2);174System.out.println("Threshold = " + thresholdValue);175if (thresholdValue.intValue() != 3) {176System.out.println("Wrong threshold! Current value = " +177thresholdValue + " Expected value = 3");178System.out.println("\nStop monitoring...");179cm.stop();180throw new IllegalArgumentException("wrong threshold");181}182Thread.sleep(300);183}184185// Stop the monitor186//187System.out.println("\nStop monitoring...");188cm.stop();189}190191public static void main(String[] args) throws Exception {192runTest();193}194}195196197