Path: blob/master/test/jdk/javax/management/remote/mandatory/notif/ServerNotifs.java
41155 views
/*1* Copyright (c) 2003, 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* @test ServerNotifs.java25* @bug 765432126* @summary Tests the reception of the notifications for opened and closed27* connections28* @author sjiang29*30* @run clean ServerNotifs31* @run build ServerNotifs32* @run main ServerNotifs33*/3435// JAVA36import java.io.*;37import java.net.*;38import java.util.*;3940// JMX41import javax.management.*;4243// RJMX44import javax.management.remote.*;4546public class ServerNotifs {4748private static void echo(String msg) {49System.out.println(msg);50}5152public static void main(String[] args) {5354try {55// Create MBeanServer56//57echo("---Create the MBeanServer...");58MBeanServer mbs = MBeanServerFactory.createMBeanServer();5960// Create RMIConnectorServer61//62echo("---Instantiate the RMIConnectorServer...");63JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");64JMXConnectorServer cs =65JMXConnectorServerFactory.newJMXConnectorServer(url,66null,67mbs);6869echo("---Register the RMIConnectorServer in the MBeanServer...");70ObjectName on =71new ObjectName("JMXConnectors:name=RMIConnectorServer");72mbs.registerMBean(cs, on);7374echo("---Start the RMIConnectorServer...");75cs.start();76url = cs.getAddress();77echo("---RMIConnectorServer address: " + url);7879echo("---Add a local listener to the RMIConnectorServer...");80mbs.addNotificationListener(on, new MyListener(), null, null);8182// Create RMI connector83//84echo("---Instantiate the RMIConnector...");85JMXConnector c = JMXConnectorFactory.newJMXConnector(url, null);8687// Expect to get a "jmx.remote.connection.opened" notification88//89echo("---Open connection...");90c.connect(null);91Thread.sleep(100);9293// Expect to get a "jmx.remote.connection.closed" notification94//95echo("---Close connection...");96c.close();97Thread.sleep(100);9899// Waiting for all notifications100//101synchronized(waiting) {102if (!succeeded) {103final long waitingTime = 10000;104long remainingTime = waitingTime;105final long startTime = System.currentTimeMillis();106while (!succeeded && remainingTime > 0) {107waiting.wait(remainingTime);108remainingTime = waitingTime -109(System.currentTimeMillis() - startTime);110}111}112}113114// Stop the RMIConnectorServer115//116echo("---Stop the RMIConnectorServer...");117cs.stop();118119if (!succeeded) {120System.out.println("Timeout, did not get all notifications!");121System.exit(1);122}123} catch (MBeanException mbe) {124echo("---Test failed.");125echo("---Got exception: " + mbe);126mbe.getTargetException().printStackTrace();127System.exit(1);128} catch (RuntimeOperationsException roe) {129echo("---Test failed.");130echo("---Got exception: " + roe);131roe.getTargetException().printStackTrace();132System.exit(1);133} catch (Throwable t) {134echo("---Test failed.");135echo("---Got throwable: " + t);136t.printStackTrace();137System.exit(1);138}139}140141private static class MyListener implements NotificationListener {142public void handleNotification(Notification n, Object o) {143if (index == types.length) {144return;145}146echo("---Got a notification: " + n.getType());147echo(n.getMessage());148if (n instanceof JMXConnectionNotification) {149if (!n.getType().equals(types[index++])) {150System.out.println("Waiting to get a notification with " +151"type: " + types[index-1] + ", but " +152"got one with type: " + n.getType());153System.exit(1);154}155if (index == types.length) {156synchronized(waiting) {157succeeded = true;158waiting.notify();159}160}161}162}163}164165private static final String[] types =166new String[] {JMXConnectionNotification.OPENED,167JMXConnectionNotification.CLOSED};168private static int index = 0;169private static int[] waiting = new int[0];170private static boolean succeeded = false;171}172173174