Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/monitor/CounterMonitorInitThresholdTest.java
38838 views
/*1* Copyright (c) 2005, 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* @run clean CounterMonitorInitThresholdTest31* @run build CounterMonitorInitThresholdTest32* @run main CounterMonitorInitThresholdTest33*/3435import java.lang.management.ManagementFactory;36import javax.management.MBeanServer;37import javax.management.MBeanServerInvocationHandler;38import javax.management.Notification;39import javax.management.NotificationEmitter;40import javax.management.NotificationListener;41import javax.management.ObjectName;42import javax.management.monitor.CounterMonitor;43import javax.management.monitor.CounterMonitorMBean;44import javax.management.monitor.MonitorNotification;4546public class CounterMonitorInitThresholdTest {4748public interface TestMBean {49public int getCounter();50public void setCounter(int count);51}5253public static class Test implements TestMBean {54public int getCounter() {55return count;56}57public void setCounter(int count) {58this.count = count;59}60private int count = 0;61}6263public static class Listener implements NotificationListener {64public void handleNotification(Notification n, Object hb) {65System.out.println("\tReceived notification: " + n.getType());66if (n instanceof MonitorNotification) {67MonitorNotification mn = (MonitorNotification) n;68System.out.println("\tSource: " +69mn.getSource());70System.out.println("\tType: " +71mn.getType());72System.out.println("\tTimeStamp: " +73mn.getTimeStamp());74System.out.println("\tObservedObject: " +75mn.getObservedObject());76System.out.println("\tObservedAttribute: " +77mn.getObservedAttribute());78System.out.println("\tDerivedGauge: " +79mn.getDerivedGauge());80System.out.println("\tTrigger: " +81mn.getTrigger());82}83}84}8586public static void runTest() throws Exception {87// Retrieve the platform MBean server88//89System.out.println("\nRetrieve the platform MBean server");90MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();91String domain = mbs.getDefaultDomain();9293// Create and register TestMBeans94//95ObjectName name1 =96new ObjectName(domain +97":type=" + Test.class.getName() +98",name=1");99mbs.createMBean(Test.class.getName(), name1);100TestMBean mbean1 = (TestMBean)101MBeanServerInvocationHandler.newProxyInstance(102mbs, name1, TestMBean.class, false);103ObjectName name2 =104new ObjectName(domain +105":type=" + Test.class.getName() +106",name=2");107mbs.createMBean(Test.class.getName(), name2);108TestMBean mbean2 = (TestMBean)109MBeanServerInvocationHandler.newProxyInstance(110mbs, name2, TestMBean.class, false);111112// Create and register CounterMonitorMBean113//114ObjectName cmn =115new ObjectName(domain +116":type=" + CounterMonitor.class.getName());117CounterMonitor m = new CounterMonitor();118mbs.registerMBean(m, cmn);119CounterMonitorMBean cm = (CounterMonitorMBean)120MBeanServerInvocationHandler.newProxyInstance(121mbs, cmn, CounterMonitorMBean.class, true);122((NotificationEmitter) cm).addNotificationListener(123new Listener(), null, null);124cm.setObservedAttribute("Counter");125cm.setGranularityPeriod(100);126cm.setInitThreshold(3);127cm.setNotify(true);128129// Add observed object name1130//131System.out.println("\nObservedObject \"" + name1 +132"\" registered before starting the monitor");133cm.addObservedObject(name1);134135// Start the monitor136//137System.out.println("\nStart monitoring...");138cm.start();139140// Play with counter for name1141//142System.out.println("\nTest ObservedObject \"" + name1 + "\"");143for (int i = 0; i < 4; i++) {144mbean1.setCounter(i);145System.out.println("\nCounter = " + mbean1.getCounter());146Thread.sleep(300);147Number thresholdValue = cm.getThreshold(name1);148System.out.println("Threshold = " + thresholdValue);149if (thresholdValue.intValue() != 3) {150System.out.println("Wrong threshold! Current value = " +151thresholdValue + " Expected value = 3");152System.out.println("\nStop monitoring...");153cm.stop();154throw new IllegalArgumentException("wrong threshold");155}156Thread.sleep(300);157}158159// Add observed object name2160//161System.out.println("\nObservedObject \"" + name2 +162"\" registered after starting the monitor");163cm.addObservedObject(name2);164165// Play with counter for name2166//167System.out.println("\nTest ObservedObject \"" + name2 + "\"");168for (int i = 0; i < 4; i++) {169mbean2.setCounter(i);170System.out.println("\nCounter = " + mbean2.getCounter());171Thread.sleep(300);172Number thresholdValue = cm.getThreshold(name2);173System.out.println("Threshold = " + thresholdValue);174if (thresholdValue.intValue() != 3) {175System.out.println("Wrong threshold! Current value = " +176thresholdValue + " Expected value = 3");177System.out.println("\nStop monitoring...");178cm.stop();179throw new IllegalArgumentException("wrong threshold");180}181Thread.sleep(300);182}183184// Stop the monitor185//186System.out.println("\nStop monitoring...");187cm.stop();188}189190public static void main(String[] args) throws Exception {191runTest();192}193}194195196