Search in sources :

Example 1 with NotificationHubListener

use of org.apache.geode.management.internal.NotificationHub.NotificationHubListener in project geode by apache.

the class CacheManagementDUnitTest method createNotificationRegion.

private void createNotificationRegion(final String memberId) {
    SystemManagementService service = this.managementTestRule.getSystemManagementService();
    Map<ObjectName, NotificationHubListener> notificationHubListenerMap = service.getNotificationHub().getListenerObjectMap();
    await().until(() -> assertThat(notificationHubListenerMap.size()).isEqualTo(1));
    RegionFactory regionFactory = this.managementTestRule.getCache().createRegionFactory(RegionShortcut.REPLICATE);
    for (int i = 1; i <= 15; i++) {
        regionFactory.create(NOTIFICATION_REGION_NAME + i);
    }
    Region region = this.managementTestRule.getCache().getRegion(ManagementConstants.NOTIFICATION_REGION + "_" + memberId);
    assertThat(region).isEmpty();
}
Also used : NotificationHubListener(org.apache.geode.management.internal.NotificationHub.NotificationHubListener) RegionFactory(org.apache.geode.cache.RegionFactory) Region(org.apache.geode.cache.Region) SystemManagementService(org.apache.geode.management.internal.SystemManagementService) ObjectName(javax.management.ObjectName)

Example 2 with NotificationHubListener

use of org.apache.geode.management.internal.NotificationHub.NotificationHubListener 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

ObjectName (javax.management.ObjectName)2 NotificationHubListener (org.apache.geode.management.internal.NotificationHub.NotificationHubListener)2 SystemManagementService (org.apache.geode.management.internal.SystemManagementService)2 ListenerNotFoundException (javax.management.ListenerNotFoundException)1 Notification (javax.management.Notification)1 NotificationBroadcasterSupport (javax.management.NotificationBroadcasterSupport)1 NotificationListener (javax.management.NotificationListener)1 Region (org.apache.geode.cache.Region)1 RegionFactory (org.apache.geode.cache.RegionFactory)1 NotificationHub (org.apache.geode.management.internal.NotificationHub)1 MemberMBean (org.apache.geode.management.internal.beans.MemberMBean)1 VM (org.apache.geode.test.dunit.VM)1 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)1 Test (org.junit.Test)1