Search in sources :

Example 6 with JmxTestBean

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

the class MBeanExporterTests method testWithExposeClassLoader.

@Test
void testWithExposeClassLoader() throws Exception {
    String name = "Rob Harrop";
    String otherName = "Juergen Hoeller";
    JmxTestBean bean = new JmxTestBean();
    bean.setName(name);
    ObjectName objectName = ObjectNameManager.getInstance("spring:type=Test");
    Map<String, Object> beans = new HashMap<>();
    beans.put(objectName.toString(), bean);
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(getServer());
    exporter.setBeans(beans);
    exporter.setExposeManagedResourceClassLoader(true);
    start(exporter);
    assertIsRegistered("Bean instance not registered", objectName);
    Object result = server.invoke(objectName, "add", new Object[] { 2, 3 }, new String[] { int.class.getName(), int.class.getName() });
    assertThat(Integer.valueOf(5)).as("Incorrect result return from add").isEqualTo(result);
    assertThat(server.getAttribute(objectName, "Name")).as("Incorrect attribute value").isEqualTo(name);
    server.setAttribute(objectName, new Attribute("Name", otherName));
    assertThat(bean.getName()).as("Incorrect updated name.").isEqualTo(otherName);
}
Also used : HashMap(java.util.HashMap) Attribute(javax.management.Attribute) JmxTestBean(org.springframework.jmx.JmxTestBean) IJmxTestBean(org.springframework.jmx.IJmxTestBean) ObjectName(javax.management.ObjectName) Test(org.junit.jupiter.api.Test)

Example 7 with JmxTestBean

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

the class MBeanExporterTests method testExportJdkProxy.

@Test
void testExportJdkProxy() throws Exception {
    JmxTestBean bean = new JmxTestBean();
    bean.setName("Rob Harrop");
    ProxyFactory factory = new ProxyFactory();
    factory.setTarget(bean);
    factory.addAdvice(new NopInterceptor());
    factory.setInterfaces(IJmxTestBean.class);
    IJmxTestBean proxy = (IJmxTestBean) factory.getProxy();
    String name = "bean:mmm=whatever";
    Map<String, Object> beans = new HashMap<>();
    beans.put(name, proxy);
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.registerBeans();
    ObjectName oname = ObjectName.getInstance(name);
    Object nameValue = server.getAttribute(oname, "Name");
    assertThat(nameValue).isEqualTo("Rob Harrop");
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) ProxyFactory(org.springframework.aop.framework.ProxyFactory) HashMap(java.util.HashMap) IJmxTestBean(org.springframework.jmx.IJmxTestBean) JmxTestBean(org.springframework.jmx.JmxTestBean) IJmxTestBean(org.springframework.jmx.IJmxTestBean) ObjectName(javax.management.ObjectName) Test(org.junit.jupiter.api.Test)

Example 8 with JmxTestBean

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

the class JmxUtilsTests method isMBeanWithStandardMBeanWrapper.

@Test
void isMBeanWithStandardMBeanWrapper() throws NotCompliantMBeanException {
    StandardMBean mbean = new StandardMBean(new JmxTestBean(), IJmxTestBean.class);
    assertThat(JmxUtils.isMBean(mbean.getClass())).as("Standard MBean not detected correctly").isTrue();
}
Also used : StandardMBean(javax.management.StandardMBean) JmxTestBean(org.springframework.jmx.JmxTestBean) IJmxTestBean(org.springframework.jmx.IJmxTestBean) Test(org.junit.jupiter.api.Test)

Example 9 with JmxTestBean

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

the class NotificationListenerTests method testRegisterNotificationListenerForMBean.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testRegisterNotificationListenerForMBean() 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(objectName, 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 10 with JmxTestBean

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

the class JmxUtilsTests method testIsMBeanWithStandardMBeanWrapper.

@Test
public void testIsMBeanWithStandardMBeanWrapper() throws Exception {
    StandardMBean mbean = new StandardMBean(new JmxTestBean(), IJmxTestBean.class);
    assertTrue("Standard MBean not detected correctly", JmxUtils.isMBean(mbean.getClass()));
}
Also used : StandardMBean(javax.management.StandardMBean) JmxTestBean(org.springframework.jmx.JmxTestBean) IJmxTestBean(org.springframework.jmx.IJmxTestBean) Test(org.junit.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