use of javax.management.NotificationListener in project btrace by btraceio.
the class BTraceRuntime method initMemoryListener.
private void initMemoryListener() {
initThreadPool();
memoryListener = new NotificationListener() {
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void handleNotification(Notification notif, Object handback) {
boolean entered = BTraceRuntime.enter(BTraceRuntime.this);
try {
String notifType = notif.getType();
if (notifType.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
CompositeData cd = (CompositeData) notif.getUserData();
final MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);
String name = info.getPoolName();
final LowMemoryHandler handler = lowMemoryHandlerMap.get(name);
if (handler != null) {
threadPool.submit(new Runnable() {
@Override
public void run() {
boolean entered = BTraceRuntime.enter(BTraceRuntime.this);
try {
if (handler.trackUsage) {
handler.invoke(clazz, null, info.getUsage());
} else {
handler.invoke(clazz, null, (Object[]) null);
}
} catch (Throwable th) {
} finally {
if (entered) {
BTraceRuntime.leave();
}
}
}
});
}
}
} finally {
if (entered) {
BTraceRuntime.leave();
}
}
}
};
}
use of javax.management.NotificationListener in project spring-framework by spring-projects.
the class MBeanExporterTests method testRegisterNotificationListenerForNonExistentMBean.
@Test
void testRegisterNotificationListenerForNonExistentMBean() throws Exception {
Map<String, NotificationListener> listeners = new HashMap<>();
NotificationListener dummyListener = (notification, handback) -> {
throw new UnsupportedOperationException();
};
// the MBean with the supplied object name does not exist...
listeners.put("spring:type=Test", dummyListener);
MBeanExporter exporter = new MBeanExporter();
exporter.setBeans(getBeanMap());
exporter.setServer(server);
exporter.setNotificationListenerMappings(listeners);
assertThatExceptionOfType(MBeanExportException.class).as("NotificationListener on a non-existent MBean").isThrownBy(() -> start(exporter)).satisfies(ex -> assertThat(ex.contains(InstanceNotFoundException.class)));
}
use of javax.management.NotificationListener in project aries by apache.
the class ServiceStateTest method createService.
private void createService(StateConfig stateConfig, final List<Notification> received, final List<AttributeChangeNotification> attributeChanges) throws Exception {
BundleContext context = mock(BundleContext.class);
Logger logger = mock(Logger.class);
ServiceState serviceState = new ServiceState(context, stateConfig, logger);
ServiceReference reference = mock(ServiceReference.class);
Bundle b1 = mock(Bundle.class);
when(b1.getBundleId()).thenReturn(new Long(9));
when(b1.getSymbolicName()).thenReturn("bundle");
when(b1.getLocation()).thenReturn("file:/location");
when(reference.getBundle()).thenReturn(b1);
when(reference.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(44));
when(reference.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[] { "org.apache.aries.jmx.Mock" });
when(context.getAllServiceReferences(null, null)).thenReturn(new ServiceReference[] { reference });
ServiceEvent registeredEvent = mock(ServiceEvent.class);
when(registeredEvent.getServiceReference()).thenReturn(reference);
when(registeredEvent.getType()).thenReturn(ServiceEvent.REGISTERED);
ServiceEvent modifiedEvent = mock(ServiceEvent.class);
when(modifiedEvent.getServiceReference()).thenReturn(reference);
when(modifiedEvent.getType()).thenReturn(ServiceEvent.MODIFIED);
MBeanServer server = mock(MBeanServer.class);
// setup for notification
ObjectName objectName = new ObjectName(OBJECTNAME);
serviceState.preRegister(server, objectName);
serviceState.postRegister(true);
// add NotificationListener to receive the events
serviceState.addNotificationListener(new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
if (notification instanceof AttributeChangeNotification) {
attributeChanges.add((AttributeChangeNotification) notification);
} else {
received.add(notification);
}
}
}, null, null);
// capture the ServiceListener registered with BundleContext to issue ServiceEvents
ArgumentCaptor<AllServiceListener> argument = ArgumentCaptor.forClass(AllServiceListener.class);
verify(context).addServiceListener(argument.capture());
// send events
AllServiceListener serviceListener = argument.getValue();
serviceListener.serviceChanged(registeredEvent);
serviceListener.serviceChanged(modifiedEvent);
// shutdown dispatcher via unregister callback
serviceState.postDeregister();
// check the ServiceListener is cleaned up
verify(context).removeServiceListener(serviceListener);
ExecutorService dispatcher = serviceState.getEventDispatcher();
assertTrue(dispatcher.isShutdown());
dispatcher.awaitTermination(2, TimeUnit.SECONDS);
assertTrue(dispatcher.isTerminated());
}
use of javax.management.NotificationListener in project aries by apache.
the class BundleStateMBeanTest method testAttributeChangeNotifications.
@Test
public void testAttributeChangeNotifications() throws Exception {
final List<AttributeChangeNotification> attributeChanges = new ArrayList<AttributeChangeNotification>();
AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter();
filter.disableAllAttributes();
filter.enableAttribute("BundleIds");
mbeanServer.addNotificationListener(objectName, new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
attributeChanges.add((AttributeChangeNotification) notification);
}
}, filter, null);
long[] idsWithout = mbean.getBundleIds();
assertEquals("Precondition", 0, attributeChanges.size());
Manifest mf = new Manifest();
mf.getMainAttributes().putValue("Bundle-ManifestVersion", "2");
mf.getMainAttributes().putValue("Bundle-SymbolicName", "empty-test-bundle");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JarOutputStream jos = new JarOutputStream(baos, mf);
jos.closeEntry();
jos.close();
InputStream bais = new ByteArrayInputStream(baos.toByteArray());
Bundle bundle = bundleContext.installBundle("http://somelocation", bais);
long[] idsWith = new long[idsWithout.length + 1];
System.arraycopy(idsWithout, 0, idsWith, 0, idsWithout.length);
idsWith[idsWith.length - 1] = bundle.getBundleId();
Arrays.sort(idsWith);
waitForListToReachSize(attributeChanges, 1);
assertEquals(1, attributeChanges.size());
AttributeChangeNotification ac = attributeChanges.get(0);
assertEquals("BundleIds", ac.getAttributeName());
long oldSequence = ac.getSequenceNumber();
assertTrue(Arrays.equals(idsWithout, (long[]) ac.getOldValue()));
assertTrue(Arrays.equals(idsWith, (long[]) ac.getNewValue()));
bundle.uninstall();
waitForListToReachSize(attributeChanges, 2);
AttributeChangeNotification ac2 = attributeChanges.get(1);
assertEquals("BundleIds", ac2.getAttributeName());
assertEquals(oldSequence + 1, ac2.getSequenceNumber());
assertTrue(Arrays.equals(idsWith, (long[]) ac2.getOldValue()));
assertTrue(Arrays.equals(idsWithout, (long[]) ac2.getNewValue()));
}
use of javax.management.NotificationListener in project tomcat by apache.
the class ConnectionPool method notify.
/**
* Return true if the notification was sent successfully, false otherwise.
* @param type The notification type
* @param message The message
* @return true if the notification succeeded
*/
public boolean notify(final String type, String message) {
try {
Notification n = new Notification(type, this, sequence.incrementAndGet(), System.currentTimeMillis(), "[" + type + "] " + message);
sendNotification(n);
for (NotificationListener listener : listeners) {
listener.handleNotification(n, this);
}
return true;
} catch (Exception x) {
if (log.isDebugEnabled()) {
log.debug("Notify failed. Type=" + type + "; Message=" + message, x);
}
return false;
}
}
Aggregations