Search in sources :

Example 1 with NotificationListenerRegistrar

use of org.springframework.jmx.access.NotificationListenerRegistrar in project spring-framework by spring-projects.

the class NotificationListenerTests method testNotificationListenerRegistrarWithMultipleNames.

@Test
public void testNotificationListenerRegistrarWithMultipleNames() throws Exception {
    ObjectName objectName = ObjectName.getInstance("spring:name=Test");
    ObjectName objectName2 = ObjectName.getInstance("spring:name=Test2");
    JmxTestBean bean = new JmxTestBean();
    JmxTestBean bean2 = new JmxTestBean();
    Map<String, Object> beans = new HashMap<>();
    beans.put(objectName.getCanonicalName(), bean);
    beans.put(objectName2.getCanonicalName(), bean2);
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    start(exporter);
    CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
    NotificationListenerRegistrar registrar = new NotificationListenerRegistrar();
    registrar.setServer(server);
    registrar.setNotificationListener(listener);
    //registrar.setMappedObjectNames(new Object[] {objectName, objectName2});
    registrar.setMappedObjectNames(new String[] { "spring:name=Test", "spring:name=Test2" });
    registrar.afterPropertiesSet();
    // update the attribute
    String attributeName = "Name";
    server.setAttribute(objectName, new Attribute(attributeName, "Rob Harrop"));
    assertEquals("Listener not notified", 1, listener.getCount(attributeName));
    registrar.destroy();
    // try to update the attribute again
    server.setAttribute(objectName, new Attribute(attributeName, "Rob Harrop"));
    assertEquals("Listener notified after destruction", 1, listener.getCount(attributeName));
}
Also used : HashMap(java.util.HashMap) Attribute(javax.management.Attribute) NotificationListenerRegistrar(org.springframework.jmx.access.NotificationListenerRegistrar) JmxTestBean(org.springframework.jmx.JmxTestBean) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 2 with NotificationListenerRegistrar

use of org.springframework.jmx.access.NotificationListenerRegistrar in project spring-framework by spring-projects.

the class NotificationListenerTests method testNotificationListenerRegistrar.

@Test
public void testNotificationListenerRegistrar() throws Exception {
    ObjectName objectName = ObjectName.getInstance("spring:name=Test");
    JmxTestBean bean = new JmxTestBean();
    Map<String, Object> beans = new HashMap<>();
    beans.put(objectName.getCanonicalName(), bean);
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    start(exporter);
    CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
    NotificationListenerRegistrar registrar = new NotificationListenerRegistrar();
    registrar.setServer(server);
    registrar.setNotificationListener(listener);
    registrar.setMappedObjectName(objectName);
    registrar.afterPropertiesSet();
    // update the attribute
    String attributeName = "Name";
    server.setAttribute(objectName, new Attribute(attributeName, "Rob Harrop"));
    assertEquals("Listener not notified", 1, listener.getCount(attributeName));
    registrar.destroy();
    // try to update the attribute again
    server.setAttribute(objectName, new Attribute(attributeName, "Rob Harrop"));
    assertEquals("Listener notified after destruction", 1, listener.getCount(attributeName));
}
Also used : HashMap(java.util.HashMap) Attribute(javax.management.Attribute) NotificationListenerRegistrar(org.springframework.jmx.access.NotificationListenerRegistrar) JmxTestBean(org.springframework.jmx.JmxTestBean) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)2 Attribute (javax.management.Attribute)2 ObjectName (javax.management.ObjectName)2 Test (org.junit.Test)2 JmxTestBean (org.springframework.jmx.JmxTestBean)2 NotificationListenerRegistrar (org.springframework.jmx.access.NotificationListenerRegistrar)2