Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/notif/EmptyDomainNotificationTest.java
38867 views
/*1* Copyright (c) 2005, 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* @run clean EmptyDomainNotificationTest30* @run build EmptyDomainNotificationTest31* @run main EmptyDomainNotificationTest32*/3334import java.util.ArrayList;35import java.util.List;36import javax.management.MBeanServer;37import javax.management.MBeanServerConnection;38import javax.management.MBeanServerFactory;39import javax.management.Notification;40import javax.management.NotificationBroadcasterSupport;41import javax.management.NotificationListener;42import javax.management.ObjectName;43import javax.management.remote.JMXConnector;44import javax.management.remote.JMXConnectorFactory;45import javax.management.remote.JMXConnectorServer;46import javax.management.remote.JMXConnectorServerFactory;47import javax.management.remote.JMXServiceURL;4849public class EmptyDomainNotificationTest {5051public static interface SimpleMBean {52public void emitNotification();53}5455public static class Simple56extends NotificationBroadcasterSupport57implements SimpleMBean {58public void emitNotification() {59sendNotification(new Notification("simple", this, 0));60}61}6263public static class Listener implements NotificationListener {64public void handleNotification(Notification n, Object h) {65System.out.println(66"EmptyDomainNotificationTest-Listener-handleNotification: receives:" + n);6768if (n.getType().equals("simple")) {69synchronized(this) {70received++;7172this.notifyAll();73}74}75}7677public int received;78}7980public static void main(String[] args) throws Exception {8182final MBeanServer mbs = MBeanServerFactory.createMBeanServer();8384final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");8586JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);87server.start();8889JMXConnector client = JMXConnectorFactory.connect(server.getAddress(), null);9091final MBeanServerConnection mbsc = client.getMBeanServerConnection();9293final ObjectName mbean = ObjectName.getInstance(":type=Simple");94mbsc.createMBean(Simple.class.getName(), mbean);9596System.out.println("EmptyDomainNotificationTest-main: add a listener ...");97final Listener li = new Listener();98mbsc.addNotificationListener(mbean, li, null, null);99100System.out.println("EmptyDomainNotificationTest-main: ask to send a notif ...");101mbsc.invoke(mbean, "emitNotification", null, null);102103System.out.println("EmptyDomainNotificationTest-main: waiting notif...");104final long stopTime = System.currentTimeMillis() + 2000;105synchronized(li) {106long toWait = stopTime - System.currentTimeMillis();107108while (li.received < 1 && toWait > 0) {109li.wait(toWait);110111toWait = stopTime - System.currentTimeMillis();112}113}114115if (li.received < 1) {116throw new RuntimeException("No notif received!");117} else if (li.received > 1) {118throw new RuntimeException("Wait one notif but got: "+li.received);119}120121System.out.println("EmptyDomainNotificationTest-main: Got the expected notif!");122123System.out.println("EmptyDomainNotificationTest-main: remove the listener.");124mbsc.removeNotificationListener(mbean, li);125126// clean127client.close();128server.stop();129130System.out.println("EmptyDomainNotificationTest-main: Bye.");131}132}133134135