use of org.apache.geode.management.internal.beans.MemberMBean 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);
}
});
}
}
Aggregations