Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/monitor/RuntimeExceptionTest.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 620039126* @summary Test that the jmx.monitor.error.runtime monitor notification27* is emitted when getAttribute throws RuntimeException.28* @author Luis-Miguel Alventosa29* @run clean RuntimeExceptionTest MBeanServerBuilderImpl30* MBeanServerForwarderInvocationHandler31* @run build RuntimeExceptionTest MBeanServerBuilderImpl32* MBeanServerForwarderInvocationHandler33* @run main RuntimeExceptionTest34*/3536import java.lang.reflect.Proxy;37import javax.management.MBeanServer;38import javax.management.MBeanServerFactory;39import javax.management.Notification;40import javax.management.NotificationListener;41import javax.management.ObjectName;42import javax.management.monitor.CounterMonitor;43import javax.management.monitor.GaugeMonitor;44import javax.management.monitor.MonitorNotification;45import javax.management.monitor.StringMonitor;4647public class RuntimeExceptionTest implements NotificationListener {4849// MBean class50public class ObservedObject implements ObservedObjectMBean {51public Integer getIntegerAttribute() {52return i;53}54public void setIntegerAttribute(Integer i) {55this.i = i;56}57public String getStringAttribute() {58return s;59}60public void setStringAttribute(String s) {61this.s = s;62}63private Integer i = 1;64private String s = "dummy";65}6667// MBean interface68public interface ObservedObjectMBean {69public Integer getIntegerAttribute();70public void setIntegerAttribute(Integer i);71public String getStringAttribute();72public void setStringAttribute(String s);73}7475// Notification handler76public void handleNotification(Notification notification, Object handback) {77echo(">>> Received notification: " + notification);78if (notification instanceof MonitorNotification) {79String type = notification.getType();80if (type.equals(MonitorNotification.RUNTIME_ERROR)) {81MonitorNotification mn = (MonitorNotification) notification;82echo("\tType: " + mn.getType());83echo("\tTimeStamp: " + mn.getTimeStamp());84echo("\tObservedObject: " + mn.getObservedObject());85echo("\tObservedAttribute: " + mn.getObservedAttribute());86echo("\tDerivedGauge: " + mn.getDerivedGauge());87echo("\tTrigger: " + mn.getTrigger());8889synchronized (this) {90messageReceived = true;91notifyAll();92}93}94}95}9697/**98* Update the counter and check for notifications99*/100public int counterMonitorNotification() throws Exception {101102CounterMonitor counterMonitor = new CounterMonitor();103try {104// Create a new CounterMonitor MBean and add it to the MBeanServer.105//106echo(">>> CREATE a new CounterMonitor MBean");107ObjectName counterMonitorName = new ObjectName(108domain + ":type=" + CounterMonitor.class.getName());109server.registerMBean(counterMonitor, counterMonitorName);110111echo(">>> ADD a listener to the CounterMonitor");112counterMonitor.addNotificationListener(this, null, null);113114//115// MANAGEMENT OF A STANDARD MBEAN116//117118echo(">>> SET the attributes of the CounterMonitor:");119120counterMonitor.addObservedObject(obsObjName);121echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);122123counterMonitor.setObservedAttribute("IntegerAttribute");124echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");125126counterMonitor.setNotify(false);127echo("\tATTRIBUTE \"NotifyFlag\" = false");128129Integer threshold = 2;130counterMonitor.setInitThreshold(threshold);131echo("\tATTRIBUTE \"Threshold\" = " + threshold);132133int granularityperiod = 500;134counterMonitor.setGranularityPeriod(granularityperiod);135echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);136137echo(">>> START the CounterMonitor");138counterMonitor.start();139140// Check if notification was received141//142doWait();143if (messageReceived) {144echo("\tOK: CounterMonitor got RUNTIME_ERROR notification!");145} else {146echo("\tKO: CounterMonitor did not get " +147"RUNTIME_ERROR notification!");148return 1;149}150} finally {151messageReceived = false;152if (counterMonitor != null)153counterMonitor.stop();154}155156return 0;157}158159/**160* Update the gauge and check for notifications161*/162public int gaugeMonitorNotification() throws Exception {163164GaugeMonitor gaugeMonitor = new GaugeMonitor();165try {166// Create a new GaugeMonitor MBean and add it to the MBeanServer.167//168echo(">>> CREATE a new GaugeMonitor MBean");169ObjectName gaugeMonitorName = new ObjectName(170domain + ":type=" + GaugeMonitor.class.getName());171server.registerMBean(gaugeMonitor, gaugeMonitorName);172173echo(">>> ADD a listener to the GaugeMonitor");174gaugeMonitor.addNotificationListener(this, null, null);175176//177// MANAGEMENT OF A STANDARD MBEAN178//179180echo(">>> SET the attributes of the GaugeMonitor:");181182gaugeMonitor.addObservedObject(obsObjName);183echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);184185gaugeMonitor.setObservedAttribute("IntegerAttribute");186echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");187188gaugeMonitor.setNotifyLow(false);189gaugeMonitor.setNotifyHigh(false);190echo("\tATTRIBUTE \"Notify Low Flag\" = false");191echo("\tATTRIBUTE \"Notify High Flag\" = false");192193Integer highThreshold = 3, lowThreshold = 2;194gaugeMonitor.setThresholds(highThreshold, lowThreshold);195echo("\tATTRIBUTE \"Low Threshold\" = " + lowThreshold);196echo("\tATTRIBUTE \"High Threshold\" = " + highThreshold);197198int granularityperiod = 500;199gaugeMonitor.setGranularityPeriod(granularityperiod);200echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);201202echo(">>> START the GaugeMonitor");203gaugeMonitor.start();204205// Check if notification was received206//207doWait();208if (messageReceived) {209echo("\tOK: GaugeMonitor got RUNTIME_ERROR notification!");210} else {211echo("\tKO: GaugeMonitor did not get " +212"RUNTIME_ERROR notification!");213return 1;214}215} finally {216messageReceived = false;217if (gaugeMonitor != null)218gaugeMonitor.stop();219}220221return 0;222}223224/**225* Update the string and check for notifications226*/227public int stringMonitorNotification() throws Exception {228229StringMonitor stringMonitor = new StringMonitor();230try {231// Create a new StringMonitor MBean and add it to the MBeanServer.232//233echo(">>> CREATE a new StringMonitor MBean");234ObjectName stringMonitorName = new ObjectName(235domain + ":type=" + StringMonitor.class.getName());236server.registerMBean(stringMonitor, stringMonitorName);237238echo(">>> ADD a listener to the StringMonitor");239stringMonitor.addNotificationListener(this, null, null);240241//242// MANAGEMENT OF A STANDARD MBEAN243//244245echo(">>> SET the attributes of the StringMonitor:");246247stringMonitor.addObservedObject(obsObjName);248echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);249250stringMonitor.setObservedAttribute("StringAttribute");251echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");252253stringMonitor.setNotifyMatch(false);254echo("\tATTRIBUTE \"NotifyMatch\" = false");255256stringMonitor.setNotifyDiffer(false);257echo("\tATTRIBUTE \"NotifyDiffer\" = false");258259stringMonitor.setStringToCompare("dummy");260echo("\tATTRIBUTE \"StringToCompare\" = \"dummy\"");261262int granularityperiod = 500;263stringMonitor.setGranularityPeriod(granularityperiod);264echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);265266echo(">>> START the StringMonitor");267stringMonitor.start();268269// Check if notification was received270//271doWait();272if (messageReceived) {273echo("\tOK: StringMonitor got RUNTIME_ERROR notification!");274} else {275echo("\tKO: StringMonitor did not get " +276"RUNTIME_ERROR notification!");277return 1;278}279} finally {280messageReceived = false;281if (stringMonitor != null)282stringMonitor.stop();283}284285return 0;286}287288/**289* Test the monitor notifications.290*/291public int monitorNotifications() throws Exception {292293server = MBeanServerFactory.newMBeanServer();294295MBeanServerForwarderInvocationHandler mbsfih =296(MBeanServerForwarderInvocationHandler)297Proxy.getInvocationHandler(server);298299mbsfih.setGetAttributeException(300new RuntimeException("Test RuntimeException"));301302domain = server.getDefaultDomain();303304obsObjName = ObjectName.getInstance(domain + ":type=ObservedObject");305server.registerMBean(new ObservedObject(), obsObjName);306307echo(">>> ----------------------------------------");308int error = counterMonitorNotification();309echo(">>> ----------------------------------------");310error += gaugeMonitorNotification();311echo(">>> ----------------------------------------");312error += stringMonitorNotification();313echo(">>> ----------------------------------------");314return error;315}316317/*318* Print message319*/320private static void echo(String message) {321System.out.println(message);322}323324/*325* Standalone entry point.326*327* Run the test and report to stdout.328*/329public static void main (String args[]) throws Exception {330System.setProperty("javax.management.builder.initial",331MBeanServerBuilderImpl.class.getName());332RuntimeExceptionTest test = new RuntimeExceptionTest();333int error = test.monitorNotifications();334if (error > 0) {335echo(">>> Unhappy Bye, Bye!");336throw new IllegalStateException("Test FAILED: Didn't get all " +337"the notifications that were " +338"expected by the test!");339} else {340echo(">>> Happy Bye, Bye!");341}342}343344/*345* Wait messageReceived to be true346*/347synchronized void doWait() {348while (!messageReceived) {349try {350wait();351} catch (InterruptedException e) {352System.err.println("Got unexpected exception: " + e);353e.printStackTrace();354break;355}356}357}358359// Flag to notify that a message has been received360private volatile boolean messageReceived = false;361362private MBeanServer server;363private ObjectName obsObjName;364private String domain;365}366367368