Path: blob/master/test/jdk/javax/management/proxy/NotificationEmitterProxy.java
41149 views
/*1* Copyright (c) 2006, 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* @summary Test that we can create proxies which are NotificationEmitters.26* @bug 641174727* @author Daniel Fuchs28*29* @run clean NotificationEmitterProxy30* @run build NotificationEmitterProxy31* @run main NotificationEmitterProxy32*/3334import java.lang.management.ManagementFactory;3536import javax.management.*;37import javax.management.remote.*;38import javax.naming.NoPermissionException;3940public class NotificationEmitterProxy {4142public static class Counter {43int count;44public synchronized int count() {45count++;46notifyAll();47return count;48}49public synchronized int peek() {50return count;51}52public synchronized int waitfor(int max, long timeout)53throws InterruptedException {54final long start = System.currentTimeMillis();55while (count < max && timeout > 0) {56final long rest = timeout -57(System.currentTimeMillis() - start);58if (rest <= 0) break;59wait(rest);60}61return count;62}63}6465public static class CounterListener66implements NotificationListener {67final private Counter counter;68public CounterListener(Counter counter) {69this.counter = counter;70}71public void handleNotification(Notification notification,72Object handback) {73System.out.println("Received notif from " + handback +74":\n\t" + notification);75counter.count();76}77}7879public static void main(String[] args) throws Exception {80System.out.println("<<< Register for notification from a proxy");8182MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();83final ObjectName name = new ObjectName(":class=Simple");8485JMXServiceURL url = new JMXServiceURL("rmi", null, 0);86final JMXConnectorServer server =87JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);88server.start();89url = server.getAddress();9091final JMXConnector client = JMXConnectorFactory.connect(url);9293final Counter counter = new Counter();94final CounterListener listener = new CounterListener(counter);95final Counter mxcounter = new Counter();96final CounterListener mxlistener = new CounterListener(mxcounter);97final NotificationFilterSupport filter =98new NotificationFilterSupport();99filter.enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION);100filter.enableType(MBeanServerNotification.UNREGISTRATION_NOTIFICATION);101int registered = 0;102try {103final MBeanServerDelegateMBean delegate =104JMX.newMBeanProxy(client.getMBeanServerConnection(),105MBeanServerDelegate.DELEGATE_NAME,106MBeanServerDelegateMBean.class,107true);108109NotificationEmitter emitter = (NotificationEmitter)delegate;110emitter.addNotificationListener(listener,filter,"JMX.newMBeanProxy");111} catch (Exception x) {112throw new RuntimeException("Failed to register listener with "+113" JMX.newMBeanProxy: " + x, x);114}115116try {117final MBeanServerDelegateMBean delegate =118MBeanServerInvocationHandler.newProxyInstance(mbs,119MBeanServerDelegate.DELEGATE_NAME,120MBeanServerDelegateMBean.class,121true);122123NotificationEmitter emitter = (NotificationEmitter)delegate;124emitter.addNotificationListener(listener,filter,125"MBeanServerInvocationHandler.newProxyInstance");126} catch (Exception x) {127throw new RuntimeException("Failed to register listener with "+128" MBeanServerInvocationHandler.newProxyInstance: " + x, x);129}130131System.out.println("<<< Register an MBean.");132133final Simple simple = new Simple();134mbs.registerMBean(simple, name);135registered++;136137SimpleMXBean simple0 =138JMX.newMXBeanProxy(client.getMBeanServerConnection(),139name,140SimpleMXBean.class,141true);142143SimpleMXBean simple1 =144JMX.newMXBeanProxy(mbs,145name,146SimpleMXBean.class,147false);148149final int expected = 2*registered;150final int reg = counter.waitfor(expected,3000);151if (reg != expected)152throw new RuntimeException("Bad notification count: " + reg +153", expected " +expected);154System.out.println("Received expected "+reg+155" notifs after registerMBean");156157((NotificationEmitter)simple0)158.addNotificationListener(mxlistener,null,name);159simple1.equals("Are you a Wombat?");160final int mxnotifs = mxcounter.waitfor(1,3000);161if (mxnotifs != 1)162throw new RuntimeException("Bad MXBean notification count: " +163mxnotifs);164System.out.println("Received expected "+mxnotifs+165" notifs from MXBean");166167mbs.unregisterMBean(name);168final int unreg = counter.waitfor(expected+reg,3000);169if (unreg != (expected+reg))170throw new RuntimeException("Bad notification count: " + unreg +171", expected " +expected+reg);172System.out.println("Received expected "+(unreg-reg)+173" notifs after unregisterMBean");174System.out.println("Total notifs received: " + unreg);175176177}178179public static interface Simplest {180181}182183public static interface SimpleMXBean extends Simplest {184public String equals(String x);185}186187private static class Simple extends NotificationBroadcasterSupport188implements SimpleMXBean {189public static final String NOTIF_TYPE = "simple.equals";190private static long seq=0;191private static synchronized long seq() { return ++seq; };192public String equals(String x) {193sendNotification(new Notification(NOTIF_TYPE,this,seq(),x));194return x;195}196197}198199200201}202203204