Search in sources :

Example 16 with ListenerNotFoundException

use of javax.management.ListenerNotFoundException in project geode by apache.

the class DistributedSystemDUnitTest method testNotificationHub.

@Test
public void testNotificationHub() throws Exception {
    this.managementTestRule.createMembers();
    this.managementTestRule.createManagers();
    class NotificationHubTestListener implements NotificationListener {

        @Override
        public synchronized void handleNotification(Notification notification, Object handback) {
            logger.info("Notification received {}", notification);
            notifications.add(notification);
        }
    }
    this.managerVM.invoke("addListenerToMemberMXBean", () -> {
        ManagementService service = this.managementTestRule.getManagementService();
        DistributedSystemMXBean distributedSystemMXBean = service.getDistributedSystemMXBean();
        await().until(() -> assertThat(distributedSystemMXBean.listMemberObjectNames()).hasSize(5));
        for (ObjectName objectName : distributedSystemMXBean.listMemberObjectNames()) {
            NotificationHubTestListener listener = new NotificationHubTestListener();
            getPlatformMBeanServer().addNotificationListener(objectName, listener, null, null);
            notificationListenerMap.put(objectName, listener);
        }
    });
    for (VM memberVM : this.memberVMs) {
        memberVM.invoke("checkNotificationHubListenerCount", () -> {
            SystemManagementService service = this.managementTestRule.getSystemManagementService();
            NotificationHub notificationHub = service.getNotificationHub();
            Map<ObjectName, NotificationHubListener> listenerMap = notificationHub.getListenerObjectMap();
            assertThat(listenerMap.keySet()).hasSize(1);
            ObjectName memberMBeanName = getMemberMBeanName(this.managementTestRule.getDistributedMember());
            NotificationHubListener listener = listenerMap.get(memberMBeanName);
            /*
         * Counter of listener should be 2 . One for default Listener which is added for each member
         * mbean by distributed system mbean One for the added listener in test
         */
            assertThat(listener.getNumCounter()).isEqualTo(2);
            // Raise some notifications
            NotificationBroadcasterSupport notifier = (MemberMBean) service.getMemberMXBean();
            String memberSource = getMemberNameOrId(this.managementTestRule.getDistributedMember());
            // Only a dummy notification , no actual region is created
            Notification notification = new Notification(JMXNotificationType.REGION_CREATED, memberSource, SequenceNumber.next(), System.currentTimeMillis(), ManagementConstants.REGION_CREATED_PREFIX + "/test");
            notifier.sendNotification(notification);
        });
    }
    this.managerVM.invoke("checkNotificationsAndRemoveListeners", () -> {
        await().until(() -> assertThat(notifications).hasSize(3));
        notifications.clear();
        for (ObjectName objectName : notificationListenerMap.keySet()) {
            NotificationListener listener = notificationListenerMap.get(objectName);
            getPlatformMBeanServer().removeNotificationListener(objectName, listener);
        }
    });
    for (VM memberVM : this.memberVMs) {
        memberVM.invoke("checkNotificationHubListenerCountAgain", () -> {
            SystemManagementService service = this.managementTestRule.getSystemManagementService();
            NotificationHub hub = service.getNotificationHub();
            Map<ObjectName, NotificationHubListener> listenerObjectMap = hub.getListenerObjectMap();
            assertThat(listenerObjectMap.keySet().size()).isEqualTo(1);
            ObjectName memberMBeanName = getMemberMBeanName(this.managementTestRule.getDistributedMember());
            NotificationHubListener listener = listenerObjectMap.get(memberMBeanName);
            /*
         * Counter of listener should be 1 for the default Listener which is added for each member
         * mbean by distributed system mbean.
         */
            assertThat(listener.getNumCounter()).isEqualTo(1);
        });
    }
    this.managerVM.invoke("removeListenerFromMemberMXBean", () -> {
        ManagementService service = this.managementTestRule.getManagementService();
        DistributedSystemMXBean distributedSystemMXBean = service.getDistributedSystemMXBean();
        await().until(() -> assertThat(distributedSystemMXBean.listMemberObjectNames()).hasSize(5));
        for (ObjectName objectName : distributedSystemMXBean.listMemberObjectNames()) {
            NotificationHubTestListener listener = new NotificationHubTestListener();
            try {
                // because new
                getPlatformMBeanServer().removeNotificationListener(objectName, listener);
            // instance!!
            } catch (ListenerNotFoundException e) {
            // TODO: [old] apparently there is never a notification listener on any these mbeans at
            // this point [fix this]
            // fix this test so it doesn't hit these unexpected exceptions -- getLogWriter().error(e);
            }
        }
    });
    for (VM memberVM : this.memberVMs) {
        memberVM.invoke("verifyNotificationHubListenersWereRemoved", () -> {
            SystemManagementService service = this.managementTestRule.getSystemManagementService();
            NotificationHub notificationHub = service.getNotificationHub();
            notificationHub.cleanUpListeners();
            assertThat(notificationHub.getListenerObjectMap()).isEmpty();
            for (ObjectName objectName : notificationListenerMap.keySet()) {
                NotificationListener listener = notificationListenerMap.get(objectName);
                assertThatThrownBy(() -> getPlatformMBeanServer().removeNotificationListener(objectName, listener)).isExactlyInstanceOf(ListenerNotFoundException.class);
            }
        });
    }
}
Also used : Notification(javax.management.Notification) SystemManagementService(org.apache.geode.management.internal.SystemManagementService) ObjectName(javax.management.ObjectName) NotificationHubListener(org.apache.geode.management.internal.NotificationHub.NotificationHubListener) SystemManagementService(org.apache.geode.management.internal.SystemManagementService) VM(org.apache.geode.test.dunit.VM) MemberMBean(org.apache.geode.management.internal.beans.MemberMBean) ListenerNotFoundException(javax.management.ListenerNotFoundException) NotificationBroadcasterSupport(javax.management.NotificationBroadcasterSupport) NotificationHub(org.apache.geode.management.internal.NotificationHub) NotificationListener(javax.management.NotificationListener) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

ListenerNotFoundException (javax.management.ListenerNotFoundException)16 NotificationEmitter (javax.management.NotificationEmitter)4 NotificationListener (javax.management.NotificationListener)4 ObjectName (javax.management.ObjectName)4 ArrayList (java.util.ArrayList)3 InstanceNotFoundException (javax.management.InstanceNotFoundException)3 Notification (javax.management.Notification)3 DynamicMBean (javax.management.DynamicMBean)2 RuntimeOperationsException (javax.management.RuntimeOperationsException)2 Test (org.junit.Test)2 NamedObject (com.sun.jmx.mbeanserver.NamedObject)1 ServerNotifForwarder (com.sun.jmx.remote.internal.ServerNotifForwarder)1 Component (java.awt.Component)1 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)1 WeakReference (java.lang.ref.WeakReference)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1