KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > remote > JMXNotificationsTestCase


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.javax.management.remote;
10
11 import java.io.IOException JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import javax.management.MBeanServer JavaDoc;
16 import javax.management.MBeanServerConnection JavaDoc;
17 import javax.management.MBeanServerNotification JavaDoc;
18 import javax.management.Notification JavaDoc;
19 import javax.management.NotificationBroadcasterSupport JavaDoc;
20 import javax.management.NotificationFilter JavaDoc;
21 import javax.management.NotificationFilterSupport JavaDoc;
22 import javax.management.NotificationListener JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24 import javax.management.loading.MLet JavaDoc;
25 import javax.management.relation.MBeanServerNotificationFilter JavaDoc;
26 import javax.management.remote.JMXConnectionNotification JavaDoc;
27 import javax.management.remote.JMXConnector JavaDoc;
28 import javax.management.remote.JMXConnectorFactory JavaDoc;
29 import javax.management.remote.JMXConnectorServer JavaDoc;
30 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
31 import javax.management.remote.JMXServiceURL JavaDoc;
32
33 import mx4j.remote.MX4JRemoteConstants;
34 import test.MX4JTestCase;
35 import test.MutableBoolean;
36 import test.MutableInteger;
37 import test.MutableObject;
38
39 /**
40  * @version $Revision: 1.10 $
41  */

