Path: blob/master/test/jdk/javax/management/monitor/RuntimeExceptionTest.java
41149 views
/*1* Copyright (c) 2005, 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 620039126* @summary Test that the jmx.monitor.error.runtime monitor notification27* is emitted when getAttribute throws RuntimeException.28* @author Luis-Miguel Alventosa29*30* @run clean RuntimeExceptionTest MBeanServerBuilderImpl31* MBeanServerForwarderInvocationHandler32* @run build RuntimeExceptionTest MBeanServerBuilderImpl33* MBeanServerForwarderInvocationHandler34* @run main RuntimeExceptionTest35*/3637import java.lang.reflect.Proxy;38import javax.management.MBeanServer;39import javax.management.MBeanServerFactory;40import javax.management.Notification;41import javax.management.NotificationListener;42import javax.management.ObjectName;43import javax.management.monitor.CounterMonitor;44import javax.management.monitor.GaugeMonitor;45import javax.management.monitor.MonitorNotification;46import javax.management.monitor.StringMonitor;4748public class RuntimeExceptionTest implements NotificationListener {4950// MBean class51public class ObservedObject implements ObservedObjectMBean {52public Integer getIntegerAttribute() {53return i;54}55public void setIntegerAttribute(Integer i) {56this.i = i;57}58public String getStringAttribute() {59return s;60}61public void setStringAttribute(String s) {62this.s = s;63}64private Integer i = 1;65private String s = "dummy";66}6768// MBean interface69public interface ObservedObjectMBean {70public Integer getIntegerAttribute();71public void setIntegerAttribute(Integer i);72public String getStringAttribute();73public void setStringAttribute(String s);74}7576// Notification handler77public void handleNotification(Notification notification, Object handback) {78echo(">>> Received notification: " + notification);79if (notification instanceof MonitorNotification) {80String type = notification.getType();81if (type.equals(MonitorNotification.RUNTIME_ERROR)) {82MonitorNotification mn = (MonitorNotification) notification;83echo("\tType: " + mn.getType());84echo("\tTimeStamp: " + mn.getTimeStamp());85echo("\tObservedObject: " + mn.getObservedObject());86echo("\tObservedAttribute: " + mn.getObservedAttribute());87echo("\tDerivedGauge: " + mn.getDerivedGauge());88echo("\tTrigger: " + mn.getTrigger());8990synchronized (this) {91messageReceived = true;92notifyAll();93}94}95}96}9798/**99* Update the counter and check for notifications100*/101public int counterMonitorNotification() throws Exception {102103CounterMonitor counterMonitor = new CounterMonitor();104try {105// Create a new CounterMonitor MBean and add it to the MBeanServer.106//107echo(">>> CREATE a new CounterMonitor MBean");108ObjectName counterMonitorName = new ObjectName(109domain + ":type=" + CounterMonitor.class.getName());110server.registerMBean(counterMonitor, counterMonitorName);111112echo(">>> ADD a listener to the CounterMonitor");113counterMonitor.addNotificationListener(this, null, null);114115//116// MANAGEMENT OF A STANDARD MBEAN117//118119echo(">>> SET the attributes of the CounterMonitor:");120121counterMonitor.addObservedObject(obsObjName);122echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);123124counterMonitor.setObservedAttribute("IntegerAttribute");125echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");126127counterMonitor.setNotify(false);128echo("\tATTRIBUTE \"NotifyFlag\" = false");129130Integer threshold = 2;131counterMonitor.setInitThreshold(threshold);132echo("\tATTRIBUTE \"Threshold\" = " + threshold);133134int granularityperiod = 500;135counterMonitor.setGranularityPeriod(granularityperiod);136echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);137138echo(">>> START the CounterMonitor");139counterMonitor.start();140141// Check if notification was received142//143doWait();144if (messageReceived) {145echo("\tOK: CounterMonitor got RUNTIME_ERROR notification!");146} else {147echo("\tKO: CounterMonitor did not get " +148"RUNTIME_ERROR notification!");149return 1;150}151} finally {152messageReceived = false;153if (counterMonitor != null)154counterMonitor.stop();155}156157return 0;158}159160/**161* Update the gauge and check for notifications162*/163public int gaugeMonitorNotification() throws Exception {164165GaugeMonitor gaugeMonitor = new GaugeMonitor();166try {167// Create a new GaugeMonitor MBean and add it to the MBeanServer.168//169echo(">>> CREATE a new GaugeMonitor MBean");170ObjectName gaugeMonitorName = new ObjectName(171domain + ":type=" + GaugeMonitor.class.getName());172server.registerMBean(gaugeMonitor, gaugeMonitorName);173174echo(">>> ADD a listener to the GaugeMonitor");175gaugeMonitor.addNotificationListener(this, null, null);176177//178// MANAGEMENT OF A STANDARD MBEAN179//180181echo(">>> SET the attributes of the GaugeMonitor:");182183gaugeMonitor.addObservedObject(obsObjName);184echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);185186gaugeMonitor.setObservedAttribute("IntegerAttribute");187echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");188189gaugeMonitor.setNotifyLow(false);190gaugeMonitor.setNotifyHigh(false);191echo("\tATTRIBUTE \"Notify Low Flag\" = false");192echo("\tATTRIBUTE \"Notify High Flag\" = false");193194Integer highThreshold = 3, lowThreshold = 2;195gaugeMonitor.setThresholds(highThreshold, lowThreshold);196echo("\tATTRIBUTE \"Low Threshold\" = " + lowThreshold);197echo("\tATTRIBUTE \"High Threshold\" = " + highThreshold);198199int granularityperiod = 500;200gaugeMonitor.setGranularityPeriod(granularityperiod);201echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);202203echo(">>> START the GaugeMonitor");204gaugeMonitor.start();205206// Check if notification was received207//208doWait();209if (messageReceived) {210echo("\tOK: GaugeMonitor got RUNTIME_ERROR notification!");211} else {212echo("\tKO: GaugeMonitor did not get " +213"RUNTIME_ERROR notification!");214return 1;215}216} finally {217messageReceived = false;218if (gaugeMonitor != null)219gaugeMonitor.stop();220}221222return 0;223}224225/**226* Update the string and check for notifications227*/228public int stringMonitorNotification() throws Exception {229230StringMonitor stringMonitor = new StringMonitor();231try {232// Create a new StringMonitor MBean and add it to the MBeanServer.233//234echo(">>> CREATE a new StringMonitor MBean");235ObjectName stringMonitorName = new ObjectName(236domain + ":type=" + StringMonitor.class.getName());237server.registerMBean(stringMonitor, stringMonitorName);238239echo(">>> ADD a listener to the StringMonitor");240stringMonitor.addNotificationListener(this, null, null);241242//243// MANAGEMENT OF A STANDARD MBEAN244//245246echo(">>> SET the attributes of the StringMonitor:");247248stringMonitor.addObservedObject(obsObjName);249echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);250251stringMonitor.setObservedAttribute("StringAttribute");252echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");253254stringMonitor.setNotifyMatch(false);255echo("\tATTRIBUTE \"NotifyMatch\" = false");256257stringMonitor.setNotifyDiffer(false);258echo("\tATTRIBUTE \"NotifyDiffer\" = false");259260stringMonitor.setStringToCompare("dummy");261echo("\tATTRIBUTE \"StringToCompare\" = \"dummy\"");262263int granularityperiod = 500;264stringMonitor.setGranularityPeriod(granularityperiod);265echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);266267echo(">>> START the StringMonitor");268stringMonitor.start();269270// Check if notification was received271//272doWait();273if (messageReceived) {274echo("\tOK: StringMonitor got RUNTIME_ERROR notification!");275} else {276echo("\tKO: StringMonitor did not get " +277"RUNTIME_ERROR notification!");278return 1;279}280} finally {281messageReceived = false;282if (stringMonitor != null)283stringMonitor.stop();284}285286return 0;287}288289/**290* Test the monitor notifications.291*/292public int monitorNotifications() throws Exception {293294server = MBeanServerFactory.newMBeanServer();295296MBeanServerForwarderInvocationHandler mbsfih =297(MBeanServerForwarderInvocationHandler)298Proxy.getInvocationHandler(server);299300mbsfih.setGetAttributeException(301new RuntimeException("Test RuntimeException"));302303domain = server.getDefaultDomain();304305obsObjName = ObjectName.getInstance(domain + ":type=ObservedObject");306server.registerMBean(new ObservedObject(), obsObjName);307308echo(">>> ----------------------------------------");309int error = counterMonitorNotification();310echo(">>> ----------------------------------------");311error += gaugeMonitorNotification();312echo(">>> ----------------------------------------");313error += stringMonitorNotification();314echo(">>> ----------------------------------------");315return error;316}317318/*319* Print message320*/321private static void echo(String message) {322System.out.println(message);323}324325/*326* Standalone entry point.327*328* Run the test and report to stdout.329*/330public static void main (String args[]) throws Exception {331System.setProperty("javax.management.builder.initial",332MBeanServerBuilderImpl.class.getName());333RuntimeExceptionTest test = new RuntimeExceptionTest();334int error = test.monitorNotifications();335if (error > 0) {336echo(">>> Unhappy Bye, Bye!");337throw new IllegalStateException("Test FAILED: Didn't get all " +338"the notifications that were " +339"expected by the test!");340} else {341echo(">>> Happy Bye, Bye!");342}343}344345/*346* Wait messageReceived to be true347*/348synchronized void doWait() {349while (!messageReceived) {350try {351wait();352} catch (InterruptedException e) {353System.err.println("Got unexpected exception: " + e);354e.printStackTrace();355break;356}357}358}359360// Flag to notify that a message has been received361private volatile boolean messageReceived = false;362363private MBeanServer server;364private ObjectName obsObjName;365private String domain;366}367368369