Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/proxy/NotificationEmitterProxy.java
38839 views
/*1* Copyright (c) 2006, 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* @summary Test that we can create proxies which are NotificationEmitters.26* @bug 641174727* @author Daniel Fuchs28* @run clean NotificationEmitterProxy29* @run build NotificationEmitterProxy30* @run main NotificationEmitterProxy31*/3233import java.lang.management.ManagementFactory;3435import javax.management.*;36import javax.management.remote.*;37import javax.naming.NoPermissionException;3839public class NotificationEmitterProxy {4041public static class Counter {42int count;43public synchronized int count() {44count++;45notifyAll();46return count;47}48public synchronized int peek() {49return count;50}51public synchronized int waitfor(int max, long timeout)52throws InterruptedException {53final long start = System.currentTimeMillis();54while (count < max && timeout > 0) {55final long rest = timeout -56(System.currentTimeMillis() - start);57if (rest <= 0) break;58wait(rest);59}60return count;61}62}6364public static class CounterListener65implements NotificationListener {66final private Counter counter;67public CounterListener(Counter counter) {68this.counter = counter;69}70public void handleNotification(Notification notification,71Object handback) {72System.out.println("Received notif from " + handback +73":\n\t" + notification);74counter.count();75}76}7778public static void main(String[] args) throws Exception {79System.out.println("<<< Register for notification from a proxy");8081MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();82final ObjectName name = new ObjectName(":class=Simple");8384JMXServiceURL url = new JMXServiceURL("rmi", null, 0);85final JMXConnectorServer server =86JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);87server.start();88url = server.getAddress();8990final JMXConnector client = JMXConnectorFactory.connect(url);9192final Counter counter = new Counter();93final CounterListener listener = new CounterListener(counter);94final Counter mxcounter = new Counter();95final CounterListener mxlistener = new CounterListener(mxcounter);96final NotificationFilterSupport filter =97new NotificationFilterSupport();98filter.enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION);99filter.enableType(MBeanServerNotification.UNREGISTRATION_NOTIFICATION);100int registered = 0;101try {102final MBeanServerDelegateMBean delegate =103JMX.newMBeanProxy(client.getMBeanServerConnection(),104MBeanServerDelegate.DELEGATE_NAME,105MBeanServerDelegateMBean.class,106true);107108NotificationEmitter emitter = (NotificationEmitter)delegate;109emitter.addNotificationListener(listener,filter,"JMX.newMBeanProxy");110} catch (Exception x) {111throw new RuntimeException("Failed to register listener with "+112" JMX.newMBeanProxy: " + x, x);113}114115try {116final MBeanServerDelegateMBean delegate =117MBeanServerInvocationHandler.newProxyInstance(mbs,118MBeanServerDelegate.DELEGATE_NAME,119MBeanServerDelegateMBean.class,120true);121122NotificationEmitter emitter = (NotificationEmitter)delegate;123emitter.addNotificationListener(listener,filter,124"MBeanServerInvocationHandler.newProxyInstance");125} catch (Exception x) {126throw new RuntimeException("Failed to register listener with "+127" MBeanServerInvocationHandler.newProxyInstance: " + x, x);128}129130System.out.println("<<< Register an MBean.");131132final Simple simple = new Simple();133mbs.registerMBean(simple, name);134registered++;135136SimpleMXBean simple0 =137JMX.newMXBeanProxy(client.getMBeanServerConnection(),138name,139SimpleMXBean.class,140true);141142SimpleMXBean simple1 =143JMX.newMXBeanProxy(mbs,144name,145SimpleMXBean.class,146false);147148final int expected = 2*registered;149final int reg = counter.waitfor(expected,3000);150if (reg != expected)151throw new RuntimeException("Bad notification count: " + reg +152", expected " +expected);153System.out.println("Received expected "+reg+154" notifs after registerMBean");155156((NotificationEmitter)simple0)157.addNotificationListener(mxlistener,null,name);158simple1.equals("Are you a Wombat?");159final int mxnotifs = mxcounter.waitfor(1,3000);160if (mxnotifs != 1)161throw new RuntimeException("Bad MXBean notification count: " +162mxnotifs);163System.out.println("Received expected "+mxnotifs+164" notifs from MXBean");165166mbs.unregisterMBean(name);167final int unreg = counter.waitfor(expected+reg,3000);168if (unreg != (expected+reg))169throw new RuntimeException("Bad notification count: " + unreg +170", expected " +expected+reg);171System.out.println("Received expected "+(unreg-reg)+172" notifs after unregisterMBean");173System.out.println("Total notifs received: " + unreg);174175176}177178public static interface Simplest {179180}181182public static interface SimpleMXBean extends Simplest {183public String equals(String x);184}185186private static class Simple extends NotificationBroadcasterSupport187implements SimpleMXBean {188public static final String NOTIF_TYPE = "simple.equals";189private static long seq=0;190private static synchronized long seq() { return ++seq; };191public String equals(String x) {192sendNotification(new Notification(NOTIF_TYPE,this,seq(),x));193return x;194}195196}197198199200}201202203