Path: blob/master/test/jdk/javax/management/monitor/CounterMonitorTest.java
41149 views
/*1* Copyright (c) 2004, 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 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*30* @run clean CounterMonitorTest31* @run build CounterMonitorTest32* @run main CounterMonitorTest33*/3435import javax.management.*;36import javax.management.monitor.*;3738public class CounterMonitorTest implements NotificationListener {3940// threshold number41private Number threshold = new Integer(2);4243// modulus number44private Number modulus = new Integer(7);4546// difference mode flag47private boolean differenceModeFlag = true;4849// notify flag50private boolean notifyFlag = true;5152// granularity period53private int granularityperiod = 10;5455// derived gauge56private volatile int derivedGauge = 2;5758// flag to notify that a message has been received59private volatile boolean messageReceived = false;6061private volatile Object observedValue = null;6263// MBean class64public class StdObservedObject implements StdObservedObjectMBean {65public Object getNbObjects() {66echo(">>> StdObservedObject.getNbObjects: " + count);67synchronized(CounterMonitorTest.class) {68observedValue = count;69CounterMonitorTest.class.notifyAll();70}71return observedValue;72}73public void setNbObjects(Object n) {74echo(">>> StdObservedObject.setNbObjects: " + n);75count = n;76}77private volatile Object count= null;78}7980// MBean interface81public interface StdObservedObjectMBean {82public Object getNbObjects();83public void setNbObjects(Object n);84}8586// Notification handler87public void handleNotification(Notification notification,88Object handback) {89MonitorNotification n = (MonitorNotification) notification;90echo("\tInside handleNotification...");91String type = n.getType();92try {93if (type.equals(MonitorNotification.THRESHOLD_VALUE_EXCEEDED)) {94echo("\t\t" + n.getObservedAttribute() +95" has reached or exceeded the threshold");96echo("\t\tDerived Gauge = " + n.getDerivedGauge());9798synchronized (this) {99messageReceived = true;100notifyAll();101}102} else {103echo("\t\tSkipping notification of type: " + type);104}105} catch (Exception e) {106echo("\tError in handleNotification!");107e.printStackTrace(System.out);108}109}110111/**112* Update the counter and check for notifications113*/114public void thresholdNotification() throws Exception {115116CounterMonitor counterMonitor = new CounterMonitor();117try {118MBeanServer server = MBeanServerFactory.newMBeanServer();119120String domain = server.getDefaultDomain();121122// Create a new CounterMonitor MBean and add it to the MBeanServer.123//124echo(">>> CREATE a new CounterMonitor MBean");125ObjectName counterMonitorName = new ObjectName(126domain + ":type=" + CounterMonitor.class.getName());127server.registerMBean(counterMonitor, counterMonitorName);128129echo(">>> ADD a listener to the CounterMonitor");130counterMonitor.addNotificationListener(this, null, null);131132//133// MANAGEMENT OF A STANDARD MBEAN134//135136echo(">>> CREATE a new StdObservedObject MBean");137138ObjectName stdObsObjName =139new ObjectName(domain + ":type=StdObservedObject");140StdObservedObject stdObsObj = new StdObservedObject();141server.registerMBean(stdObsObj, stdObsObjName);142143echo(">>> SET the attributes of the CounterMonitor:");144145counterMonitor.addObservedObject(stdObsObjName);146echo("\tATTRIBUTE \"ObservedObject\" = " + stdObsObjName);147148counterMonitor.setObservedAttribute("NbObjects");149echo("\tATTRIBUTE \"ObservedAttribute\" = NbObjects");150151counterMonitor.setNotify(notifyFlag);152echo("\tATTRIBUTE \"Notify\" = " + notifyFlag);153154counterMonitor.setInitThreshold(threshold);155echo("\tATTRIBUTE \"Threshold\" = " + threshold);156157counterMonitor.setGranularityPeriod(granularityperiod);158echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);159160counterMonitor.setModulus(modulus);161echo("\tATTRIBUTE \"Modulus\" = " + modulus);162163counterMonitor.setDifferenceMode(differenceModeFlag);164echo("\tATTRIBUTE \"DifferenceMode\" = " + differenceModeFlag);165166echo(">>> START the CounterMonitor");167counterMonitor.start();168169// Set initial value170//171Integer data = new Integer(0);172echo(">>> Set data = " + data.intValue());173174Attribute attrib = new Attribute("NbObjects", data);175server.setAttribute(stdObsObjName, attrib);176177waitObservation(data);178179// Loop through the values180//181while (derivedGauge++ < 10) {182System.out.print(">>> Set data from " + data.intValue());183data = new Integer(data.intValue() + derivedGauge);184echo(" to " + data.intValue());185186attrib = new Attribute("NbObjects", data);187server.setAttribute(stdObsObjName, attrib);188waitObservation(data);189190echo("\tdoWait in Counter Monitor");191doWait();192193// Check if notification was received194//195if (messageReceived) {196echo("\tOKAY: Notification received");197} else {198echo("\tError: notification missed or not emitted");199throw new IllegalStateException("Notification lost");200}201messageReceived = false;202}203} finally {204counterMonitor.stop();205}206207echo(">>> Bye! Bye!");208}209210/*211* Wait messageReceived to be true212*/213synchronized void doWait() {214while (!messageReceived) {215try {216wait();217} catch (InterruptedException e) {218System.err.println("Got unexpected exception: " + e);219e.printStackTrace();220break;221}222}223}224225private void waitObservation(Object value) {226synchronized (CounterMonitorTest.class) {227while (value != observedValue) {228try {229CounterMonitorTest.class.wait();230} catch (InterruptedException e) {231System.err.println("Got unexpected exception: " + e);232e.printStackTrace();233break;234}235}236}237}238239/*240* Print message241*/242void echo(String message) {243System.out.println(message);244}245246/*247* Standalone entry point.248*249* Run the test and report to stdout.250*/251public static void main (String args[]) throws Exception {252CounterMonitorTest test = new CounterMonitorTest();253test.thresholdNotification();254}255}256257258