Search in sources :

Example 11 with JmxTestBean

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

the class NotificationListenerTests method testRegisterNotificationListenerWithWildcard.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testRegisterNotificationListenerWithWildcard() 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();
    Map notificationListeners = new HashMap();
    notificationListeners.put("*", listener);
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.setNotificationListenerMappings(notificationListeners);
    start(exporter);
    // update the attribute
    String attributeName = "Name";
    server.setAttribute(objectName, new Attribute(attributeName, "Rob Harrop"));
    assertEquals("Listener not notified", 1, listener.getCount(attributeName));
}
Also used : HashMap(java.util.HashMap) Attribute(javax.management.Attribute) JmxTestBean(org.springframework.jmx.JmxTestBean) HashMap(java.util.HashMap) Map(java.util.Map) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 12 with JmxTestBean

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

Example 13 with JmxTestBean

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

the class NotificationListenerTests method testRegisterNotificationListenerForAllMBeans.

@Test
public void testRegisterNotificationListenerForAllMBeans() 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);
    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(objectName, new Attribute(attributeName, "Rob Harrop"));
    assertEquals("Listener not notified", 1, listener.getCount(attributeName));
}
Also used : HashMap(java.util.HashMap) Attribute(javax.management.Attribute) JmxTestBean(org.springframework.jmx.JmxTestBean) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 14 with JmxTestBean

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

the class IdentityNamingStrategyTests method naming.

@Test
public void naming() throws MalformedObjectNameException {
    JmxTestBean bean = new JmxTestBean();
    IdentityNamingStrategy strategy = new IdentityNamingStrategy();
    ObjectName objectName = strategy.getObjectName(bean, "null");
    assertThat(objectName.getDomain()).as("Domain is incorrect").isEqualTo(bean.getClass().getPackage().getName());
    assertThat(objectName.getKeyProperty(IdentityNamingStrategy.TYPE_KEY)).as("Type property is incorrect").isEqualTo(ClassUtils.getShortName(bean.getClass()));
    assertThat(objectName.getKeyProperty(IdentityNamingStrategy.HASH_CODE_KEY)).as("HashCode property is incorrect").isEqualTo(ObjectUtils.getIdentityHexString(bean));
}
Also used : JmxTestBean(org.springframework.jmx.JmxTestBean) ObjectName(javax.management.ObjectName) Test(org.junit.jupiter.api.Test)

Example 15 with JmxTestBean

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

the class MBeanExporterOperationsTests method testRegisterManagedResourceWithGeneratedObjectNameWithoutUniqueness.

@Test
void testRegisterManagedResourceWithGeneratedObjectNameWithoutUniqueness() throws Exception {
    final ObjectName objectNameTemplate = ObjectNameManager.getInstance("spring:type=Test");
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(getServer());
    exporter.setEnsureUniqueRuntimeObjectNames(false);
    exporter.setNamingStrategy((managedBean, beanKey) -> objectNameTemplate);
    JmxTestBean bean1 = new JmxTestBean();
    JmxTestBean bean2 = new JmxTestBean();
    ObjectName reg1 = exporter.registerManagedResource(bean1);
    assertIsRegistered("Bean 1 not registered with MBeanServer", reg1);
    assertThatExceptionOfType(MBeanExportException.class).isThrownBy(() -> exporter.registerManagedResource(bean2)).withCauseExactlyInstanceOf(InstanceAlreadyExistsException.class);
}
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