Search in sources :

Example 1 with JmxTestBean

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

the class NotificationListenerTests method testRegisterNotificationListenerWithHandback.

@Test
public void testRegisterNotificationListenerWithHandback() throws Exception {
    String objectName = "spring:name=Test";
    JmxTestBean bean = new JmxTestBean();
    Map<String, Object> beans = new HashMap<>();
    beans.put(objectName, bean);
    CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
    Object handback = new Object();
    NotificationListenerBean listenerBean = new NotificationListenerBean();
    listenerBean.setNotificationListener(listener);
    listenerBean.setMappedObjectName("spring:name=Test");
    listenerBean.setHandback(handback);
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.setNotificationListeners(new NotificationListenerBean[] { listenerBean });
    start(exporter);
    // update the attribute
    String attributeName = "Name";
    server.setAttribute(ObjectNameManager.getInstance("spring:name=Test"), new Attribute(attributeName, "Rob Harrop"));
    assertEquals("Listener not notified", 1, listener.getCount(attributeName));
    assertEquals("Handback object not transmitted correctly", handback, listener.getLastHandback(attributeName));
}
Also used : HashMap(java.util.HashMap) Attribute(javax.management.Attribute) JmxTestBean(org.springframework.jmx.JmxTestBean) Test(org.junit.Test)

Example 2 with JmxTestBean

use of org.springframework.jmx.JmxTestBean 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 JmxTestBean

use of org.springframework.jmx.JmxTestBean 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 4 with JmxTestBean

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

the class AbstractAutodetectTests method autodetect.

@Test
public void autodetect() throws Exception {
    JmxTestBean bean = new JmxTestBean();
    AutodetectCapableMBeanInfoAssembler assembler = getAssembler();
    assertTrue("The bean should be included", assembler.includeBean(bean.getClass(), "testBean"));
}
Also used : JmxTestBean(org.springframework.jmx.JmxTestBean) Test(org.junit.Test)

Example 5 with JmxTestBean

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

the class MBeanExporterOperationsTests method testRegisterManagedResourceWithUserSuppliedObjectName.

@Test
void testRegisterManagedResourceWithUserSuppliedObjectName() throws Exception {
    ObjectName objectName = ObjectNameManager.getInstance("spring:name=Foo");
    JmxTestBean bean = new JmxTestBean();
    bean.setName("Rob Harrop");
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(getServer());
    exporter.registerManagedResource(bean, objectName);
    String name = (String) getServer().getAttribute(objectName, "Name");
    assertThat(bean.getName()).as("Incorrect name on MBean").isEqualTo(name);
}
Also used : JmxTestBean(org.springframework.jmx.JmxTestBean) ObjectName(javax.management.ObjectName) Test(org.junit.jupiter.api.Test)

Aggregations

JmxTestBean (org.springframework.jmx.JmxTestBean)18 ObjectName (javax.management.ObjectName)12 HashMap (java.util.HashMap)11 Test (org.junit.Test)9 Attribute (javax.management.Attribute)8 Test (org.junit.jupiter.api.Test)7 IJmxTestBean (org.springframework.jmx.IJmxTestBean)6 Map (java.util.Map)2 StandardMBean (javax.management.StandardMBean)2 NotificationListenerRegistrar (org.springframework.jmx.access.NotificationListenerRegistrar)2 AttributeChangeNotification (javax.management.AttributeChangeNotification)1 Notification (javax.management.Notification)1 NotificationFilter (javax.management.NotificationFilter)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)1 MBeanExporter (org.springframework.jmx.export.MBeanExporter)1