Search in sources :

Example 6 with IJmxTestBean

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

the class MBeanClientInterceptorTests method testTestLazyConnectionToRemote.

@Test
public void testTestLazyConnectionToRemote() throws Exception {
    assumeTrue(runTests);
    Assume.group(TestGroup.JMXMP);
    final int port = SocketUtils.findAvailableTcpPort();
    JMXServiceURL url = new JMXServiceURL("service:jmx:jmxmp://localhost:" + port);
    JMXConnectorServer connector = JMXConnectorServerFactory.newJMXConnectorServer(url, null, getServer());
    MBeanProxyFactoryBean factory = new MBeanProxyFactoryBean();
    factory.setServiceUrl(url.toString());
    factory.setProxyInterface(IJmxTestBean.class);
    factory.setObjectName(OBJECT_NAME);
    factory.setConnectOnStartup(false);
    factory.setRefreshOnConnectFailure(true);
    // should skip connection to the server
    factory.afterPropertiesSet();
    IJmxTestBean bean = (IJmxTestBean) factory.getObject();
    // now start the connector
    try {
        connector.start();
    } catch (BindException ex) {
        System.out.println("Skipping remainder of JMX LazyConnectionToRemote test because binding to local port [" + port + "] failed: " + ex.getMessage());
        return;
    }
    // should now be able to access data via the lazy proxy
    try {
        assertEquals("Rob Harrop", bean.getName());
        assertEquals(100, bean.getAge());
    } finally {
        connector.stop();
    }
    try {
        bean.getName();
    } catch (JmxException ex) {
    // expected
    }
    connector = JMXConnectorServerFactory.newJMXConnectorServer(url, null, getServer());
    connector.start();
    // should now be able to access data via the lazy proxy
    try {
        assertEquals("Rob Harrop", bean.getName());
        assertEquals(100, bean.getAge());
    } finally {
        connector.stop();
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JmxException(org.springframework.jmx.JmxException) BindException(java.net.BindException) IJmxTestBean(org.springframework.jmx.IJmxTestBean) JMXConnectorServer(javax.management.remote.JMXConnectorServer) Test(org.junit.Test)

Example 7 with IJmxTestBean

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

the class MBeanExporterTests method testExportJdkProxy.

@Test
public 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");
    assertEquals("Rob Harrop", nameValue);
}
Also used : NopInterceptor(org.springframework.tests.aop.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.Test)

Example 8 with IJmxTestBean

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

the class PropertyPlaceholderConfigurerTests method testPropertiesReplaced.

@Test
public void testPropertiesReplaced() {
    IJmxTestBean bean = (IJmxTestBean) getContext().getBean("testBean");
    assertEquals("Name is incorrect", "Rob Harrop", bean.getName());
    assertEquals("Age is incorrect", 100, bean.getAge());
}
Also used : IJmxTestBean(org.springframework.jmx.IJmxTestBean) Test(org.junit.Test)

Example 9 with IJmxTestBean

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

the class MBeanClientInterceptorTests method testSetAttributeValueWithRuntimeException.

@Test(expected = IllegalArgumentException.class)
public void testSetAttributeValueWithRuntimeException() throws Exception {
    assumeTrue(runTests);
    IJmxTestBean proxy = getProxy();
    proxy.setName("Juergen");
}
Also used : IJmxTestBean(org.springframework.jmx.IJmxTestBean) Test(org.junit.Test)

Example 10 with IJmxTestBean

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

the class MBeanClientInterceptorTests method testDifferentProxiesSameClass.

@Test
public void testDifferentProxiesSameClass() throws Exception {
    assumeTrue(runTests);
    IJmxTestBean proxy1 = getProxy();
    IJmxTestBean proxy2 = getProxy();
    assertNotSame("The proxies should NOT be the same", proxy1, proxy2);
    assertSame("The proxy classes should be the same", proxy1.getClass(), proxy2.getClass());
}
Also used : IJmxTestBean(org.springframework.jmx.IJmxTestBean) Test(org.junit.Test)

Aggregations

IJmxTestBean (org.springframework.jmx.IJmxTestBean)19 Test (org.junit.Test)18 ModelMBeanInfo (javax.management.modelmbean.ModelMBeanInfo)4 MBeanInfo (javax.management.MBeanInfo)3 HashMap (java.util.HashMap)2 ObjectName (javax.management.ObjectName)2 ProxyFactory (org.springframework.aop.framework.ProxyFactory)2 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)2 BindException (java.net.BindException)1 Attribute (javax.management.Attribute)1 JMXConnectorServer (javax.management.remote.JMXConnectorServer)1 JMXServiceURL (javax.management.remote.JMXServiceURL)1 JmxException (org.springframework.jmx.JmxException)1 JmxTestBean (org.springframework.jmx.JmxTestBean)1 MBeanExporter (org.springframework.jmx.export.MBeanExporter)1