1 8 9 package test.javax.management.remote; 10 11 import java.io.IOException ; 12 import java.net.MalformedURLException ; 13 import java.util.Map ; 14 15 import javax.management.MBeanServer ; 16 import javax.management.MBeanServerConnection ; 17 import javax.management.MBeanServerNotification ; 18 import javax.management.Notification ; 19 import javax.management.NotificationBroadcasterSupport ; 20 import javax.management.NotificationFilter ; 21 import javax.management.NotificationFilterSupport ; 22 import javax.management.NotificationListener ; 23 import javax.management.ObjectName ; 24 import javax.management.loading.MLet ; 25 import javax.management.relation.MBeanServerNotificationFilter ; 26 import javax.management.remote.JMXConnectionNotification ; 27 import javax.management.remote.JMXConnector ; 28 import javax.management.remote.JMXConnectorFactory ; 29 import javax.management.remote.JMXConnectorServer ; 30 import javax.management.remote.JMXConnectorServerFactory ; 31 import javax.management.remote.JMXServiceURL ; 32 33 import mx4j.remote.MX4JRemoteConstants; 34 import test.MX4JTestCase; 35 import test.MutableBoolean; 36 import test.MutableInteger; 37 import test.MutableObject; 38 39 42 public abstract class JMXNotificationsTestCase extends MX4JTestCase 43 { 44 public JMXNotificationsTestCase(String name) 45 { 46 super(name); 47 } 48 49 protected void tearDown() throws Exception 50 { 51 sleep(5000); 52 } 53 54 public abstract JMXServiceURL createJMXConnectorServerAddress() throws MalformedURLException ; 55 56 public abstract Map getEnvironment(); 57 58 public void testConnectionNotificationOpenedOnServer() throws Exception 59 { 60 JMXConnectorServer cntorServer = null; 61 try 62 { 63 JMXServiceURL 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 () 70 { 71 public void handleNotification(Notification notification, Object handback) 72 { 73 holder.set(notification); 74 } 75 }, null, null); 76 77 JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment()); 78 79 Notification notification = (Notification ) holder.get(); 80 if (!(notification instanceof JMXConnectionNotification )) fail(); 81 assertEquals(notification.getType(), JMXConnectionNotification.OPENED); 82 } catch (Exception 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 93 { 94 JMXConnectorServer cntorServer = null; 95 try 96 { 97 JMXServiceURL 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 () 104 { 105 public void handleNotification(Notification notification, Object handback) 106 { 107 holder.set(notification); 108 } 109 }, null, null); 110 111 JMXConnector cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment()); 112 cntor.close(); 113 114 Notification notification = (Notification ) holder.get(); 115 if (!(notification instanceof JMXConnectionNotification )) fail(); 116 assertEquals(notification.getType(), JMXConnectionNotification.CLOSED); 117 } catch (Exception 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 128 { 129 JMXConnectorServer cntorServer = null; 130 JMXConnector cntor = null; 131 try 132 { 133 JMXServiceURL url = createJMXConnectorServerAddress(); 134 MBeanServer 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 () 142 { 143 public void handleNotification(Notification notification, Object handback) 144 { 145 holder.set(notification); 146 } 147 }, null, null); 148 149 cntor.connect(getEnvironment()); 150 151 JMXConnectionNotification notification = (JMXConnectionNotification ) holder.get(); 152 assertEquals(notification.getType(), JMXConnectionNotification.OPENED); 153 } catch (Exception 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 165 { 166 JMXConnectorServer cntorServer = null; 167 JMXConnector cntor = null; 168 try 169 { 170 JMXServiceURL url = createJMXConnectorServerAddress(); 171 MBeanServer 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 () 179 { 180 public void handleNotification(Notification notification, Object handback) 181 { 182 holder.set(notification); 183 } 184 }, null, null); 185 186 cntor.connect(getEnvironment()); 187 cntor.close(); 188 189 JMXConnectionNotification notification = (JMXConnectionNotification ) holder.get(); 190 assertEquals(notification.getType(), JMXConnectionNotification.CLOSED); 191 } catch (Exception 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 203 { 204 JMXServiceURL url = createJMXConnectorServerAddress(); 205 MBeanServer server = newMBeanServer(); 206 JMXConnectorServer 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 cntor = JMXConnectorFactory.newJMXConnector(cntorServer.getAddress(), getEnvironment()); 217 cntor.addConnectionNotificationListener(new NotificationListener () 218 { 219 public void handleNotification(Notification notification, Object handback) 220 { 221 holder.set(notification); 222 } 223 }, null, null); 224 225 Map clientEnv = getEnvironment(); 226 clientEnv.put(MX4JRemoteConstants.CONNECTION_HEARTBEAT_PERIOD, new Long (period)); 227 clientEnv.put(MX4JRemoteConstants.CONNECTION_HEARTBEAT_RETRIES, new Integer (retries)); 228 cntor.connect(clientEnv); 229 230 JMXConnectionNotification notification = (JMXConnectionNotification ) holder.get(); 231 assertEquals(notification.getType(), JMXConnectionNotification.OPENED); 232 holder.set(null); 233 } catch (Exception x) 234 { 235 x.printStackTrace(); 236 throw x; 237 } finally 238 { 239 cntorServer.stop(); 240 sleep(5000); 241 } 242 243 sleep((retries * 3) * period); 245 246 JMXConnectionNotification notification = (JMXConnectionNotification ) holder.get(); 247 assertNotNull(notification); 248 assertEquals(notification.getType(), JMXConnectionNotification.FAILED); 249 } 250 251 public void testRemoteNotificationListener() throws Exception 252 { 253 JMXConnectorServer cntorServer = null; 254 JMXConnector cntor = null; 255 try 256 { 257 MBeanServer server = newMBeanServer(); 258 259 JMXServiceURL 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 mbsc = cntor.getMBeanServerConnection(); 266 ObjectName delegate = new ObjectName ("JMImplementation:type=MBeanServerDelegate"); 267 268 final MutableObject holder = new MutableObject(null); 269 mbsc.addNotificationListener(delegate, new NotificationListener () 270 { 271 public void handleNotification(Notification notification, Object handback) 272 { 273 holder.set(notification); 274 } 275 }, null, null); 276 277 sleep(1000); 279 280 MLet mlet = new MLet (); 282 ObjectName name = new ObjectName (":mbean=mlet"); 283 server.registerMBean(mlet, name); 284 285 sleep(1000); 287 288 Notification notification = (Notification ) holder.get(); 289 assertEquals(notification.getType(), MBeanServerNotification.REGISTRATION_NOTIFICATION); 290 holder.set(null); 291 292 server.unregisterMBean(name); 294 295 sleep(1000); 297 298 notification = (Notification ) holder.get(); 299 assertEquals(notification.getType(), MBeanServerNotification.UNREGISTRATION_NOTIFICATION); 300 holder.set(null); 301 } catch (Exception 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 313 { 314 JMXConnectorServer cntorServer = null; 315 JMXConnector cntor = null; 316 try 317 { 318 MBeanServer server = newMBeanServer(); 319 320 ObjectName emitterName = ObjectName.getInstance(":mbean=emitter"); 322 MBeanEmitter emitter = new MBeanEmitter(); 323 server.registerMBean(emitter, emitterName); 324 325 JMXServiceURL url = createJMXConnectorServerAddress(); 326 cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), null); 327 ObjectName 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 mbsc = cntor.getMBeanServerConnection(); 334 final MutableObject plainNotification = new MutableObject(null); 335 mbsc.addNotificationListener(emitterName, new NotificationListener () 336 { 337 public void handleNotification(Notification notification, Object handback) 338 { 339 plainNotification.set(notification); 340 } 341 }, null, null); 342 343 final MutableObject connectionNotification = new MutableObject(null); 344 cntor.addConnectionNotificationListener(new NotificationListener () 345 { 346 public void handleNotification(Notification notification, Object handback) 347 { 348 connectionNotification.set(notification); 349 } 350 }, null, null); 351 352 sleep(1000); 354 355 String type = "notification.type"; 356 Notification notification = new Notification (type, this, 0); 357 notification.setUserData(this); 359 emitter.emit(notification); 360 361 sleep(1000); 363 364 assertNull(plainNotification.get()); 365 assertNotNull(connectionNotification.get()); 366 assertEquals(((Notification ) connectionNotification.get()).getType(), JMXConnectionNotification.NOTIFS_LOST); 367 } catch (Exception 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 379 { 380 JMXConnectorServer cntorServer = null; 381 JMXConnector cntor = null; 382 try 383 { 384 MBeanServer server = newMBeanServer(); 385 386 ObjectName emitterName = ObjectName.getInstance(":mbean=emitter"); 388 MBeanEmitter emitter = new MBeanEmitter(); 389 server.registerMBean(emitter, emitterName); 390 391 MutableObject notificationHolder = new MutableObject(null); 393 MutableObject handbackHolder = new MutableObject(null); 394 ObjectName listenerName = ObjectName.getInstance(":mbean=listener"); 395 MBeanListener listener = new MBeanListener(notificationHolder, handbackHolder); 396 server.registerMBean(listener, listenerName); 397 398 JMXServiceURL 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 mbsc = cntor.getMBeanServerConnection(); 405 406 try 408 { 409 mbsc.addNotificationListener(emitterName, listenerName, new NotificationFilter () 410 { 411 public boolean isNotificationEnabled(Notification notification) 412 { 413 return false; 414 } 415 }, null); 416 fail(); 417 } catch (IOException x) 418 { 419 } 420 421 try 423 { 424 mbsc.addNotificationListener(emitterName, listenerName, null, new Object ()); 425 fail(); 426 } catch (IOException x) 427 { 428 } 429 430 try 432 { 433 mbsc.addNotificationListener(emitterName, listenerName, new NotificationFilter () 434 { 435 public boolean isNotificationEnabled(Notification notification) 436 { 437 return false; 438 } 439 }, new Object ()); 440 fail(); 441 } catch (IOException x) 442 { 443 } 444 445 ObjectName name = ObjectName.getInstance(":mbean=dummy"); 447 MBeanServerNotificationFilter filter = new MBeanServerNotificationFilter (); 448 filter.disableObjectName(name); 449 Object handback = new Integer (13); 450 mbsc.addNotificationListener(emitterName, listenerName, filter, handback); 451 452 sleep(1000); 454 455 Notification notification = new MBeanServerNotification (MBeanServerNotification.REGISTRATION_NOTIFICATION, this, 0, name); 456 emitter.emit(notification); 457 458 sleep(1000); 460 461 assertNull(notificationHolder.get()); 463 assertNull(handbackHolder.get()); 464 465 filter.enableAllObjectNames(); 467 mbsc.removeNotificationListener(emitterName, listenerName); 469 mbsc.addNotificationListener(emitterName, listenerName, filter, handback); 470 471 sleep(1000); 473 474 emitter.emit(notification); 475 476 sleep(1000); 478 479 assertEquals(handbackHolder.get(), handback); 481 Notification emitted = (Notification ) notificationHolder.get(); 482 assertNotNull(emitted); 483 if (!(notification instanceof MBeanServerNotification )) fail(); 484 assertEquals(((MBeanServerNotification ) emitted).getMBeanName(), name); 485 notificationHolder.set(null); 486 handbackHolder.set(null); 487 488 mbsc.removeNotificationListener(emitterName, listenerName, filter, handback); 489 490 emitter.emit(notification); 492 493 sleep(1000); 495 496 assertNull(notificationHolder.get()); 497 assertNull(handbackHolder.get()); 498 } catch (Exception 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 510 { 511 JMXConnectorServer cntorServer = null; 512 JMXConnector cntor = null; 513 try 514 { 515 MBeanServer server = newMBeanServer(); 516 517 ObjectName 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 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 mbsc = cntor.getMBeanServerConnection(); 533 534 final MutableBoolean enable = new MutableBoolean(false); 536 NotificationFilter filter = new NotificationFilter () 537 { 538 public boolean isNotificationEnabled(Notification notification) 539 { 540 return enable.get(); 541 } 542 }; 543 mbsc.addNotificationListener(emitterName, listener, filter, null); 544 545 sleep(1000); 547 548 String type = "notification.type"; 549 Notification notification = new Notification (type, this, 0); 550 emitter.emit(notification); 551 552 sleep(1000); 554 555 assertNull(notificationHolder.get()); 557 assertNull(handbackHolder.get()); 558 559 enable.set(true); 561 562 emitter.emit(notification); 563 564 sleep(1000); 566 567 assertNull(handbackHolder.get()); 569 Notification emitted = (Notification ) notificationHolder.get(); 570 assertNotNull(emitted); 571 notificationHolder.set(null); 572 handbackHolder.set(null); 573 574 mbsc.removeNotificationListener(emitterName, listener, filter, null); 575 576 emitter.emit(notification); 578 579 sleep(1000); 581 582 assertNull(notificationHolder.get()); 583 assertNull(handbackHolder.get()); 584 } catch (Exception 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 596 { 597 JMXConnectorServer cntorServer = null; 598 JMXConnector cntor = null; 599 try 600 { 601 MBeanServer server = newMBeanServer(); 602 603 ObjectName 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 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 mbsc = cntor.getMBeanServerConnection(); 619 620 Object handback = new Object (); 622 mbsc.addNotificationListener(emitterName, listener, null, handback); 623 624 sleep(1000); 626 627 String type = "notification.type"; 628 Notification notification = new Notification (type, this, 0); 629 emitter.emit(notification); 630 631 sleep(1000); 633 634 assertSame(handbackHolder.get(), handback); 636 Notification emitted = (Notification ) notificationHolder.get(); 637 assertNotNull(emitted); 638 notificationHolder.set(null); 639 handbackHolder.set(null); 640 641 mbsc.removeNotificationListener(emitterName, listener, null, handback); 642 643 emitter.emit(notification); 645 646 sleep(1000); 648 649 assertNull(notificationHolder.get()); 650 assertNull(handbackHolder.get()); 651 } catch (Exception 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 663 { 664 JMXConnectorServer cntorServer = null; 665 JMXConnector cntor = null; 666 try 667 { 668 MBeanServer server = newMBeanServer(); 669 670 ObjectName 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 listener1 = new NotificationListener () 677 { 678 public void handleNotification(Notification notification, Object handback) 679 { 680 counter1.set(counter1.get() + 1); 681 } 682 }; 683 final MutableInteger counter2 = new MutableInteger(0); 684 NotificationListener listener2 = new NotificationListener () 685 { 686 public void handleNotification(Notification notification, Object handback) 687 { 688 counter2.set(counter2.get() + 1); 689 } 690 }; 691 692 JMXServiceURL 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 mbsc = cntor.getMBeanServerConnection(); 699 700 String type = "notification.type"; 701 702 mbsc.addNotificationListener(emitterName, listener1, null, null); 704 NotificationFilterSupport filter = new NotificationFilterSupport (); 706 filter.enableType(type); 707 mbsc.addNotificationListener(emitterName, listener1, filter, null); 708 Object handback = new Object (); 710 mbsc.addNotificationListener(emitterName, listener1, null, handback); 711 mbsc.addNotificationListener(emitterName, listener2, null, null); 713 714 sleep(1000); 716 717 Notification notification = new Notification (type, this, 0); 718 emitter.emit(notification); 719 sleep(1000); 721 722 assertEquals(counter1.get(), 3); 724 assertEquals(counter2.get(), 1); 725 counter1.set(0); 726 counter2.set(0); 727 728 mbsc.removeNotificationListener(emitterName, listener1, null, handback); 730 731 emitter.emit(notification); 732 sleep(1000); 734 735 assertEquals(counter1.get(), 2); 736 assertEquals(counter2.get(), 1); 737 counter1.set(0); 738 counter2.set(0); 739 740 mbsc.removeNotificationListener(emitterName, listener1); 742 743 emitter.emit(notification); 744 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 sleep(1000); 757 758 assertEquals(counter1.get(), 0); 759 assertEquals(counter2.get(), 0); 760 } catch (Exception 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 772 { 773 JMXConnectorServer cntorServer = null; 774 JMXConnector cntor = null; 775 try 776 { 777 MBeanServer server = newMBeanServer(); 778 779 ObjectName 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 listener = new NotificationListener () 786 { 787 public void handleNotification(Notification notification, Object handback) 788 { 789 counter.set(counter.get() + 1); 790 } 791 }; 792 793 JMXServiceURL 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 mbsc1 = cntor.getMBeanServerConnection(); 800 801 mbsc1.addNotificationListener(emitterName, listener, null, null); 802 sleep(1000); 804 805 Notification notification = new Notification ("type", emitter, 0); 806 emitter.emit(notification); 807 808 sleep(1000); 810 811 assertEquals(counter.get(), 1); 813 814 MBeanServerConnection mbsc2 = cntor.getMBeanServerConnection(); 816 mbsc2.removeNotificationListener(emitterName, listener, null, null); 817 818 emitter.emit(notification); 819 820 assertEquals(counter.get(), 1); 822 } catch (Exception 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 , 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 notification, Object handback) 849 { 850 notificationHolder.set(notification); 851 handbackHolder.set(handback); 852 } 853 } 854 855 public interface MBeanEmitterMBean 856 { 857 public void emit(Notification notification); 858 } 859 860 public static class MBeanEmitter extends NotificationBroadcasterSupport implements MBeanEmitterMBean 861 { 862 public void emit(Notification notification) 863 { 864 sendNotification(notification); 865 } 866 } 867 } 868 | Popular Tags |