Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/notif/ServerNotifs.java
38867 views
/*1* Copyright (c) 2003, 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* @test ServerNotifs.java25* @bug 765432126* @summary Tests the reception of the notifications for opened and closed27* connections28* @author sjiang29* @run clean ServerNotifs30* @run build ServerNotifs31* @run main ServerNotifs32*/3334// JAVA35import java.io.*;36import java.net.*;37import java.util.*;3839// JMX40import javax.management.*;4142// RJMX43import javax.management.remote.*;4445public class ServerNotifs {4647private static void echo(String msg) {48System.out.println(msg);49}5051public static void main(String[] args) {5253try {54// Create MBeanServer55//56echo("---Create the MBeanServer...");57MBeanServer mbs = MBeanServerFactory.createMBeanServer();5859// Create RMIConnectorServer60//61echo("---Instantiate the RMIConnectorServer...");62JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");63JMXConnectorServer cs =64JMXConnectorServerFactory.newJMXConnectorServer(url,65null,66mbs);6768echo("---Register the RMIConnectorServer in the MBeanServer...");69ObjectName on =70new ObjectName("JMXConnectors:name=RMIConnectorServer");71mbs.registerMBean(cs, on);7273echo("---Start the RMIConnectorServer...");74cs.start();75url = cs.getAddress();76echo("---RMIConnectorServer address: " + url);7778echo("---Add a local listener to the RMIConnectorServer...");79mbs.addNotificationListener(on, new MyListener(), null, null);8081// Create RMI connector82//83echo("---Instantiate the RMIConnector...");84JMXConnector c = JMXConnectorFactory.newJMXConnector(url, null);8586// Expect to get a "jmx.remote.connection.opened" notification87//88echo("---Open connection...");89c.connect(null);90Thread.sleep(100);9192// Expect to get a "jmx.remote.connection.closed" notification93//94echo("---Close connection...");95c.close();96Thread.sleep(100);9798// Waiting for all notifications99//100synchronized(waiting) {101if (!succeeded) {102final long waitingTime = 10000;103long remainingTime = waitingTime;104final long startTime = System.currentTimeMillis();105while (!succeeded && remainingTime > 0) {106waiting.wait(remainingTime);107remainingTime = waitingTime -108(System.currentTimeMillis() - startTime);109}110}111}112113// Stop the RMIConnectorServer114//115echo("---Stop the RMIConnectorServer...");116cs.stop();117118if (!succeeded) {119System.out.println("Timeout, did not get all notifications!");120System.exit(1);121}122} catch (MBeanException mbe) {123echo("---Test failed.");124echo("---Got exception: " + mbe);125mbe.getTargetException().printStackTrace();126System.exit(1);127} catch (RuntimeOperationsException roe) {128echo("---Test failed.");129echo("---Got exception: " + roe);130roe.getTargetException().printStackTrace();131System.exit(1);132} catch (Throwable t) {133echo("---Test failed.");134echo("---Got throwable: " + t);135t.printStackTrace();136System.exit(1);137}138}139140private static class MyListener implements NotificationListener {141public void handleNotification(Notification n, Object o) {142if (index == types.length) {143return;144}145echo("---Got a notification: " + n.getType());146echo(n.getMessage());147if (n instanceof JMXConnectionNotification) {148if (!n.getType().equals(types[index++])) {149System.out.println("Waiting to get a notification with " +150"type: " + types[index-1] + ", but " +151"got one with type: " + n.getType());152System.exit(1);153}154if (index == types.length) {155synchronized(waiting) {156succeeded = true;157waiting.notify();158}159}160}161}162}163164private static final String[] types =165new String[] {JMXConnectionNotification.OPENED,166JMXConnectionNotification.CLOSED};167private static int index = 0;168private static int[] waiting = new int[0];169private static boolean succeeded = false;170}171172173