Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/monitor/CounterMonitorTest.java
38838 views
/*1* Copyright (c) 2004, 2014, 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 498182926* @summary Test that the counter monitor, when running in difference mode,27* emits a notification every time the threshold is exceeded.28* @author Luis-Miguel Alventosa, Shanliang JIANG29* @run clean CounterMonitorTest30* @run build CounterMonitorTest31* @run main CounterMonitorTest32*/3334import javax.management.*;35import javax.management.monitor.*;3637public class CounterMonitorTest implements NotificationListener {3839// threshold number40private Number threshold = new Integer(2);4142// modulus number43private Number modulus = new Integer(7);4445// difference mode flag46private boolean differenceModeFlag = true;4748// notify flag49private boolean notifyFlag = true;5051// granularity period52private int granularityperiod = 10;5354// derived gauge55private volatile int derivedGauge = 2;5657// flag to notify that a message has been received58private volatile boolean messageReceived = false;5960private volatile Object observedValue = null;6162// MBean class63public class StdObservedObject implements StdObservedObjectMBean {64public Object getNbObjects() {65echo(">>> StdObservedObject.getNbObjects: " + count);66synchronized(CounterMonitorTest.class) {67observedValue = count;68CounterMonitorTest.class.notifyAll();69}70return observedValue;71}72public void setNbObjects(Object n) {73echo(">>> StdObservedObject.setNbObjects: " + n);74count = n;75}76private volatile Object count= null;77}7879// MBean interface80public interface StdObservedObjectMBean {81public Object getNbObjects();82public void setNbObjects(Object n);83}8485// Notification handler86public void handleNotification(Notification notification,87Object handback) {88MonitorNotification n = (MonitorNotification) notification;89echo("\tInside handleNotification...");90String type = n.getType();91try {92if (type.equals(MonitorNotification.THRESHOLD_VALUE_EXCEEDED)) {93echo("\t\t" + n.getObservedAttribute() +94" has reached or exceeded the threshold");95echo("\t\tDerived Gauge = " + n.getDerivedGauge());9697synchronized (this) {98messageReceived = true;99notifyAll();100}101} else {102echo("\t\tSkipping notification of type: " + type);103}104} catch (Exception e) {105echo("\tError in handleNotification!");106e.printStackTrace(System.out);107}108}109110/**111* Update the counter and check for notifications112*/113public void thresholdNotification() throws Exception {114115CounterMonitor counterMonitor = new CounterMonitor();116try {117MBeanServer server = MBeanServerFactory.newMBeanServer();118119String domain = server.getDefaultDomain();120121// Create a new CounterMonitor MBean and add it to the MBeanServer.122//123echo(">>> CREATE a new CounterMonitor MBean");124ObjectName counterMonitorName = new ObjectName(125domain + ":type=" + CounterMonitor.class.getName());126server.registerMBean(counterMonitor, counterMonitorName);127128echo(">>> ADD a listener to the CounterMonitor");129counterMonitor.addNotificationListener(this, null, null);130131//132// MANAGEMENT OF A STANDARD MBEAN133//134135echo(">>> CREATE a new StdObservedObject MBean");136137ObjectName stdObsObjName =138new ObjectName(domain + ":type=StdObservedObject");139StdObservedObject stdObsObj = new StdObservedObject();140server.registerMBean(stdObsObj, stdObsObjName);141142echo(">>> SET the attributes of the CounterMonitor:");143144counterMonitor.addObservedObject(stdObsObjName);145echo("\tATTRIBUTE \"ObservedObject\" = " + stdObsObjName);146147counterMonitor.setObservedAttribute("NbObjects");148echo("\tATTRIBUTE \"ObservedAttribute\" = NbObjects");149150counterMonitor.setNotify(notifyFlag);151echo("\tATTRIBUTE \"Notify\" = " + notifyFlag);152153counterMonitor.setInitThreshold(threshold);154echo("\tATTRIBUTE \"Threshold\" = " + threshold);155156counterMonitor.setGranularityPeriod(granularityperiod);157echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);158159counterMonitor.setModulus(modulus);160echo("\tATTRIBUTE \"Modulus\" = " + modulus);161162counterMonitor.setDifferenceMode(differenceModeFlag);163echo("\tATTRIBUTE \"DifferenceMode\" = " + differenceModeFlag);164165echo(">>> START the CounterMonitor");166counterMonitor.start();167168// Set initial value169//170Integer data = new Integer(0);171echo(">>> Set data = " + data.intValue());172173Attribute attrib = new Attribute("NbObjects", data);174server.setAttribute(stdObsObjName, attrib);175176waitObservation(data);177178// Loop through the values179//180while (derivedGauge++ < 10) {181System.out.print(">>> Set data from " + data.intValue());182data = new Integer(data.intValue() + derivedGauge);183echo(" to " + data.intValue());184185attrib = new Attribute("NbObjects", data);186server.setAttribute(stdObsObjName, attrib);187waitObservation(data);188189echo("\tdoWait in Counter Monitor");190doWait();191192// Check if notification was received193//194if (messageReceived) {195echo("\tOKAY: Notification received");196} else {197echo("\tError: notification missed or not emitted");198throw new IllegalStateException("Notification lost");199}200messageReceived = false;201}202} finally {203counterMonitor.stop();204}205206echo(">>> Bye! Bye!");207}208209/*210* Wait messageReceived to be true211*/212synchronized void doWait() {213while (!messageReceived) {214try {215wait();216} catch (InterruptedException e) {217System.err.println("Got unexpected exception: " + e);218e.printStackTrace();219break;220}221}222}223224private void waitObservation(Object value) {225synchronized (CounterMonitorTest.class) {226while (value != observedValue) {227try {228CounterMonitorTest.class.wait();229} catch (InterruptedException e) {230System.err.println("Got unexpected exception: " + e);231e.printStackTrace();232break;233}234}235}236}237238/*239* Print message240*/241void echo(String message) {242System.out.println(message);243}244245/*246* Standalone entry point.247*248* Run the test and report to stdout.249*/250public static void main (String args[]) throws Exception {251CounterMonitorTest test = new CounterMonitorTest();252test.thresholdNotification();253}254}255256257