Path: blob/master/test/jdk/javax/management/remote/mandatory/notif/EmptyDomainNotificationTest.java
41155 views
/*1* Copyright (c) 2005, 2016, 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 623873126* @summary Check that the expected notification is received by the JMX27* client even when the domain in the ObjectName is not specified28* @author Shanliang JIANG29*30* @run clean EmptyDomainNotificationTest31* @run build EmptyDomainNotificationTest32* @run main EmptyDomainNotificationTest33*/3435import java.util.ArrayList;36import java.util.List;37import javax.management.MBeanServer;38import javax.management.MBeanServerConnection;39import javax.management.MBeanServerFactory;40import javax.management.Notification;41import javax.management.NotificationBroadcasterSupport;42import javax.management.NotificationListener;43import javax.management.ObjectName;44import javax.management.remote.JMXConnector;45import javax.management.remote.JMXConnectorFactory;46import javax.management.remote.JMXConnectorServer;47import javax.management.remote.JMXConnectorServerFactory;48import javax.management.remote.JMXServiceURL;4950public class EmptyDomainNotificationTest {5152public static interface SimpleMBean {53public void emitNotification();54}5556public static class Simple57extends NotificationBroadcasterSupport58implements SimpleMBean {59public void emitNotification() {60sendNotification(new Notification("simple", this, 0));61}62}6364public static class Listener implements NotificationListener {65public void handleNotification(Notification n, Object h) {66System.out.println(67"EmptyDomainNotificationTest-Listener-handleNotification: receives:" + n);6869if (n.getType().equals("simple")) {70synchronized(this) {71received++;7273this.notifyAll();74}75}76}7778public int received;79}8081public static void main(String[] args) throws Exception {8283final MBeanServer mbs = MBeanServerFactory.createMBeanServer();8485final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");8687JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);88server.start();8990JMXConnector client = JMXConnectorFactory.connect(server.getAddress(), null);9192final MBeanServerConnection mbsc = client.getMBeanServerConnection();9394final ObjectName mbean = ObjectName.getInstance(":type=Simple");95mbsc.createMBean(Simple.class.getName(), mbean);9697System.out.println("EmptyDomainNotificationTest-main: add a listener ...");98final Listener li = new Listener();99mbsc.addNotificationListener(mbean, li, null, null);100101System.out.println("EmptyDomainNotificationTest-main: ask to send a notif ...");102mbsc.invoke(mbean, "emitNotification", null, null);103104System.out.println("EmptyDomainNotificationTest-main: waiting notif...");105synchronized(li) {106while (li.received < 1) {107li.wait();108}109}110111if (li.received != 1) {112throw new RuntimeException("Wait one notif but got: "+li.received);113}114115System.out.println("EmptyDomainNotificationTest-main: Got the expected notif!");116117System.out.println("EmptyDomainNotificationTest-main: remove the listener.");118mbsc.removeNotificationListener(mbean, li);119120// clean121client.close();122server.stop();123124System.out.println("EmptyDomainNotificationTest-main: Bye.");125}126}127128129