1 /* |
|
2 * Copyright 2007-2008 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 import javax.management.MBeanServer; |
|
25 import javax.management.MBeanServerConnection; |
|
26 import javax.management.MBeanServerFactory; |
|
27 import javax.management.Notification; |
|
28 import javax.management.NotificationListener; |
|
29 import javax.management.ObjectName; |
|
30 import javax.management.event.*; |
|
31 import javax.management.remote.JMXConnector; |
|
32 import javax.management.remote.JMXConnectorFactory; |
|
33 import javax.management.remote.JMXConnectorServer; |
|
34 import javax.management.remote.JMXConnectorServerFactory; |
|
35 import javax.management.remote.JMXServiceURL; |
|
36 |
|
37 /** |
|
38 * |
|
39 */ |
|
40 public class PublishTest { |
|
41 private static MBeanServer mbeanServer; |
|
42 private static EventManager eventManager; |
|
43 private static ObjectName emitter; |
|
44 private static JMXServiceURL url; |
|
45 private static JMXConnectorServer server; |
|
46 private static JMXConnector conn; |
|
47 private static MBeanServerConnection client; |
|
48 |
|
49 /** |
|
50 * @param args the command line arguments |
|
51 */ |
|
52 public static void main(String[] args) throws Exception { |
|
53 System.out.println(">>> PublishTest-main basic tests ..."); |
|
54 mbeanServer = MBeanServerFactory.createMBeanServer(); |
|
55 |
|
56 // for 1.5 |
|
57 if (System.getProperty("java.version").startsWith("1.5") && |
|
58 !mbeanServer.isRegistered(EventClientDelegateMBean.OBJECT_NAME)) { |
|
59 System.out.print("Working on "+System.getProperty("java.version")+ |
|
60 " register "+EventClientDelegateMBean.OBJECT_NAME); |
|
61 |
|
62 mbeanServer.registerMBean(EventClientDelegate. |
|
63 getEventClientDelegate(mbeanServer), |
|
64 EventClientDelegateMBean.OBJECT_NAME); |
|
65 } |
|
66 |
|
67 eventManager = EventManager.getEventManager(mbeanServer); |
|
68 |
|
69 emitter = new ObjectName("Default:name=NotificationEmitter"); |
|
70 |
|
71 url = new JMXServiceURL("rmi", null, 0) ; |
|
72 server = |
|
73 JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbeanServer); |
|
74 server.start(); |
|
75 |
|
76 url = server.getAddress(); |
|
77 conn = JMXConnectorFactory.connect(url, null); |
|
78 client = conn.getMBeanServerConnection(); |
|
79 |
|
80 boolean succeed; |
|
81 |
|
82 System.out.println(">>> PublishTest-main: using the fetching EventRelay..."); |
|
83 succeed = test(new EventClient(client)); |
|
84 |
|
85 System.out.println(">>> PublishTest-main: using the pushing EventRelay..."); |
|
86 succeed &= test(new EventClient(client, |
|
87 new RMIPushEventRelay(EventClientDelegate.getProxy(client)), |
|
88 null, |
|
89 EventClient.DEFAULT_LEASE_TIMEOUT)); |
|
90 |
|
91 conn.close(); |
|
92 server.stop(); |
|
93 |
|
94 if (succeed) { |
|
95 System.out.println(">>> PublishTest-main: PASSE!"); |
|
96 } else { |
|
97 System.out.println("\n>>> PublishTest-main: FAILED!"); |
|
98 System.exit(1); |
|
99 } |
|
100 } |
|
101 |
|
102 public static boolean test(EventClient efClient) throws Exception { |
|
103 // add listener from the client side |
|
104 Listener listener = new Listener(); |
|
105 efClient.subscribe(emitter, listener, null, null); |
|
106 |
|
107 ObjectName other = new ObjectName("Default:name=other"); |
|
108 // publish notifs |
|
109 for (int i=0; i<sendNB; i++) { |
|
110 Notification notif = new Notification(myType, emitter, count++); |
|
111 Notification notif2 = new Notification(myType, other, 0); |
|
112 //System.out.println(">>> EventManagerService-NotificationEmitter-sendNotifications: "+i); |
|
113 |
|
114 eventManager.publish(emitter, notif); |
|
115 eventManager.publish(other, notif2); // should not received |
|
116 } |
|
117 |
|
118 // waiting |
|
119 long toWait = 6000; |
|
120 long stopTime = System.currentTimeMillis() + toWait; |
|
121 |
|
122 synchronized(listener) { |
|
123 while(listener.received < sendNB && toWait > 0) { |
|
124 listener.wait(toWait); |
|
125 toWait = stopTime - System.currentTimeMillis(); |
|
126 } |
|
127 } |
|
128 |
|
129 // clean |
|
130 efClient.unsubscribe(emitter, listener); |
|
131 efClient.close(); |
|
132 |
|
133 if (listener.received != sendNB) { |
|
134 System.out.println(">>> PublishTest-test: FAILED! Expected to receive "+sendNB+", but got "+listener.received); |
|
135 |
|
136 return false; |
|
137 } else if (listener.seqErr > 0) { |
|
138 System.out.println(">>> PublishTest-test: FAILED! The receiving sequence is not correct."); |
|
139 |
|
140 return false; |
|
141 } else { |
|
142 System.out.println(">>> PublishTest-test: got all expected "+listener.received); |
|
143 return true; |
|
144 } |
|
145 } |
|
146 |
|
147 private static class Listener implements NotificationListener { |
|
148 public int received = 0; |
|
149 public int seqErr = 0; |
|
150 |
|
151 private long lastSeq = -1; |
|
152 |
|
153 public void handleNotification(Notification notif, Object handback) { |
|
154 if (!myType.equals(notif.getType())) { |
|
155 System.out.println(">>> PublishTest-Listener: got unexpected notif: "+notif); |
|
156 System.exit(1); |
|
157 } else if (!emitter.equals(notif.getSource())) { |
|
158 System.out.println(">>> PublishTest-Listener: unknown ObjectName: "+notif.getSource()); |
|
159 System.exit(1); |
|
160 } |
|
161 |
|
162 if (lastSeq == -1) { |
|
163 lastSeq = notif.getSequenceNumber(); |
|
164 } else if (notif.getSequenceNumber() - lastSeq++ != 1) { |
|
165 seqErr++; |
|
166 } |
|
167 |
|
168 System.out.println(">>> PublishTest-Listener: got notif "+notif.getSequenceNumber()); |
|
169 |
|
170 synchronized(this) { |
|
171 if (++received >= sendNB) { |
|
172 this.notify(); |
|
173 } |
|
174 } |
|
175 |
|
176 System.out.println(">>> PublishTest-Listener: received = "+received); |
|
177 } |
|
178 } |
|
179 |
|
180 private static int sendNB = 20; |
|
181 private static long count = 0; |
|
182 |
|
183 private static final String myType = "notification.my_notification"; |
|
184 } |
|