42 public abstract class JMXNotificationsTestCase extends MX4JTestCase
43 {
44    public JMXNotificationsTestCase(String JavaDoc name)
45    {
46       super(name);
47    }
48
49    protected void tearDown() throws Exception JavaDoc
50    {
51       sleep(5000);
52    }
53
54    public abstract JMXServiceURL JavaDoc createJMXConnectorServerAddress() throws MalformedURLException JavaDoc;
55
56    public abstract Map JavaDoc getEnvironment();
57
58    public void testConnectionNotificationOpenedOnServer() throws Exception JavaDoc
59    {
60       JMXConnectorServer JavaDoc cntorServer = null;
61       try
62       {
63          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
64          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer());
65          cntorServer.start();
66          sleep(5000);
67
68          final MutableObject holder = new MutableObject(null);
69          cntorServer.addNotificationListener(new NotificationListener JavaDoc()
70          {
71             public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
72             {
73                holder.set(notification);
74             }
75          }, null, null);
76
77          JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
78
79          Notification JavaDoc notification = (Notification JavaDoc) holder.get();
80          if (!(notification instanceof JMXConnectionNotification JavaDoc)) fail();
81          assertEquals(notification.getType(), JMXConnectionNotification.OPENED);
82       } catch (Exception JavaDoc x)
83       {
84          x.printStackTrace();
85          throw x;
86       } finally
87       {
88          if (cntorServer != null) cntorServer.stop();
89       }
90    }
91
92    public void testConnectionNotificationClosedOnServer() throws Exception JavaDoc
93    {
94       JMXConnectorServer JavaDoc cntorServer = null;
95       try
96       {
97          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
98          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), newMBeanServer());
99          cntorServer.start();
100          sleep(5000);
101
102          final MutableObject holder = new MutableObject(null);
103          cntorServer.addNotificationListener(new NotificationListener JavaDoc()
104          {
105             public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
106             {
107                holder.set(notification);
108             }
109          }, null, null);
110
111          JMXConnector JavaDoc cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
112          cntor.close();
113
114          Notification JavaDoc notification = (Notification JavaDoc) holder.get();
115          if (!(notification instanceof JMXConnectionNotification JavaDoc)) fail();
116          assertEquals(notification.getType(), JMXConnectionNotification.CLOSED);
117       } catch (Exception JavaDoc x)
118       {
119          x.printStackTrace();
120          throw x;
121       } finally
122       {
123          if (cntorServer != null) cntorServer.stop();
124       }
125    }
126
127    public void testConnectionNotificationOpenedOnClient() throws Exception JavaDoc
128    {
129       JMXConnectorServer JavaDoc cntorServer = null;
130       JMXConnector JavaDoc cntor = null;
131       try
132       {
133          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
134          MBeanServer JavaDoc server = newMBeanServer();
135          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), server);
136          cntorServer.start();
137          sleep(5000);
138
139          final MutableObject holder = new MutableObject(null);
140          cntor = JMXConnectorFactory.newJMXConnector(cntorServer.getAddress(), getEnvironment());
141          cntor.addConnectionNotificationListener(new NotificationListener JavaDoc()
142          {
143             public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
144             {
145                holder.set(notification);
146             }
147          }, null, null);
148
149          cntor.connect(getEnvironment());
150
151          JMXConnectionNotification JavaDoc notification = (JMXConnectionNotification JavaDoc) holder.get();
152          assertEquals(notification.getType(), JMXConnectionNotification.OPENED);
153       } catch (Exception JavaDoc x)
154       {
155          x.printStackTrace();
156          throw x;
157       } finally
158       {
159          if (cntor != null) cntor.close();
160          if (cntorServer != null) cntorServer.stop();
161       }
162    }
163
164    public void testConnectionNotificationClosedOnClient() throws Exception JavaDoc
165    {
166       JMXConnectorServer JavaDoc cntorServer = null;
167       JMXConnector JavaDoc cntor = null;
168       try
169       {
170          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
171          MBeanServer JavaDoc server = newMBeanServer();
172          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), server);
173          cntorServer.start();
174          sleep(5000);
175
176          final MutableObject holder = new MutableObject(null);
177          cntor = JMXConnectorFactory.newJMXConnector(cntorServer.getAddress(), getEnvironment());
178          cntor.addConnectionNotificationListener(new NotificationListener JavaDoc()
179          {
180             public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
181             {
182                holder.set(notification);
183             }
184          }, null, null);
185
186          cntor.connect(getEnvironment());
187          cntor.close();
188
189          JMXConnectionNotification JavaDoc notification = (JMXConnectionNotification JavaDoc) holder.get();
190          assertEquals(notification.getType(), JMXConnectionNotification.CLOSED);
191       } catch (Exception JavaDoc x)
192       {
193          x.printStackTrace();
194          throw x;
195       } finally
196       {
197          if (cntor != null) cntor.close();
198          if (cntorServer != null) cntorServer.stop();
199       }
200    }
201
202    public void testConnectionNotificationFailedOnClient() throws Exception JavaDoc
203    {
204       JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
205       MBeanServer JavaDoc server = newMBeanServer();
206       JMXConnectorServer JavaDoc cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), server);
207
208       cntorServer.start();
209       sleep(5000);
210
211       final MutableObject holder = new MutableObject(null);
212       long period = 1000;
213       int retries = 3;
214       try
215       {
216          JMXConnector JavaDoc cntor = JMXConnectorFactory.newJMXConnector(cntorServer.getAddress(), getEnvironment());
217          cntor.addConnectionNotificationListener(new NotificationListener JavaDoc()
218          {
219             public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
220             {
221                holder.set(notification);
222             }
223          }, null, null);
224
225          Map JavaDoc clientEnv = getEnvironment();
226          clientEnv.put(MX4JRemoteConstants.CONNECTION_HEARTBEAT_PERIOD, new Long JavaDoc(period));
227          clientEnv.put(MX4JRemoteConstants.CONNECTION_HEARTBEAT_RETRIES, new Integer JavaDoc(retries));
228          cntor.connect(clientEnv);
229
230          JMXConnectionNotification JavaDoc notification = (JMXConnectionNotification JavaDoc) holder.get();
231          assertEquals(notification.getType(), JMXConnectionNotification.OPENED);
232          holder.set(null);
233       } catch (Exception JavaDoc x)
234       {
235          x.printStackTrace();
236          throw x;
237       } finally
238       {
239          cntorServer.stop();
240          sleep(5000);
241       }
242
243       // Wait for the heartbeat to send the failed notification
244
sleep((retries * 3) * period);
245
246       JMXConnectionNotification JavaDoc notification = (JMXConnectionNotification JavaDoc) holder.get();
247       assertNotNull(notification);
248       assertEquals(notification.getType(), JMXConnectionNotification.FAILED);
249    }
250
251    public void testRemoteNotificationListener() throws Exception JavaDoc
252    {
253       JMXConnectorServer JavaDoc cntorServer = null;
254       JMXConnector JavaDoc cntor = null;
255       try
256       {
257          MBeanServer JavaDoc server = newMBeanServer();
258
259          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
260          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), server);
261          cntorServer.start();
262          sleep(5000);
263
264          cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
265          MBeanServerConnection JavaDoc mbsc = cntor.getMBeanServerConnection();
266          ObjectName JavaDoc delegate = new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate");
267
268          final MutableObject holder = new MutableObject(null);
269          mbsc.addNotificationListener(delegate, new NotificationListener JavaDoc()
270          {
271             public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
272             {
273                holder.set(notification);
274             }
275          }, null, null);
276
277          // Wait for notifications threads to start
278
sleep(1000);
279
280          // Register a new MBean, it will generate a notification
281
MLet JavaDoc mlet = new MLet JavaDoc();
282          ObjectName JavaDoc name = new ObjectName JavaDoc(":mbean=mlet");
283          server.registerMBean(mlet, name);
284
285          // Wait for notifications to arrive
286
sleep(1000);
287
288          Notification JavaDoc notification = (Notification JavaDoc) holder.get();
289          assertEquals(notification.getType(), MBeanServerNotification.REGISTRATION_NOTIFICATION);
290          holder.set(null);
291
292          // Unregister the MBean
293
server.unregisterMBean(name);
294
295          // Wait for notifications to arrive
296
sleep(1000);
297
298          notification = (Notification JavaDoc) holder.get();
299          assertEquals(notification.getType(), MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
300          holder.set(null);
301       } catch (Exception JavaDoc x)
302       {
303          x.printStackTrace();
304          throw x;
305       } finally
306       {
307          if (cntor != null) cntor.close();
308          if (cntorServer != null) cntorServer.stop();
309       }
310    }
311
312    public void testNonSerializableNotifications() throws Exception JavaDoc
313    {
314       JMXConnectorServer JavaDoc cntorServer = null;
315       JMXConnector JavaDoc cntor = null;
316       try
317       {
318          MBeanServer JavaDoc server = newMBeanServer();
319
320          // Register an MBean Emitter
321
ObjectName JavaDoc emitterName = ObjectName.getInstance(":mbean=emitter");
322          MBeanEmitter emitter = new MBeanEmitter();
323          server.registerMBean(emitter, emitterName);
324
325          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
326          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), null);
327          ObjectName JavaDoc cntorName = ObjectName.getInstance("connector:protocol=rmi");
328          server.registerMBean(cntorServer, cntorName);
329          cntorServer.start();
330          sleep(5000);
331
332          cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
333          MBeanServerConnection JavaDoc mbsc = cntor.getMBeanServerConnection();
334          final MutableObject plainNotification = new MutableObject(null);
335          mbsc.addNotificationListener(emitterName, new NotificationListener JavaDoc()
336          {
337             public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
338             {
339                plainNotification.set(notification);
340             }
341          }, null, null);
342
343          final MutableObject connectionNotification = new MutableObject(null);
344          cntor.addConnectionNotificationListener(new NotificationListener JavaDoc()
345          {
346             public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
347             {
348                connectionNotification.set(notification);
349             }
350          }, null, null);
351
352          // Wait for notifications threads to start
353
sleep(1000);
354
355          String JavaDoc type = "notification.type";
356          Notification JavaDoc notification = new Notification JavaDoc(type, this, 0);
357          // Make it non-serializable
358
notification.setUserData(this);
359          emitter.emit(notification);
360
361          // Wait for notifications to arrive
362
sleep(1000);
363
364          assertNull(plainNotification.get());
365          assertNotNull(connectionNotification.get());
366          assertEquals(((Notification JavaDoc) connectionNotification.get()).getType(), JMXConnectionNotification.NOTIFS_LOST);
367       } catch (Exception JavaDoc x)
368       {
369          x.printStackTrace();
370          throw x;
371       } finally
372       {
373          if (cntor != null) cntor.close();
374          if (cntorServer != null) cntorServer.stop();
375       }
376    }
377
378    public void testAddRemoveMBeanListener() throws Exception JavaDoc
379    {
380       JMXConnectorServer JavaDoc cntorServer = null;
381       JMXConnector JavaDoc cntor = null;
382       try
383       {
384          MBeanServer JavaDoc server = newMBeanServer();
385
386          // Register an MBean Emitter
387
ObjectName JavaDoc emitterName = ObjectName.getInstance(":mbean=emitter");
388          MBeanEmitter emitter = new MBeanEmitter();
389          server.registerMBean(emitter, emitterName);
390
391          // Register an MBean Listener
392
MutableObject notificationHolder = new MutableObject(null);
393          MutableObject handbackHolder = new MutableObject(null);
394          ObjectName JavaDoc listenerName = ObjectName.getInstance(":mbean=listener");
395          MBeanListener listener = new MBeanListener(notificationHolder, handbackHolder);
396          server.registerMBean(listener, listenerName);
397
398          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
399          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), server);
400          cntorServer.start();
401          sleep(5000);
402
403          cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
404          MBeanServerConnection JavaDoc mbsc = cntor.getMBeanServerConnection();
405
406          // Non-serializable filter
407
try
408          {
409             mbsc.addNotificationListener(emitterName, listenerName, new NotificationFilter JavaDoc()
410             {
411                public boolean isNotificationEnabled(Notification JavaDoc notification)
412                {
413                   return false;
414                }
415             }, null);
416             fail();
417          } catch (IOException JavaDoc x)
418          {
419          }
420
421          // Non-serializable handback
422
try
423          {
424             mbsc.addNotificationListener(emitterName, listenerName, null, new Object JavaDoc());
425             fail();
426          } catch (IOException JavaDoc x)
427          {
428          }
429
430          // Non-serializable filter and non serializable handback
431
try
432          {
433             mbsc.addNotificationListener(emitterName, listenerName, new NotificationFilter JavaDoc()
434             {
435                public boolean isNotificationEnabled(Notification JavaDoc notification)
436                {
437                   return false;
438                }
439             }, new Object JavaDoc());
440             fail();
441          } catch (IOException JavaDoc x)
442          {
443          }
444
445          // Everything is serializable
446
ObjectName JavaDoc name = ObjectName.getInstance(":mbean=dummy");
447          MBeanServerNotificationFilter JavaDoc filter = new MBeanServerNotificationFilter JavaDoc();
448          filter.disableObjectName(name);
449          Object JavaDoc handback = new Integer JavaDoc(13);
450          mbsc.addNotificationListener(emitterName, listenerName, filter, handback);
451
452          // Wait for notifications threads to start
453
sleep(1000);
454
455          Notification JavaDoc notification = new MBeanServerNotification JavaDoc(MBeanServerNotification.REGISTRATION_NOTIFICATION, this, 0, name);
456          emitter.emit(notification);
457
458          // Wait for notification to arrive
459
sleep(1000);
460
461          // Be sure the notification has been filtered
462
assertNull(notificationHolder.get());
463          assertNull(handbackHolder.get());
464
465          // Disable filtering
466
filter.enableAllObjectNames();
467          // Remove and readd: on server side there is a serialized copy of the filter
468
mbsc.removeNotificationListener(emitterName, listenerName);
469          mbsc.addNotificationListener(emitterName, listenerName, filter, handback);
470
471          // Wait for notifications threads to start
472
sleep(1000);
473
474          emitter.emit(notification);
475
476          // Wait for notification to arrive
477
sleep(1000);
478
479          // Be sure we got it
480
assertEquals(handbackHolder.get(), handback);
481          Notification JavaDoc emitted = (Notification JavaDoc) notificationHolder.get();
482          assertNotNull(emitted);
483          if (!(notification instanceof MBeanServerNotification JavaDoc)) fail();
484          assertEquals(((MBeanServerNotification JavaDoc) emitted).getMBeanName(), name);
485          notificationHolder.set(null);
486          handbackHolder.set(null);
487
488          mbsc.removeNotificationListener(emitterName, listenerName, filter, handback);
489
490          // Be sure we don't get notifications anymore
491
emitter.emit(notification);
492
493          // Wait for notification to arrive
494
sleep(1000);
495
496          assertNull(notificationHolder.get());
497          assertNull(handbackHolder.get());
498       } catch (Exception JavaDoc x)
499       {
500          x.printStackTrace();
501          throw x;
502       } finally
503       {
504          if (cntor != null) cntor.close();
505          if (cntorServer != null) cntorServer.stop();
506       }
507    }
508
509    public void testAddRemoveListenerWithNonSerializableFilter() throws Exception JavaDoc
510    {
511       JMXConnectorServer JavaDoc cntorServer = null;
512       JMXConnector JavaDoc cntor = null;
513       try
514       {
515          MBeanServer JavaDoc server = newMBeanServer();
516
517          // Register an MBean Emitter
518
ObjectName JavaDoc emitterName = ObjectName.getInstance(":mbean=emitter");
519          MBeanEmitter emitter = new MBeanEmitter();
520          server.registerMBean(emitter, emitterName);
521
522          MutableObject notificationHolder = new MutableObject(null);
523          MutableObject handbackHolder = new MutableObject(null);
524          MBeanListener listener = new MBeanListener(notificationHolder, handbackHolder);
525
526          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
527          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), server);
528          cntorServer.start();
529          sleep(5000);
530
531          cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
532          MBeanServerConnection JavaDoc mbsc = cntor.getMBeanServerConnection();
533
534          // Non-serializable filter, should run on client side
535
final MutableBoolean enable = new MutableBoolean(false);
536          NotificationFilter JavaDoc filter = new NotificationFilter JavaDoc()
537          {
538             public boolean isNotificationEnabled(Notification JavaDoc notification)
539             {
540                return enable.get();
541             }
542          };
543          mbsc.addNotificationListener(emitterName, listener, filter, null);
544
545          // Wait for notification threads to start
546
sleep(1000);
547
548          String JavaDoc type = "notification.type";
549          Notification JavaDoc notification = new Notification JavaDoc(type, this, 0);
550          emitter.emit(notification);
551
552          // Wait for notification to arrive
553
sleep(1000);
554
555          // Be sure the notification has been filtered
556
assertNull(notificationHolder.get());
557          assertNull(handbackHolder.get());
558
559          // Disable the filter
560
enable.set(true);
561
562          emitter.emit(notification);
563
564          // Wait for notification to arrive
565
sleep(1000);
566
567          // Be sure we got the notification
568
assertNull(handbackHolder.get());
569          Notification JavaDoc emitted = (Notification JavaDoc) notificationHolder.get();
570          assertNotNull(emitted);
571          notificationHolder.set(null);
572          handbackHolder.set(null);
573
574          mbsc.removeNotificationListener(emitterName, listener, filter, null);
575
576          // Be sure we don't get notifications anymore
577
emitter.emit(notification);
578
579          // Wait for notification to arrive
580
sleep(1000);
581
582          assertNull(notificationHolder.get());
583          assertNull(handbackHolder.get());
584       } catch (Exception JavaDoc x)
585       {
586          x.printStackTrace();
587          throw x;
588       } finally
589       {
590          if (cntor != null) cntor.close();
591          if (cntorServer != null) cntorServer.stop();
592       }
593    }
594
595    public void testAddRemoveListenerWithNonSerializableHandback() throws Exception JavaDoc
596    {
597       JMXConnectorServer JavaDoc cntorServer = null;
598       JMXConnector JavaDoc cntor = null;
599       try
600       {
601          MBeanServer JavaDoc server = newMBeanServer();
602
603          // Register an MBean Emitter
604
ObjectName JavaDoc emitterName = ObjectName.getInstance(":mbean=emitter");
605          MBeanEmitter emitter = new MBeanEmitter();
606          server.registerMBean(emitter, emitterName);
607
608          MutableObject notificationHolder = new MutableObject(null);
609          MutableObject handbackHolder = new MutableObject(null);
610          MBeanListener listener = new MBeanListener(notificationHolder, handbackHolder);
611
612          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
613          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), server);
614          cntorServer.start();
615          sleep(5000);
616
617          cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
618          MBeanServerConnection JavaDoc mbsc = cntor.getMBeanServerConnection();
619
620          // Non-serializable handback, should stay on client side
621
Object JavaDoc handback = new Object JavaDoc();
622          mbsc.addNotificationListener(emitterName, listener, null, handback);
623
624          // Wait for notification threads to start
625
sleep(1000);
626
627          String JavaDoc type = "notification.type";
628          Notification JavaDoc notification = new Notification JavaDoc(type, this, 0);
629          emitter.emit(notification);
630
631          // Wait for notification to arrive
632
sleep(1000);
633
634          // Be sure we got the notification
635
assertSame(handbackHolder.get(), handback);
636          Notification JavaDoc emitted = (Notification JavaDoc) notificationHolder.get();
637          assertNotNull(emitted);
638          notificationHolder.set(null);
639          handbackHolder.set(null);
640
641          mbsc.removeNotificationListener(emitterName, listener, null, handback);
642
643          // Be sure we don't get notifications anymore
644
emitter.emit(notification);
645
646          // Wait for notification to arrive
647
sleep(1000);
648
649          assertNull(notificationHolder.get());
650          assertNull(handbackHolder.get());
651       } catch (Exception JavaDoc x)
652       {
653          x.printStackTrace();
654          throw x;
655       } finally
656       {
657          if (cntor != null) cntor.close();
658          if (cntorServer != null) cntorServer.stop();
659       }
660    }
661
662    public void testAddRemoveSameListenerMultipleTimesWithDifferentFiltersAndHandbacks() throws Exception JavaDoc
663    {
664       JMXConnectorServer JavaDoc cntorServer = null;
665       JMXConnector JavaDoc cntor = null;
666       try
667       {
668          MBeanServer JavaDoc server = newMBeanServer();
669
670          // Register an MBean Emitter
671
ObjectName JavaDoc emitterName = ObjectName.getInstance(":mbean=emitter");
672          MBeanEmitter emitter = new MBeanEmitter();
673          server.registerMBean(emitter, emitterName);
674
675          final MutableInteger counter1 = new MutableInteger(0);
676          NotificationListener JavaDoc listener1 = new NotificationListener JavaDoc()
677          {
678             public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
679             {
680                counter1.set(counter1.get() + 1);
681             }
682          };
683          final MutableInteger counter2 = new MutableInteger(0);
684          NotificationListener JavaDoc listener2 = new NotificationListener JavaDoc()
685          {
686             public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
687             {
688                counter2.set(counter2.get() + 1);
689             }
690          };
691
692          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
693          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), server);
694          cntorServer.start();
695          sleep(5000);
696
697          cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
698          MBeanServerConnection JavaDoc mbsc = cntor.getMBeanServerConnection();
699
700          String JavaDoc type = "notification.type";
701
702          // First listener
703
mbsc.addNotificationListener(emitterName, listener1, null, null);
704          // Second listener
705
NotificationFilterSupport JavaDoc filter = new NotificationFilterSupport JavaDoc();
706          filter.enableType(type);
707          mbsc.addNotificationListener(emitterName, listener1, filter, null);
708          // Third listener
709
Object JavaDoc handback = new Object JavaDoc();
710          mbsc.addNotificationListener(emitterName, listener1, null, handback);
711          // Fourth listener
712
mbsc.addNotificationListener(emitterName, listener2, null, null);
713
714          // Wait for notification threads to start
715
sleep(1000);
716
717          Notification JavaDoc notification = new Notification JavaDoc(type, this, 0);
718          emitter.emit(notification);
719          // Wait for notification to arrive
720
sleep(1000);
721
722          // Be sure we got all notifications
723
assertEquals(counter1.get(), 3);
724          assertEquals(counter2.get(), 1);
725          counter1.set(0);
726          counter2.set(0);
727
728          // Remove one listener
729
mbsc.removeNotificationListener(emitterName, listener1, null, handback);
730
731          emitter.emit(notification);
732          // Wait for notification to arrive
733
sleep(1000);
734
735          assertEquals(counter1.get(), 2);
736          assertEquals(counter2.get(), 1);
737          counter1.set(0);
738          counter2.set(0);
739
740          // Remove all listeners
741
mbsc.removeNotificationListener(emitterName, listener1);
742
743          emitter.emit(notification);
744          // Wait for notification to arrive
745
sleep(1000);
746
747          assertEquals(counter1.get(), 0);
748          assertEquals(counter2.get(), 1);
749          counter1.set(0);
750          counter2.set(0);
751
752          mbsc.removeNotificationListener(emitterName, listener2);
753
754          emitter.emit(notification);
755          // Wait for notification to arrive
756
sleep(1000);
757
758          assertEquals(counter1.get(), 0);
759          assertEquals(counter2.get(), 0);
760       } catch (Exception JavaDoc x)
761       {
762          x.printStackTrace();
763          throw x;
764       } finally
765       {
766          if (cntor != null) cntor.close();
767          if (cntorServer != null) cntorServer.stop();
768       }
769    }
770
771    public void testTwoMBeanServerConnectionsHaveSameListener() throws Exception JavaDoc
772    {
773       JMXConnectorServer JavaDoc cntorServer = null;
774       JMXConnector JavaDoc cntor = null;
775       try
776       {
777          MBeanServer JavaDoc server = newMBeanServer();
778
779          // Register an MBean Emitter
780
ObjectName JavaDoc emitterName = ObjectName.getInstance(":mbean=emitter");
781          MBeanEmitter emitter = new MBeanEmitter();
782          server.registerMBean(emitter, emitterName);
783
784          final MutableInteger counter = new MutableInteger(0);
785          NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
786          {
787             public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
788             {
789                counter.set(counter.get() + 1);
790             }
791          };
792
793          JMXServiceURL JavaDoc url = createJMXConnectorServerAddress();
794          cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), server);
795          cntorServer.start();
796          sleep(5000);
797
798          cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
799          MBeanServerConnection JavaDoc mbsc1 = cntor.getMBeanServerConnection();
800
801          mbsc1.addNotificationListener(emitterName, listener, null, null);
802          // Wait for notification threads to start
803
sleep(1000);
804
805          Notification JavaDoc notification = new Notification JavaDoc("type", emitter, 0);
806          emitter.emit(notification);
807
808          // Wait for the notification to arrive
809
sleep(1000);
810
811          // Be sure it's received
812
assertEquals(counter.get(), 1);
813
814          // Be sure I can remove the same listener from another MBeanServerConnection
815
MBeanServerConnection JavaDoc mbsc2 = cntor.getMBeanServerConnection();
816          mbsc2.removeNotificationListener(emitterName, listener, null, null);
817
818          emitter.emit(notification);
819
820          // Be sure no listeners anymore
821
assertEquals(counter.get(), 1);
822       } catch (Exception JavaDoc x)
823       {
824          x.printStackTrace();
825          throw x;
826       } finally
827       {
828          if (cntor != null) cntor.close();
829          if (cntorServer != null) cntorServer.stop();
830       }
831    }
832
833    public interface MBeanListenerMBean
834    {
835    }
836
837    public static class MBeanListener implements NotificationListener JavaDoc, MBeanListenerMBean
838    {
839       private MutableObject notificationHolder;
840       private MutableObject handbackHolder;
841
842       public MBeanListener(MutableObject notificationHolder, MutableObject handbackHolder)
843       {
844          this.notificationHolder = notificationHolder;
845          this.handbackHolder = handbackHolder;
846       }
847
848       public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
849       {
850          notificationHolder.set(notification);
851          handbackHolder.set(handback);
852       }
853    }
854
855    public interface MBeanEmitterMBean
856    {
857       public void emit(Notification JavaDoc notification);
858    }
859
860    public static class MBeanEmitter extends NotificationBroadcasterSupport JavaDoc implements MBeanEmitterMBean
861    {
862       public void emit(Notification JavaDoc notification)
863       {
864          sendNotification(notification);
865       }
866    }
867 }
868
Popular Tags