0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4981829
0N/A * @summary Test that the counter monitor, when running in difference mode,
0N/A * emits a notification every time the threshold is exceeded.
0N/A * @author Luis-Miguel Alventosa
0N/A * @run clean CounterMonitorTest
0N/A * @run build CounterMonitorTest
0N/A * @run main CounterMonitorTest
0N/A */
0N/A
0N/Aimport javax.management.*;
0N/Aimport javax.management.monitor.*;
0N/A
0N/Apublic class CounterMonitorTest implements NotificationListener {
0N/A
0N/A // threshold number
0N/A private Number threshold = new Integer(2);
0N/A
0N/A // modulus number
0N/A private Number modulus = new Integer(7);
0N/A
0N/A // offset number
0N/A private int offset = 0;
0N/A
0N/A // difference mode flag
0N/A private boolean differenceModeFlag = true;
0N/A
0N/A // notify flag
0N/A private boolean notifyFlag = true;
0N/A
0N/A // granularity period
0N/A private int granularityperiod = 500;
0N/A
0N/A // counter values
0N/A private int[] values = new int[] {4, 6, 9, 11};
0N/A
0N/A // time to wait for notification (in seconds)
0N/A private int timeout = 5;
0N/A
0N/A // flag to notify that a message has been received
0N/A private volatile boolean messageReceived = false;
0N/A
0N/A // MBean class
0N/A public class StdObservedObject implements StdObservedObjectMBean {
0N/A public Object getNbObjects() {
0N/A return count;
0N/A }
0N/A public void setNbObjects(Object n) {
0N/A count = n;
0N/A }
0N/A private Object count= null;
0N/A }
0N/A
0N/A // MBean interface
0N/A public interface StdObservedObjectMBean {
0N/A public Object getNbObjects();
0N/A public void setNbObjects(Object n);
0N/A }
0N/A
0N/A // Notification handler
0N/A public void handleNotification(Notification notification,
0N/A Object handback) {
0N/A MonitorNotification n = (MonitorNotification) notification;
0N/A echo("\tInside handleNotification...");
0N/A String type = n.getType();
0N/A try {
0N/A if (type.equals(MonitorNotification.THRESHOLD_VALUE_EXCEEDED)) {
0N/A echo("\t\t" + n.getObservedAttribute() +
0N/A " has reached or exceeded the threshold");
0N/A echo("\t\tDerived Gauge = " + n.getDerivedGauge());
0N/A messageReceived = true;
0N/A synchronized (this) {
0N/A notifyAll();
0N/A }
0N/A } else {
0N/A echo("\t\tSkipping notification of type: " + type);
0N/A }
0N/A } catch (Exception e) {
0N/A echo("\tError in handleNotification!");
0N/A e.printStackTrace(System.out);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Update the counter and check for notifications
0N/A */
0N/A public void thresholdNotification() throws Exception {
0N/A
0N/A CounterMonitor counterMonitor = new CounterMonitor();
0N/A try {
0N/A MBeanServer server = MBeanServerFactory.newMBeanServer();
0N/A
0N/A String domain = server.getDefaultDomain();
0N/A
0N/A // Create a new CounterMonitor MBean and add it to the MBeanServer.
0N/A //
0N/A echo(">>> CREATE a new CounterMonitor MBean");
0N/A ObjectName counterMonitorName = new ObjectName(
0N/A domain + ":type=" + CounterMonitor.class.getName());
0N/A server.registerMBean(counterMonitor, counterMonitorName);
0N/A
0N/A echo(">>> ADD a listener to the CounterMonitor");
0N/A counterMonitor.addNotificationListener(this, null, null);
0N/A
0N/A //
0N/A // MANAGEMENT OF A STANDARD MBEAN
0N/A //
0N/A
0N/A echo(">>> CREATE a new StdObservedObject MBean");
0N/A
0N/A ObjectName stdObsObjName =
0N/A new ObjectName(domain + ":type=StdObservedObject");
0N/A StdObservedObject stdObsObj = new StdObservedObject();
0N/A server.registerMBean(stdObsObj, stdObsObjName);
0N/A
0N/A echo(">>> SET the attributes of the CounterMonitor:");
0N/A
0N/A counterMonitor.addObservedObject(stdObsObjName);
0N/A echo("\tATTRIBUTE \"ObservedObject\" = " + stdObsObjName);
0N/A
0N/A counterMonitor.setObservedAttribute("NbObjects");
0N/A echo("\tATTRIBUTE \"ObservedAttribute\" = NbObjects");
0N/A
0N/A counterMonitor.setNotify(notifyFlag);
0N/A echo("\tATTRIBUTE \"Notify\" = " + notifyFlag);
0N/A
0N/A counterMonitor.setInitThreshold(threshold);
0N/A echo("\tATTRIBUTE \"Threshold\" = " + threshold);
0N/A
0N/A counterMonitor.setGranularityPeriod(granularityperiod);
0N/A echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
0N/A
0N/A counterMonitor.setModulus(modulus);
0N/A echo("\tATTRIBUTE \"Modulus\" = " + modulus);
0N/A
0N/A counterMonitor.setDifferenceMode(differenceModeFlag);
0N/A echo("\tATTRIBUTE \"DifferenceMode\" = " + differenceModeFlag);
0N/A
0N/A echo(">>> START the CounterMonitor");
0N/A counterMonitor.start();
0N/A
0N/A // Set initial value
0N/A //
0N/A Integer data = new Integer(0);
0N/A echo(">>> Set data = " + data.intValue());
0N/A
0N/A Attribute attrib = new Attribute("NbObjects", data);
0N/A server.setAttribute(stdObsObjName, attrib);
0N/A
0N/A // Wait for granularity period (multiplied by 2 for sure)
0N/A //
0N/A Thread.sleep(granularityperiod * 2);
0N/A
0N/A // Loop through the values
0N/A //
0N/A for (int i = 0; i < values.length; i++) {
0N/A data = new Integer(values[i]);
0N/A echo(">>> Set data = " + data.intValue());
0N/A
0N/A attrib = new Attribute("NbObjects", data);
0N/A server.setAttribute(stdObsObjName, attrib);
0N/A
0N/A echo("\tdoWait in Counter Monitor");
0N/A doWait();
0N/A
0N/A // Check if notification was received
0N/A //
0N/A if (messageReceived) {
0N/A echo("\tOKAY: Notification received");
0N/A } else {
0N/A echo("\tError: notification missed or not emitted");
0N/A throw new IllegalStateException("Notification lost");
0N/A }
0N/A messageReceived = false;
0N/A }
0N/A } finally {
0N/A counterMonitor.stop();
0N/A }
0N/A
0N/A echo(">>> Bye! Bye!");
0N/A }
0N/A
0N/A /*
0N/A * Wait until timeout reached
0N/A */
0N/A void doWait() {
0N/A for (int i = 0; i < timeout; i++) {
0N/A echo("\tdoWait: Waiting for " + timeout + " seconds. " +
0N/A "i = " + i + ", messageReceived = " + messageReceived);
0N/A if (messageReceived) {
0N/A break;
0N/A }
0N/A try {
0N/A synchronized (this) {
0N/A wait(1000);
0N/A }
0N/A } catch (InterruptedException e) {
0N/A // OK: Ignore...
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Print message
0N/A */
0N/A void echo(String message) {
0N/A System.out.println(message);
0N/A }
0N/A
0N/A /*
0N/A * Standalone entry point.
0N/A *
0N/A * Run the test and report to stdout.
0N/A */
0N/A public static void main (String args[]) throws Exception {
0N/A CounterMonitorTest test = new CounterMonitorTest();
0N/A test.thresholdNotification();
0N/A }
0N/A}