use of javax.management.AttributeChangeNotification in project camel by apache.
the class SimpleBean method setStringValue.
public void setStringValue(String aStringValue) {
String oldValue = getStringValue();
mStringValue = aStringValue;
AttributeChangeNotification acn = new AttributeChangeNotification(this, mSequence++, mTimestamp, "attribute changed", "stringValue", "string", oldValue, mStringValue);
sendNotification(acn);
}
use of javax.management.AttributeChangeNotification in project spring-framework by spring-projects.
the class NotificationListenerTests method testRegisterNotificationListenerWithFilter.
@SuppressWarnings("serial")
@Test
public void testRegisterNotificationListenerWithFilter() throws Exception {
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
JmxTestBean bean = new JmxTestBean();
Map<String, Object> beans = new HashMap<>();
beans.put(objectName.getCanonicalName(), bean);
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
NotificationListenerBean listenerBean = new NotificationListenerBean();
listenerBean.setNotificationListener(listener);
listenerBean.setNotificationFilter(new NotificationFilter() {
@Override
public boolean isNotificationEnabled(Notification notification) {
if (notification instanceof AttributeChangeNotification) {
AttributeChangeNotification changeNotification = (AttributeChangeNotification) notification;
return "Name".equals(changeNotification.getAttributeName());
} else {
return false;
}
}
});
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(server);
exporter.setBeans(beans);
exporter.setNotificationListeners(new NotificationListenerBean[] { listenerBean });
start(exporter);
// update the attributes
String nameAttribute = "Name";
String ageAttribute = "Age";
server.setAttribute(objectName, new Attribute(nameAttribute, "Rob Harrop"));
server.setAttribute(objectName, new Attribute(ageAttribute, new Integer(90)));
assertEquals("Listener not notified for Name", 1, listener.getCount(nameAttribute));
assertEquals("Listener incorrectly notified for Age", 0, listener.getCount(ageAttribute));
}
use of javax.management.AttributeChangeNotification in project spring-framework by spring-projects.
the class ModelMBeanNotificationPublisherTests method testSendAttributeChangeNotification.
public void testSendAttributeChangeNotification() throws Exception {
StubSpringModelMBean mbean = new StubSpringModelMBean();
Notification notification = new AttributeChangeNotification(mbean, 1872, System.currentTimeMillis(), "Shall we break for some tea?", "agree", "java.lang.Boolean", Boolean.FALSE, Boolean.TRUE);
ObjectName objectName = createObjectName();
NotificationPublisher publisher = new ModelMBeanNotificationPublisher(mbean, objectName, mbean);
publisher.sendNotification(notification);
assertNotNull(mbean.getActualNotification());
assertTrue(mbean.getActualNotification() instanceof AttributeChangeNotification);
assertSame("The exact same Notification is not being passed through from the publisher to the mbean.", notification, mbean.getActualNotification());
assertSame("The 'source' property of the Notification is not being set to the ObjectName of the associated MBean.", objectName, mbean.getActualNotification().getSource());
}
use of javax.management.AttributeChangeNotification in project aries by apache.
the class ServiceStateMBeanTest method testAttributeChangeNotifications.
@Test
public void testAttributeChangeNotifications() throws Exception {
final List<AttributeChangeNotification> attributeChanges = new ArrayList<AttributeChangeNotification>();
mbeanServer.addNotificationListener(objectName, new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
if (notification instanceof AttributeChangeNotification) {
attributeChanges.add((AttributeChangeNotification) notification);
}
}
}, null, null);
assertEquals("Precondition", 0, attributeChanges.size());
long[] idsWithout = mbean.getServiceIds();
String svc = "A String Service";
ServiceRegistration reg = bundleContext.registerService(String.class.getName(), svc, null);
long id = (Long) reg.getReference().getProperty(Constants.SERVICE_ID);
long[] idsWith = new long[idsWithout.length + 1];
System.arraycopy(idsWithout, 0, idsWith, 0, idsWithout.length);
idsWith[idsWith.length - 1] = id;
Arrays.sort(idsWith);
waitForListToReachSize(attributeChanges, 1);
AttributeChangeNotification ac = attributeChanges.get(0);
assertEquals("ServiceIds", ac.getAttributeName());
long seq1 = ac.getSequenceNumber();
assertTrue(Arrays.equals(idsWithout, (long[]) ac.getOldValue()));
assertTrue(Arrays.equals(idsWith, (long[]) ac.getNewValue()));
Dictionary<String, Object> props = new Hashtable<String, Object>();
props.put("somekey", "someval");
reg.setProperties(props);
// Setting the properties updates the service registration, however it should not cause the attribute notification
// Give the system a bit of time to send potential notifications
Thread.sleep(500);
assertEquals("Changing the service registration should not cause an attribute notification", 1, attributeChanges.size());
reg.unregister();
waitForListToReachSize(attributeChanges, 2);
AttributeChangeNotification ac2 = attributeChanges.get(1);
assertEquals("ServiceIds", ac2.getAttributeName());
assertEquals(seq1 + 1, ac2.getSequenceNumber());
assertTrue(Arrays.equals(idsWith, (long[]) ac2.getOldValue()));
assertTrue(Arrays.equals(idsWithout, (long[]) ac2.getNewValue()));
}
use of javax.management.AttributeChangeNotification in project aries by apache.
the class ServiceStateMBeanTest method testMBeanInterface.
@Test
public void testMBeanInterface() throws Exception {
//get bundles
Bundle a = getBundleByName("org.apache.aries.jmx.test.bundlea");
assertBundleStarted(a);
Bundle b = getBundleByName("org.apache.aries.jmx.test.bundleb");
assertBundleStarted(b);
// get services
ServiceReference refA = bundleContext.getServiceReference(InterfaceA.class.getName());
assertNotNull(refA);
long serviceAId = (Long) refA.getProperty(Constants.SERVICE_ID);
assertTrue(serviceAId > -1);
ServiceReference refB = bundleContext.getServiceReference(InterfaceB.class.getName());
assertNotNull(refB);
long serviceBId = (Long) refB.getProperty(Constants.SERVICE_ID);
assertTrue(serviceBId > -1);
ServiceReference[] refs = bundleContext.getServiceReferences(ManagedServiceFactory.class.getName(), "(" + Constants.SERVICE_PID + "=jmx.test.B.factory)");
assertNotNull(refs);
assertEquals(1, refs.length);
ServiceReference msf = refs[0];
// getBundleIdentifier
assertEquals(a.getBundleId(), mbean.getBundleIdentifier(serviceAId));
//getObjectClass
String[] objectClass = mbean.getObjectClass(serviceAId);
assertEquals(2, objectClass.length);
List<String> classNames = Arrays.asList(objectClass);
assertTrue(classNames.contains(InterfaceA.class.getName()));
assertTrue(classNames.contains(ManagedService.class.getName()));
// getProperties
TabularData serviceProperties = mbean.getProperties(serviceBId);
assertNotNull(serviceProperties);
assertEquals(JmxConstants.PROPERTIES_TYPE, serviceProperties.getTabularType());
assertTrue(serviceProperties.values().size() > 1);
assertEquals("org.apache.aries.jmx.test.ServiceB", PropertyData.from(serviceProperties.get(new Object[] { Constants.SERVICE_PID })).getValue());
// getUsingBundles
long[] usingBundles = mbean.getUsingBundles(serviceBId);
assertEquals(1, usingBundles.length);
assertEquals(a.getBundleId(), usingBundles[0]);
// listServices
ServiceReference[] allSvsRefs = bundleContext.getAllServiceReferences(null, null);
TabularData allServices = mbean.listServices();
assertNotNull(allServices);
assertEquals(allSvsRefs.length, allServices.values().size());
// notifications
final List<Notification> received = new ArrayList<Notification>();
final List<AttributeChangeNotification> attributeChanges = new ArrayList<AttributeChangeNotification>();
mbeanServer.addNotificationListener(objectName, new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
if (notification instanceof AttributeChangeNotification) {
attributeChanges.add((AttributeChangeNotification) notification);
} else {
received.add(notification);
}
}
}, null, null);
assertNotNull(refB);
assertNotNull(msf);
b.stop();
refB = bundleContext.getServiceReference(InterfaceB.class.getName());
refs = bundleContext.getServiceReferences(ManagedServiceFactory.class.getName(), "(" + Constants.SERVICE_PID + "=jmx.test.B.factory)");
assertNull(refs);
assertNull(refB);
b.start();
refB = bundleContext.getServiceReference(InterfaceB.class.getName());
refs = bundleContext.getServiceReferences(ManagedServiceFactory.class.getName(), "(" + Constants.SERVICE_PID + "=jmx.test.B.factory)");
assertNotNull(refB);
assertNotNull(refs);
assertEquals(1, refs.length);
waitForListToReachSize(received, 4);
assertEquals(4, received.size());
assertEquals(4, attributeChanges.size());
}
Aggregations