Path: blob/master/test/jdk/javax/management/remote/mandatory/notif/RMINotifTest.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* @test25* @bug 765432126* @summary Tests to receive notifications for opened and closed connections27* @author sjiang28*29* @run clean RMINotifTest30* @run build RMINotifTest31* @run main RMINotifTest32* @run main RMINotifTest event33*/3435// java imports36//3738import java.rmi.RemoteException;39import java.rmi.registry.LocateRegistry;40import java.rmi.registry.Registry;41import java.util.Random;42import javax.management.MBeanNotificationInfo;43import javax.management.MBeanServer;44import javax.management.MBeanServerConnection;45import javax.management.MBeanServerFactory;46import javax.management.Notification;47import javax.management.NotificationBroadcasterSupport;48import javax.management.NotificationFilterSupport;49import javax.management.NotificationListener;50import javax.management.ObjectInstance;51import javax.management.ObjectName;52import javax.management.remote.JMXConnector;53import javax.management.remote.JMXConnectorFactory;54import javax.management.remote.JMXConnectorServer;55import javax.management.remote.JMXConnectorServerFactory;56import javax.management.remote.JMXServiceURL;5758public class RMINotifTest {5960public static void main(String[] args) {61try {62// create a rmi registry63Registry reg = null;64int port = 6666;65final Random r = new Random();6667while(port++<7000) {68try {69reg = LocateRegistry.createRegistry(++port);70System.out.println("Creation of rmi registry succeeded. Running on port " + port);71break;72} catch (RemoteException re) {73// no problem74}75}7677if (reg == null) {78System.out.println("Failed to create a RMI registry, "+79"the ports from 6666 to 6999 are all occupied.");80System.exit(1);81}8283// create a MBeanServer84MBeanServer server = MBeanServerFactory.createMBeanServer();8586// create a notif emitter mbean87ObjectName mbean = new ObjectName ("Default:name=NotificationEmitter");8889server.registerMBean(new NotificationEmitter(), mbean);9091// create a rmi server92JMXServiceURL url =93new JMXServiceURL("rmi", null, port,94"/jndi/rmi://:" + port + "/server" + port);95System.out.println("RMIConnectorServer address " + url);9697JMXConnectorServer sServer =98JMXConnectorServerFactory.newJMXConnectorServer(url, null,99null);100101ObjectInstance ss = server.registerMBean(sServer, new ObjectName("Default:name=RmiConnectorServer"));102103sServer.start();104105// create a rmi client106JMXConnector rmiConnection =107JMXConnectorFactory.newJMXConnector(url, null);108rmiConnection.connect(null);109MBeanServerConnection client = rmiConnection.getMBeanServerConnection();110111// add listener at the client side112client.addNotificationListener(mbean, listener, null, null);113114//ask to send notifs115Object[] params = new Object[1];116String[] signatures = new String[1];117118params[0] = new Integer(nb);119signatures[0] = "java.lang.Integer";120121client.invoke(mbean, "sendNotifications", params, signatures);122123// waiting ...124synchronized (lock) {125if (receivedNotifs != nb) {126lock.wait(10000);127System.out.println(">>> Received notifications..."+receivedNotifs);128129}130}131132// check133if (receivedNotifs != nb) {134System.exit(1);135} else {136System.out.println("The client received all notifications.");137}138139// remove listener140client.removeNotificationListener(mbean, listener);141142// more test143NotificationFilterSupport filter = new NotificationFilterSupport();144Object o = new Object();145client.addNotificationListener(mbean, listener, filter, o);146client.removeNotificationListener(mbean, listener, filter, o);147148sServer.stop();149150// // clean151// client.unregisterMBean(mbean);152// rmiConnection.close();153154// Thread.sleep(2000);155156157158} catch (Exception e) {159e.printStackTrace();160System.exit(1);161}162}163164//--------------------------165// private classes166//--------------------------167168private static class Listener implements NotificationListener {169public void handleNotification(Notification notif, Object handback) {170if(++receivedNotifs == nb) {171synchronized(lock) {172lock.notifyAll();173}174}175}176}177178public static class NotificationEmitter extends NotificationBroadcasterSupport implements NotificationEmitterMBean {179// public NotificationEmitter() {180// super();181// System.out.println("===NotificationEmitter: new instance.");182// }183184/**185* Returns a NotificationInfo object containing the name of the Java class of the notification186* and the notification types sent by this notification broadcaster.187*/188public MBeanNotificationInfo[] getNotificationInfo() {189190MBeanNotificationInfo[] ntfInfoArray = new MBeanNotificationInfo[1];191192String[] ntfTypes = new String[1];193ntfTypes[0] = myType;194195ntfInfoArray[0] = new MBeanNotificationInfo(ntfTypes,196"javax.management.Notification",197"Notifications sent by the NotificationEmitter");198return ntfInfoArray;199}200201// public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) {202// super.addNotificationListener(listener, filter, handback);203204// System.out.println("============NotificationEmitter: add new listener");205// }206207/**208* Send a Notification object with the specified times.209* The sequence number will be from zero to times-1.210*211* @param nb The number of notifications to send212*/213public void sendNotifications(Integer nb) {214System.out.println("===NotificationEmitter: be asked to send notifications: "+nb);215216Notification notif;217for (int i=1; i<=nb.intValue(); i++) {218notif = new Notification(myType, this, i);219sendNotification(notif);220}221}222223private String myType = "notification.my_notification";224}225226public interface NotificationEmitterMBean {227public void sendNotifications(Integer nb);228}229230private static NotificationListener listener = new Listener();231232private static int nb = 10;233private static int receivedNotifs = 0;234private static int[] lock = new int[0];235}236237238