use of javax.management.NotificationListener in project intellij-community by JetBrains.
the class PerformanceWatcher method watchCodeCache.
private void watchCodeCache(final MemoryPoolMXBean bean) {
final long threshold = bean.getUsage().getMax() - 5 * 1024 * 1024;
if (!bean.isUsageThresholdSupported() || threshold <= 0)
return;
bean.setUsageThreshold(threshold);
final NotificationEmitter emitter = (NotificationEmitter) ManagementFactory.getMemoryMXBean();
emitter.addNotificationListener(new NotificationListener() {
@Override
public void handleNotification(Notification n, Object hb) {
if (bean.getUsage().getUsed() > threshold) {
LOG.info("Code Cache is almost full");
dumpThreads("codeCacheFull", true);
try {
emitter.removeNotificationListener(this);
} catch (ListenerNotFoundException e) {
LOG.error(e);
}
}
}
}, null, null);
}
use of javax.management.NotificationListener in project geode by apache.
the class MBeanUtil method registerServerNotificationListener.
static void registerServerNotificationListener() {
if (mbeanServer == null) {
return;
}
try {
// the MBeanServerDelegate name is spec'ed as the following...
ObjectName delegate = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");
mbeanServer.addNotificationListener(delegate, new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
MBeanServerNotification serverNotification = (MBeanServerNotification) notification;
if (MBeanServerNotification.UNREGISTRATION_NOTIFICATION.equals(serverNotification.getType())) {
ObjectName objectName = serverNotification.getMBeanName();
synchronized (MBeanUtil.managedResources) {
Object entry = MBeanUtil.managedResources.get(objectName);
if (entry == null)
return;
if (!(entry instanceof ManagedResource)) {
throw new ClassCastException(LocalizedStrings.MBeanUtil_0_IS_NOT_A_MANAGEDRESOURCE.toLocalizedString(new Object[] { entry.getClass().getName() }));
}
ManagedResource resource = (ManagedResource) entry;
{
// call cleanup on managedResource
cleanupResource(resource);
}
}
}
}
}, null, null);
} catch (JMException e) {
logStackTrace(Level.WARN, e, LocalizedStrings.MBeanUtil_FAILED_TO_REGISTER_SERVERNOTIFICATIONLISTENER.toLocalizedString());
} catch (JMRuntimeException e) {
logStackTrace(Level.WARN, e, LocalizedStrings.MBeanUtil_FAILED_TO_REGISTER_SERVERNOTIFICATIONLISTENER.toLocalizedString());
}
}
use of javax.management.NotificationListener 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);
}
});
}
}
use of javax.management.NotificationListener in project geode by apache.
the class CacheManagementDUnitTest method attachListenerToDistributedSystemMXBean.
private void attachListenerToDistributedSystemMXBean(final VM managerVM) {
managerVM.invoke("attachListenerToDistributedSystemMXBean", () -> {
ManagementService service = this.managementTestRule.getManagementService();
assertThat(service.isManager()).isTrue();
NotificationListener listener = (final Notification notification, final Object handback) -> {
if (notification.getType().equals(JMXNotificationType.REGION_CREATED)) {
notifications.add(notification);
}
};
ManagementFactory.getPlatformMBeanServer().addNotificationListener(MBeanJMXAdapter.getDistributedSystemName(), listener, null, null);
});
}
use of javax.management.NotificationListener in project jdk8u_jdk by JetBrains.
the class ScanManagerTest method doTestOperation.
/**
* Test of addNotificationListener method, of class com.sun.jmx.examples.scandir.ScanManager.
*/
private void doTestOperation(ScanManagerMXBean proxy, Call op, EnumSet<ScanState> after, String testName) throws Exception {
System.out.println("doTestOperation: " + testName);
final LinkedBlockingQueue<Notification> queue = new LinkedBlockingQueue<Notification>();
NotificationListener listener = new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
try {
queue.put(notification);
} catch (Exception x) {
System.err.println("Failed to queue notif: " + x);
}
}
};
NotificationFilter filter = null;
Object handback = null;
final ScanState before;
final NotificationEmitter emitter = (NotificationEmitter) proxy;
emitter.addNotificationListener(listener, filter, handback);
before = proxy.getState();
op.call();
try {
final Notification notification = queue.poll(3000, TimeUnit.MILLISECONDS);
assertEquals(AttributeChangeNotification.ATTRIBUTE_CHANGE, notification.getType());
assertEquals(AttributeChangeNotification.class, notification.getClass());
assertEquals(ScanManager.SCAN_MANAGER_NAME, notification.getSource());
AttributeChangeNotification acn = (AttributeChangeNotification) notification;
assertEquals("State", acn.getAttributeName());
assertEquals(ScanState.class.getName(), acn.getAttributeType());
assertEquals(before, ScanState.valueOf((String) acn.getOldValue()));
assertContained(after, ScanState.valueOf((String) acn.getNewValue()));
emitter.removeNotificationListener(listener, filter, handback);
} finally {
try {
op.cancel();
} catch (Exception x) {
System.err.println("Failed to cleanup: " + x);
}
}
}
Aggregations