blob: 327274c7a63119aed952a8fb7aa78d490223e87f [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test RMINotifTest.java
26 * @bug 7654321
27 * @summary Tests to receive notifications for opened and closed connect
28ions
29 * @author sjiang
30 * @run clean RMINotifTest
31 * @run build RMINotifTest
32 * @run main RMINotifTest
33 */
34
35// java imports
36//
37import java.io.IOException;
38import java.net.UnknownHostException;
39
40import java.rmi.*;
41import java.rmi.registry.*;
42import java.util.Random;
43
44// JMX imports
45//
46import javax.management.* ;
47
48import javax.management.remote.*;
49import javax.management.remote.rmi.*;
50import javax.management.remote.JMXServiceURL;
51
52public class RMINotifTest {
53
54 public static void main(String[] args) {
55 try {
56 // create a rmi registry
57 Registry reg = null;
58 int port = 6666;
59 final Random r = new Random();
60
61 while(port++<7000) {
62 try {
63 reg = LocateRegistry.createRegistry(++port);
64 System.out.println("Creation of rmi registry succeeded. Running on port " + port);
65 break;
66 } catch (RemoteException re) {
67 // no problem
68 }
69 }
70
71 if (reg == null) {
72 System.out.println("Failed to create a RMI registry, "+
73 "the ports from 6666 to 6999 are all occupied.");
74 System.exit(1);
75 }
76
77 // create a MBeanServer
78 MBeanServer server = MBeanServerFactory.createMBeanServer();
79
80 // create a notif emitter mbean
81 ObjectName mbean = new ObjectName ("Default:name=NotificationEmitter");
82
83 server.registerMBean(new NotificationEmitter(), mbean);
84
85 // create a rmi server
86 JMXServiceURL url =
87 new JMXServiceURL("rmi", null, port,
88 "/jndi/rmi://:" + port + "/server" + port);
89 System.out.println("RMIConnectorServer address " + url);
90
91 JMXConnectorServer sServer =
92 JMXConnectorServerFactory.newJMXConnectorServer(url, null,
93 null);
94
95 ObjectInstance ss = server.registerMBean(sServer, new ObjectName("Default:name=RmiConnectorServer"));
96
97 sServer.start();
98
99 // create a rmi client
100 JMXConnector rmiConnection =
101 JMXConnectorFactory.newJMXConnector(url, null);
102 rmiConnection.connect(null);
103 MBeanServerConnection client = rmiConnection.getMBeanServerConnection();
104
105 // add listener at the client side
106 client.addNotificationListener(mbean, listener, null, null);
107
108 //ask to send notifs
109 Object[] params = new Object[1];
110 String[] signatures = new String[1];
111
112 params[0] = new Integer(nb);
113 signatures[0] = "java.lang.Integer";
114
115 client.invoke(mbean, "sendNotifications", params, signatures);
116
117 // waiting ...
118 synchronized (lock) {
119 if (receivedNotifs != nb) {
120 lock.wait(10000);
121 System.out.println(">>> Received notifications..."+receivedNotifs);
122
123 }
124 }
125
126 // check
127 if (receivedNotifs != nb) {
128 System.exit(1);
129 } else {
130 System.out.println("The client received all notifications.");
131 }
132
133 // remove listener
134 client.removeNotificationListener(mbean, listener);
135
136 // more test
137 NotificationFilterSupport filter = new NotificationFilterSupport();
138 Object o = new Object();
139 client.addNotificationListener(mbean, listener, filter, o);
140 client.removeNotificationListener(mbean, listener, filter, o);
141
142 sServer.stop();
143
144// // clean
145// client.unregisterMBean(mbean);
146// rmiConnection.close();
147
148// Thread.sleep(2000);
149
150
151
152 } catch (Exception e) {
153 e.printStackTrace();
154 System.exit(1);
155 }
156 }
157
158//--------------------------
159// private classes
160//--------------------------
161
162 private static class Listener implements NotificationListener {
163 public void handleNotification(Notification notif, Object handback) {
164 if(++receivedNotifs == nb) {
165 synchronized(lock) {
166 lock.notifyAll();
167 }
168 }
169 }
170 }
171
172 public static class NotificationEmitter extends NotificationBroadcasterSupport implements NotificationEmitterMBean {
173// public NotificationEmitter() {
174// super();
175// System.out.println("===NotificationEmitter: new instance.");
176// }
177
178 /**
179 * Returns a NotificationInfo object containing the name of the Java class of the notification
180 * and the notification types sent by this notification broadcaster.
181 */
182 public MBeanNotificationInfo[] getNotificationInfo() {
183
184 MBeanNotificationInfo[] ntfInfoArray = new MBeanNotificationInfo[1];
185
186 String[] ntfTypes = new String[1];
187 ntfTypes[0] = myType;
188
189 ntfInfoArray[0] = new MBeanNotificationInfo(ntfTypes,
190 "javax.management.Notification",
191 "Notifications sent by the NotificationEmitter");
192 return ntfInfoArray;
193 }
194
195// public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) {
196// super.addNotificationListener(listener, filter, handback);
197
198// System.out.println("============NotificationEmitter: add new listener");
199// }
200
201 /**
202 * Send a Notification object with the specified times.
203 * The sequence number will be from zero to times-1.
204 *
205 * @param nb The number of notifications to send
206 */
207 public void sendNotifications(Integer nb) {
208 System.out.println("===NotificationEmitter: be asked to send notifications: "+nb);
209
210 Notification notif;
211 for (int i=1; i<=nb.intValue(); i++) {
212 notif = new Notification(myType, this, i);
213 sendNotification(notif);
214 }
215 }
216
217 private String myType = "notification.my_notification";
218 }
219
220 public interface NotificationEmitterMBean {
221 public void sendNotifications(Integer nb);
222 }
223
224 private static NotificationListener listener = new Listener();
225
226 private static int nb = 10;
227 private static int receivedNotifs = 0;
228 private static int[] lock = new int[0];
229}