0N/A/*
2362N/A * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
469N/A * @test
0N/A * @bug 7654321
469N/A * @summary Tests to receive notifications for opened and closed connections
1790N/Aions
0N/A * @author sjiang
0N/A * @run clean RMINotifTest
0N/A * @run build RMINotifTest
1790N/A * @run main RMINotifTest
469N/A * @run main RMINotifTest event
0N/A */
0N/A
0N/A// java imports
0N/A//
0N/A
469N/Aimport java.rmi.RemoteException;
469N/Aimport java.rmi.registry.LocateRegistry;
469N/Aimport java.rmi.registry.Registry;
0N/Aimport java.util.Random;
469N/Aimport javax.management.MBeanNotificationInfo;
469N/Aimport javax.management.MBeanServer;
469N/Aimport javax.management.MBeanServerConnection;
469N/Aimport javax.management.MBeanServerFactory;
469N/Aimport javax.management.Notification;
469N/Aimport javax.management.NotificationBroadcasterSupport;
469N/Aimport javax.management.NotificationFilterSupport;
469N/Aimport javax.management.NotificationListener;
469N/Aimport javax.management.ObjectInstance;
469N/Aimport javax.management.ObjectName;
469N/Aimport javax.management.remote.JMXConnector;
469N/Aimport javax.management.remote.JMXConnectorFactory;
469N/Aimport javax.management.remote.JMXConnectorServer;
469N/Aimport javax.management.remote.JMXConnectorServerFactory;
0N/Aimport javax.management.remote.JMXServiceURL;
0N/A
0N/Apublic class RMINotifTest {
0N/A
0N/A public static void main(String[] args) {
0N/A try {
0N/A // create a rmi registry
0N/A Registry reg = null;
0N/A int port = 6666;
0N/A final Random r = new Random();
0N/A
0N/A while(port++<7000) {
0N/A try {
0N/A reg = LocateRegistry.createRegistry(++port);
0N/A System.out.println("Creation of rmi registry succeeded. Running on port " + port);
0N/A break;
0N/A } catch (RemoteException re) {
0N/A // no problem
0N/A }
0N/A }
0N/A
0N/A if (reg == null) {
0N/A System.out.println("Failed to create a RMI registry, "+
0N/A "the ports from 6666 to 6999 are all occupied.");
0N/A System.exit(1);
0N/A }
0N/A
0N/A // create a MBeanServer
0N/A MBeanServer server = MBeanServerFactory.createMBeanServer();
0N/A
0N/A // create a notif emitter mbean
0N/A ObjectName mbean = new ObjectName ("Default:name=NotificationEmitter");
0N/A
0N/A server.registerMBean(new NotificationEmitter(), mbean);
0N/A
0N/A // create a rmi server
0N/A JMXServiceURL url =
0N/A new JMXServiceURL("rmi", null, port,
0N/A "/jndi/rmi://:" + port + "/server" + port);
0N/A System.out.println("RMIConnectorServer address " + url);
0N/A
0N/A JMXConnectorServer sServer =
1790N/A JMXConnectorServerFactory.newJMXConnectorServer(url, null,
1790N/A null);
0N/A
0N/A ObjectInstance ss = server.registerMBean(sServer, new ObjectName("Default:name=RmiConnectorServer"));
0N/A
0N/A sServer.start();
0N/A
0N/A // create a rmi client
0N/A JMXConnector rmiConnection =
0N/A JMXConnectorFactory.newJMXConnector(url, null);
0N/A rmiConnection.connect(null);
0N/A MBeanServerConnection client = rmiConnection.getMBeanServerConnection();
0N/A
0N/A // add listener at the client side
0N/A client.addNotificationListener(mbean, listener, null, null);
0N/A
0N/A //ask to send notifs
0N/A Object[] params = new Object[1];
0N/A String[] signatures = new String[1];
0N/A
0N/A params[0] = new Integer(nb);
0N/A signatures[0] = "java.lang.Integer";
0N/A
0N/A client.invoke(mbean, "sendNotifications", params, signatures);
0N/A
0N/A // waiting ...
0N/A synchronized (lock) {
0N/A if (receivedNotifs != nb) {
0N/A lock.wait(10000);
0N/A System.out.println(">>> Received notifications..."+receivedNotifs);
0N/A
0N/A }
0N/A }
0N/A
0N/A // check
0N/A if (receivedNotifs != nb) {
0N/A System.exit(1);
0N/A } else {
0N/A System.out.println("The client received all notifications.");
0N/A }
0N/A
0N/A // remove listener
0N/A client.removeNotificationListener(mbean, listener);
0N/A
0N/A // more test
0N/A NotificationFilterSupport filter = new NotificationFilterSupport();
0N/A Object o = new Object();
0N/A client.addNotificationListener(mbean, listener, filter, o);
0N/A client.removeNotificationListener(mbean, listener, filter, o);
0N/A
0N/A sServer.stop();
0N/A
0N/A// // clean
0N/A// client.unregisterMBean(mbean);
0N/A// rmiConnection.close();
0N/A
0N/A// Thread.sleep(2000);
0N/A
0N/A
0N/A
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A System.exit(1);
0N/A }
0N/A }
0N/A
0N/A//--------------------------
0N/A// private classes
0N/A//--------------------------
0N/A
0N/A private static class Listener implements NotificationListener {
0N/A public void handleNotification(Notification notif, Object handback) {
0N/A if(++receivedNotifs == nb) {
0N/A synchronized(lock) {
0N/A lock.notifyAll();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public static class NotificationEmitter extends NotificationBroadcasterSupport implements NotificationEmitterMBean {
0N/A// public NotificationEmitter() {
0N/A// super();
0N/A// System.out.println("===NotificationEmitter: new instance.");
0N/A// }
0N/A
0N/A /**
0N/A * Returns a NotificationInfo object containing the name of the Java class of the notification
0N/A * and the notification types sent by this notification broadcaster.
0N/A */
0N/A public MBeanNotificationInfo[] getNotificationInfo() {
0N/A
0N/A MBeanNotificationInfo[] ntfInfoArray = new MBeanNotificationInfo[1];
0N/A
0N/A String[] ntfTypes = new String[1];
0N/A ntfTypes[0] = myType;
0N/A
0N/A ntfInfoArray[0] = new MBeanNotificationInfo(ntfTypes,
0N/A "javax.management.Notification",
0N/A "Notifications sent by the NotificationEmitter");
0N/A return ntfInfoArray;
0N/A }
0N/A
0N/A// public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) {
0N/A// super.addNotificationListener(listener, filter, handback);
0N/A
0N/A// System.out.println("============NotificationEmitter: add new listener");
0N/A// }
0N/A
0N/A /**
0N/A * Send a Notification object with the specified times.
0N/A * The sequence number will be from zero to times-1.
0N/A *
0N/A * @param nb The number of notifications to send
0N/A */
0N/A public void sendNotifications(Integer nb) {
0N/A System.out.println("===NotificationEmitter: be asked to send notifications: "+nb);
0N/A
0N/A Notification notif;
0N/A for (int i=1; i<=nb.intValue(); i++) {
0N/A notif = new Notification(myType, this, i);
0N/A sendNotification(notif);
0N/A }
0N/A }
0N/A
0N/A private String myType = "notification.my_notification";
0N/A }
0N/A
0N/A public interface NotificationEmitterMBean {
0N/A public void sendNotifications(Integer nb);
0N/A }
0N/A
0N/A private static NotificationListener listener = new Listener();
0N/A
0N/A private static int nb = 10;
0N/A private static int receivedNotifs = 0;
0N/A private static int[] lock = new int[0];
0N/A}