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));
}
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));
}
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));
}
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));
}
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);
}
Aggregations