2
|
1 |
/*
|
|
2 |
* Copyright 2003 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
|
|
26 |
* @bug 7654321
|
|
27 |
* @summary Tests whether a listener receives notifs emitted before the
|
|
28 |
* listener is registered.
|
|
29 |
* @author Shanliang JIANG
|
|
30 |
* @run clean UnexpectedNotifTest
|
|
31 |
* @run build UnexpectedNotifTest
|
|
32 |
* @run main UnexpectedNotifTest
|
|
33 |
*/
|
|
34 |
|
1004
|
35 |
import java.util.ArrayList;
|
|
36 |
import java.util.Collections;
|
|
37 |
import java.util.List;
|
|
38 |
import java.util.Map;
|
|
39 |
import javax.management.MBeanNotificationInfo;
|
|
40 |
import javax.management.MBeanServer;
|
|
41 |
import javax.management.MBeanServerConnection;
|
|
42 |
import javax.management.MBeanServerFactory;
|
|
43 |
import javax.management.Notification;
|
|
44 |
import javax.management.NotificationBroadcasterSupport;
|
|
45 |
import javax.management.NotificationListener;
|
|
46 |
import javax.management.ObjectName;
|
|
47 |
import javax.management.remote.JMXConnector;
|
|
48 |
import javax.management.remote.JMXConnectorFactory;
|
|
49 |
import javax.management.remote.JMXConnectorServer;
|
|
50 |
import javax.management.remote.JMXConnectorServerFactory;
|
|
51 |
import javax.management.remote.JMXServiceURL;
|
2
|
52 |
//
|
1004
|
53 |
import javax.management.remote.rmi.RMIConnectorServer;
|
2
|
54 |
|
|
55 |
public class UnexpectedNotifTest {
|
|
56 |
|
|
57 |
public static void main(String[] args) throws Exception {
|
1004
|
58 |
List<String> protos = new ArrayList<String>();
|
|
59 |
protos.add("rmi");
|
2
|
60 |
try {
|
|
61 |
Class.forName("javax.management.remote.jmxmp.JMXMPConnectorServer");
|
1004
|
62 |
protos.add("jmxmp");
|
2
|
63 |
} catch (ClassNotFoundException e) {
|
1004
|
64 |
// OK: JMXMP not present so don't test it.
|
|
65 |
}
|
|
66 |
for (String proto : protos) {
|
|
67 |
test(proto, false);
|
|
68 |
test(proto, true);
|
2
|
69 |
}
|
1004
|
70 |
}
|
2
|
71 |
|
1004
|
72 |
private static void test(String proto, boolean eventService)
|
|
73 |
throws Exception {
|
|
74 |
System.out.println("Unexpected notifications test for protocol " +
|
|
75 |
proto + " with" +
|
|
76 |
(eventService ? "" : "out") + " event service");
|
|
77 |
MBeanServer mbs = null;
|
|
78 |
try {
|
|
79 |
// Create a MBeanServer
|
|
80 |
//
|
|
81 |
mbs = MBeanServerFactory.createMBeanServer();
|
|
82 |
|
|
83 |
// Create a NotificationEmitter MBean
|
|
84 |
//
|
|
85 |
mbean = new ObjectName ("Default:name=NotificationEmitter");
|
|
86 |
mbs.registerMBean(new NotificationEmitter(), mbean);
|
2
|
87 |
|
1004
|
88 |
// Create a connector server
|
|
89 |
//
|
|
90 |
url = new JMXServiceURL("service:jmx:" + proto + "://");
|
|
91 |
Map<String, String> env = Collections.singletonMap(
|
|
92 |
RMIConnectorServer.DELEGATE_TO_EVENT_SERVICE,
|
|
93 |
Boolean.toString(eventService));
|
2
|
94 |
|
1004
|
95 |
server = JMXConnectorServerFactory.newJMXConnectorServer(url,
|
|
96 |
env,
|
|
97 |
mbs);
|
2
|
98 |
|
1004
|
99 |
mbs.registerMBean(
|
|
100 |
server,
|
|
101 |
new ObjectName("Default:name=ConnectorServer"));
|
2
|
102 |
|
1004
|
103 |
server.start();
|
|
104 |
|
|
105 |
url = server.getAddress();
|
2
|
106 |
|
1004
|
107 |
for (int j = 0; j < 2; j++) {
|
|
108 |
test();
|
2
|
109 |
}
|
1004
|
110 |
} finally {
|
|
111 |
// Stop server
|
|
112 |
//
|
|
113 |
server.stop();
|
|
114 |
// Release the MBeanServer
|
|
115 |
//
|
|
116 |
MBeanServerFactory.releaseMBeanServer(mbs);
|
2
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
private static void test() throws Exception {
|
|
121 |
// Create client
|
|
122 |
//
|
|
123 |
JMXConnector connector = JMXConnectorFactory.connect(url);
|
|
124 |
MBeanServerConnection client = connector.getMBeanServerConnection();
|
|
125 |
|
|
126 |
// Add listener at the client side
|
|
127 |
//
|
|
128 |
client.addNotificationListener(mbean, listener, null, null);
|
|
129 |
|
|
130 |
// Cleanup
|
|
131 |
//
|
|
132 |
receivedNotifs = 0;
|
|
133 |
|
|
134 |
// Ask to send notifs
|
|
135 |
//
|
|
136 |
Object[] params = new Object[] {new Integer(nb)};
|
|
137 |
String[] signatures = new String[] {"java.lang.Integer"};
|
|
138 |
|
|
139 |
client.invoke(mbean, "sendNotifications", params, signatures);
|
|
140 |
|
|
141 |
// Waiting...
|
|
142 |
//
|
|
143 |
synchronized (lock) {
|
|
144 |
for (int i = 0; i < 10; i++) {
|
|
145 |
if (receivedNotifs < nb) {
|
|
146 |
lock.wait(1000);
|
|
147 |
}
|
|
148 |
}
|
|
149 |
}
|
|
150 |
|
|
151 |
// Waiting again to ensure no more notifs
|
|
152 |
//
|
|
153 |
Thread.sleep(3000);
|
|
154 |
|
|
155 |
synchronized (lock) {
|
|
156 |
if (receivedNotifs != nb) {
|
|
157 |
throw new Exception("The client expected to receive " +
|
|
158 |
nb + " notifs, but got " + receivedNotifs);
|
|
159 |
}
|
|
160 |
}
|
|
161 |
|
|
162 |
// Remove listener
|
|
163 |
//
|
|
164 |
client.removeNotificationListener(mbean, listener);
|
|
165 |
|
|
166 |
connector.close();
|
|
167 |
}
|
|
168 |
|
|
169 |
//--------------------------
|
|
170 |
// private classes
|
|
171 |
//--------------------------
|
|
172 |
|
|
173 |
private static class Listener implements NotificationListener {
|
|
174 |
public void handleNotification(Notification notif, Object handback) {
|
|
175 |
System.out.println("Received: " + notif + " (" +
|
|
176 |
notif.getSequenceNumber() + ")");
|
|
177 |
synchronized(lock) {
|
|
178 |
if(++receivedNotifs == nb) {
|
|
179 |
lock.notifyAll();
|
|
180 |
} else if (receivedNotifs > nb) {
|
|
181 |
System.out.println("The client expected to receive " +
|
|
182 |
nb + " notifs, but got at least " +
|
|
183 |
receivedNotifs);
|
|
184 |
System.exit(1);
|
|
185 |
}
|
|
186 |
}
|
|
187 |
}
|
|
188 |
}
|
|
189 |
|
|
190 |
public static class NotificationEmitter
|
|
191 |
extends NotificationBroadcasterSupport
|
|
192 |
implements NotificationEmitterMBean {
|
|
193 |
|
|
194 |
/**
|
|
195 |
* Returns a NotificationInfo object containing the name of the Java
|
|
196 |
* class of the notification and the notification types sent by this
|
|
197 |
* notification broadcaster.
|
|
198 |
*/
|
|
199 |
public MBeanNotificationInfo[] getNotificationInfo() {
|
|
200 |
|
|
201 |
MBeanNotificationInfo[] ntfInfoArray = new MBeanNotificationInfo[1];
|
|
202 |
|
|
203 |
String[] ntfTypes = new String[1];
|
|
204 |
ntfTypes[0] = myType;
|
|
205 |
|
|
206 |
ntfInfoArray[0] = new MBeanNotificationInfo(
|
|
207 |
ntfTypes,
|
|
208 |
"javax.management.Notification",
|
|
209 |
"Notifications sent by the NotificationEmitter");
|
|
210 |
return ntfInfoArray;
|
|
211 |
}
|
|
212 |
|
|
213 |
/**
|
|
214 |
* Send a Notification object with the specified times.
|
|
215 |
* The sequence number will be from zero to times-1.
|
|
216 |
*
|
|
217 |
* @param nb The number of notifications to send
|
|
218 |
*/
|
|
219 |
public void sendNotifications(Integer nb) {
|
|
220 |
System.out.println("NotificationEmitter: asked to send " +
|
|
221 |
"notifications: " + nb);
|
|
222 |
|
|
223 |
Notification notif;
|
|
224 |
for (int i = 1; i <= nb.intValue(); i++) {
|
|
225 |
notif = new Notification(myType, this, ++seqno);
|
|
226 |
sendNotification(notif);
|
|
227 |
}
|
|
228 |
}
|
|
229 |
|
|
230 |
private String myType = "notification.my_notification";
|
|
231 |
}
|
|
232 |
|
|
233 |
public interface NotificationEmitterMBean {
|
|
234 |
public void sendNotifications(Integer nb);
|
|
235 |
}
|
|
236 |
|
|
237 |
private static JMXConnectorServer server;
|
|
238 |
private static JMXServiceURL url;
|
|
239 |
private static ObjectName mbean;
|
|
240 |
private static NotificationListener listener = new Listener();
|
|
241 |
|
|
242 |
private static int nb = 10;
|
|
243 |
private static int receivedNotifs = 0;
|
|
244 |
private static int[] lock = new int[0];
|
|
245 |
private static volatile long seqno;
|
|
246 |
}
|