Path: blob/master/test/jdk/javax/management/monitor/ReflectionExceptionTest.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 620507226* @key intermittent27* @summary Test that the jmx.monitor.error.runtime monitor notification28* is emitted when getAttribute throws ReflectionException.29* @author Luis-Miguel Alventosa30*31* @run clean ReflectionExceptionTest MBeanServerBuilderImpl32* MBeanServerForwarderInvocationHandler33* @run build ReflectionExceptionTest MBeanServerBuilderImpl34* MBeanServerForwarderInvocationHandler35* @run main ReflectionExceptionTest36*/3738import java.lang.reflect.Proxy;39import javax.management.MBeanServer;40import javax.management.MBeanServerFactory;41import javax.management.Notification;42import javax.management.NotificationListener;43import javax.management.ObjectName;44import javax.management.ReflectionException;45import javax.management.monitor.CounterMonitor;46import javax.management.monitor.GaugeMonitor;47import javax.management.monitor.MonitorNotification;48import javax.management.monitor.StringMonitor;4950public class ReflectionExceptionTest implements NotificationListener {5152// MBean class53public class ObservedObject implements ObservedObjectMBean {54public Integer getIntegerAttribute() {55return i;56}57public void setIntegerAttribute(Integer i) {58this.i = i;59}60public String getStringAttribute() {61return s;62}63public void setStringAttribute(String s) {64this.s = s;65}66private Integer i = 1;67private String s = "dummy";68}6970// MBean interface71public interface ObservedObjectMBean {72public Integer getIntegerAttribute();73public void setIntegerAttribute(Integer i);74public String getStringAttribute();75public void setStringAttribute(String s);76}7778// Notification handler79public void handleNotification(Notification notification, Object handback) {80echo(">>> Received notification: " + notification);81if (notification instanceof MonitorNotification) {82String type = notification.getType();83if (type.equals(MonitorNotification.RUNTIME_ERROR)) {84MonitorNotification mn = (MonitorNotification) notification;85echo("\tType: " + mn.getType());86echo("\tTimeStamp: " + mn.getTimeStamp());87echo("\tObservedObject: " + mn.getObservedObject());88echo("\tObservedAttribute: " + mn.getObservedAttribute());89echo("\tDerivedGauge: " + mn.getDerivedGauge());90echo("\tTrigger: " + mn.getTrigger());9192synchronized (this) {93messageReceived = true;94notifyAll();95}96}97}98}99100/**101* Update the counter and check for notifications102*/103public int counterMonitorNotification() throws Exception {104105CounterMonitor counterMonitor = new CounterMonitor();106try {107// Create a new CounterMonitor MBean and add it to the MBeanServer.108//109echo(">>> CREATE a new CounterMonitor MBean");110ObjectName counterMonitorName = new ObjectName(111domain + ":type=" + CounterMonitor.class.getName());112server.registerMBean(counterMonitor, counterMonitorName);113114echo(">>> ADD a listener to the CounterMonitor");115counterMonitor.addNotificationListener(this, null, null);116117//118// MANAGEMENT OF A STANDARD MBEAN119//120121echo(">>> SET the attributes of the CounterMonitor:");122123counterMonitor.addObservedObject(obsObjName);124echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);125126counterMonitor.setObservedAttribute("IntegerAttribute");127echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");128129counterMonitor.setNotify(false);130echo("\tATTRIBUTE \"NotifyFlag\" = false");131132Integer threshold = 2;133counterMonitor.setInitThreshold(threshold);134echo("\tATTRIBUTE \"Threshold\" = " + threshold);135136int granularityperiod = 500;137counterMonitor.setGranularityPeriod(granularityperiod);138echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);139140echo(">>> START the CounterMonitor");141counterMonitor.start();142143// Check if notification was received144//145doWait();146if (messageReceived) {147echo("\tOK: CounterMonitor got RUNTIME_ERROR notification!");148} else {149echo("\tKO: CounterMonitor did not get " +150"RUNTIME_ERROR notification!");151return 1;152}153} finally {154messageReceived = false;155if (counterMonitor != null)156counterMonitor.stop();157}158159return 0;160}161162/**163* Update the gauge and check for notifications164*/165public int gaugeMonitorNotification() throws Exception {166167GaugeMonitor gaugeMonitor = new GaugeMonitor();168try {169// Create a new GaugeMonitor MBean and add it to the MBeanServer.170//171echo(">>> CREATE a new GaugeMonitor MBean");172ObjectName gaugeMonitorName = new ObjectName(173domain + ":type=" + GaugeMonitor.class.getName());174server.registerMBean(gaugeMonitor, gaugeMonitorName);175176echo(">>> ADD a listener to the GaugeMonitor");177gaugeMonitor.addNotificationListener(this, null, null);178179//180// MANAGEMENT OF A STANDARD MBEAN181//182183echo(">>> SET the attributes of the GaugeMonitor:");184185gaugeMonitor.addObservedObject(obsObjName);186echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);187188gaugeMonitor.setObservedAttribute("IntegerAttribute");189echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");190191gaugeMonitor.setNotifyLow(false);192gaugeMonitor.setNotifyHigh(false);193echo("\tATTRIBUTE \"Notify Low Flag\" = false");194echo("\tATTRIBUTE \"Notify High Flag\" = false");195196Integer highThreshold = 3, lowThreshold = 2;197gaugeMonitor.setThresholds(highThreshold, lowThreshold);198echo("\tATTRIBUTE \"Low Threshold\" = " + lowThreshold);199echo("\tATTRIBUTE \"High Threshold\" = " + highThreshold);200201int granularityperiod = 500;202gaugeMonitor.setGranularityPeriod(granularityperiod);203echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);204205echo(">>> START the GaugeMonitor");206gaugeMonitor.start();207208// Check if notification was received209//210doWait();211if (messageReceived) {212echo("\tOK: GaugeMonitor got RUNTIME_ERROR notification!");213} else {214echo("\tKO: GaugeMonitor did not get " +215"RUNTIME_ERROR notification!");216return 1;217}218} finally {219messageReceived = false;220if (gaugeMonitor != null)221gaugeMonitor.stop();222}223224return 0;225}226227/**228* Update the string and check for notifications229*/230public int stringMonitorNotification() throws Exception {231232StringMonitor stringMonitor = new StringMonitor();233try {234// Create a new StringMonitor MBean and add it to the MBeanServer.235//236echo(">>> CREATE a new StringMonitor MBean");237ObjectName stringMonitorName = new ObjectName(238domain + ":type=" + StringMonitor.class.getName());239server.registerMBean(stringMonitor, stringMonitorName);240241echo(">>> ADD a listener to the StringMonitor");242stringMonitor.addNotificationListener(this, null, null);243244//245// MANAGEMENT OF A STANDARD MBEAN246//247248echo(">>> SET the attributes of the StringMonitor:");249250stringMonitor.addObservedObject(obsObjName);251echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);252253stringMonitor.setObservedAttribute("StringAttribute");254echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");255256stringMonitor.setNotifyMatch(false);257echo("\tATTRIBUTE \"NotifyMatch\" = false");258259stringMonitor.setNotifyDiffer(false);260echo("\tATTRIBUTE \"NotifyDiffer\" = false");261262stringMonitor.setStringToCompare("dummy");263echo("\tATTRIBUTE \"StringToCompare\" = \"dummy\"");264265int granularityperiod = 500;266stringMonitor.setGranularityPeriod(granularityperiod);267echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);268269echo(">>> START the StringMonitor");270stringMonitor.start();271272// Check if notification was received273//274doWait();275if (messageReceived) {276echo("\tOK: StringMonitor got RUNTIME_ERROR notification!");277} else {278echo("\tKO: StringMonitor did not get " +279"RUNTIME_ERROR notification!");280return 1;281}282} finally {283messageReceived = false;284if (stringMonitor != null)285stringMonitor.stop();286}287288return 0;289}290291/**292* Test the monitor notifications.293*/294public int monitorNotifications() throws Exception {295296server = MBeanServerFactory.newMBeanServer();297298MBeanServerForwarderInvocationHandler mbsfih =299(MBeanServerForwarderInvocationHandler)300Proxy.getInvocationHandler(server);301302mbsfih.setGetAttributeException(303new ReflectionException(new RuntimeException(),304"Test ReflectionException"));305306domain = server.getDefaultDomain();307308obsObjName = ObjectName.getInstance(domain + ":type=ObservedObject");309server.registerMBean(new ObservedObject(), obsObjName);310311echo(">>> ----------------------------------------");312int error = counterMonitorNotification();313echo(">>> ----------------------------------------");314error += gaugeMonitorNotification();315echo(">>> ----------------------------------------");316error += stringMonitorNotification();317echo(">>> ----------------------------------------");318return error;319}320321/*322* Print message323*/324private static void echo(String message) {325System.out.println(message);326}327328/*329* Standalone entry point.330*331* Run the test and report to stdout.332*/333public static void main (String args[]) throws Exception {334System.setProperty("javax.management.builder.initial",335MBeanServerBuilderImpl.class.getName());336ReflectionExceptionTest test = new ReflectionExceptionTest();337int error = test.monitorNotifications();338if (error > 0) {339echo(">>> Unhappy Bye, Bye!");340throw new IllegalStateException("Test FAILED: Didn't get all " +341"the notifications that were " +342"expected by the test!");343} else {344echo(">>> Happy Bye, Bye!");345}346}347348/*349* Wait messageReceived to be true350*/351synchronized void doWait() {352while (!messageReceived) {353try {354wait();355} catch (InterruptedException e) {356System.err.println("Got unexpected exception: " + e);357e.printStackTrace();358break;359}360}361}362363// Flag to notify that a message has been received364private volatile boolean messageReceived = false;365366private MBeanServer server;367private ObjectName obsObjName;368private String domain;369}370371372