Search in sources :

Example 1 with AttributeChangeNotification

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);
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification)

Example 2 with AttributeChangeNotification

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));
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) HashMap(java.util.HashMap) Attribute(javax.management.Attribute) Notification(javax.management.Notification) AttributeChangeNotification(javax.management.AttributeChangeNotification) ObjectName(javax.management.ObjectName) JmxTestBean(org.springframework.jmx.JmxTestBean) NotificationFilter(javax.management.NotificationFilter) Test(org.junit.Test)

Example 3 with AttributeChangeNotification

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());
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName)

Example 4 with AttributeChangeNotification

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()));
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) NotificationListener(javax.management.NotificationListener) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Example 5 with AttributeChangeNotification

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());
}
Also used : InterfaceA(org.apache.aries.jmx.test.bundlea.api.InterfaceA) InterfaceB(org.apache.aries.jmx.test.bundleb.api.InterfaceB) AttributeChangeNotification(javax.management.AttributeChangeNotification) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) ServiceReference(org.osgi.framework.ServiceReference) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory) TabularData(javax.management.openmbean.TabularData) NotificationListener(javax.management.NotificationListener) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Aggregations

AttributeChangeNotification (javax.management.AttributeChangeNotification)28 Notification (javax.management.Notification)14 NotificationListener (javax.management.NotificationListener)8 Test (org.junit.Test)6 ObjectName (javax.management.ObjectName)5 Bundle (org.osgi.framework.Bundle)4 ScanState (com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 NotificationFilter (javax.management.NotificationFilter)3 LinkedList (java.util.LinkedList)2 ExecutorService (java.util.concurrent.ExecutorService)2 MBeanServer (javax.management.MBeanServer)2 NotificationEmitter (javax.management.NotificationEmitter)2 RuntimeOperationsException (javax.management.RuntimeOperationsException)2 CompositeData (javax.management.openmbean.CompositeData)2 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)2 Call (com.sun.jmx.examples.scandir.ScanManagerTest.Call)1 DirectoryScannerConfig (com.sun.jmx.examples.scandir.config.DirectoryScannerConfig)1