Search in sources :

Example 51 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project samza by apache.

the class TestJmxAppender method testJmxAppender.

@Test
public void testJmxAppender() throws Exception {
    MBeanServerConnection mbserver = JMXConnectorFactory.connect(URL).getMBeanServerConnection();
    ObjectName objectName = new ObjectName(JmxAppender.JMX_OBJECT_DOMAIN + ":type=" + JmxAppender.JMX_OBJECT_TYPE + ",name=" + JmxAppender.JMX_OBJECT_NAME);
    String level = null;
    MockAppender mockAppender = new MockAppender();
    Logger.getRootLogger().addAppender(mockAppender);
    // Check INFO is set (from log4j.xml).
    level = (String) mbserver.getAttribute(objectName, "Level");
    assertEquals("INFO", level);
    log.info("info1");
    log.debug("debug1");
    // Set to debug.
    mbserver.setAttribute(objectName, new Attribute("Level", "debug"));
    // Check DEBUG is set.
    level = (String) mbserver.getAttribute(objectName, "Level");
    assertEquals("DEBUG", level);
    log.info("info2");
    log.debug("debug2");
    List<LoggingEvent> logLines = mockAppender.getLogLines();
    // Should not have debug1 because log level is info at first.
    Iterator<LoggingEvent> logLineIterator = logLines.iterator();
    assertEquals(3, logLines.size());
    assertEquals("info1", logLineIterator.next().getMessage());
    assertEquals("info2", logLineIterator.next().getMessage());
    assertEquals("debug2", logLineIterator.next().getMessage());
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) Attribute(javax.management.Attribute) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 52 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project wildfly by wildfly.

the class ServiceMBeanSupportTestCase method testSarWithServiceMBeanSupport.

/**
     * Tests that invocation on a service deployed within a .sar, inside a .ear without an application.xml, is successful.
     *
     * @throws Exception
     */
@Test
public void testSarWithServiceMBeanSupport() throws Exception {
    // get mbean server
    final JMXConnector connector = JMXConnectorFactory.connect(managementClient.getRemoteJMXURL(), DefaultConfiguration.credentials());
    final MBeanServerConnection mBeanServerConnection = connector.getMBeanServerConnection();
    try {
        // deploy the unmanaged sar
        deployer.deploy(ServiceMBeanSupportTestCase.UNMANAGED_SAR_DEPLOYMENT_NAME);
        // check the unmanaged mbean state
        int state = (Integer) mBeanServerConnection.getAttribute(new ObjectName("jboss:name=service-mbean-support-test"), "State");
        Assert.assertEquals("Unexpected return state from Test MBean: " + state, ServiceMBean.STARTED, state);
    } finally {
        // undeploy it
        deployer.undeploy(ServiceMBeanSupportTestCase.UNMANAGED_SAR_DEPLOYMENT_NAME);
    }
    // check the result of life-cycle methods invocation, using result mbean
    // also check that the result mbean received lifecycle notifications
    String[] expectedAttributes = new String[] { "CreateServiceInvoked", "StartServiceInvoked", "StopServiceInvoked", "DestroyServiceInvoked", "StartingNotificationReceived", "StartedNotificationReceived", "StoppingNotificationReceived", "StoppedNotificationReceived" };
    // each of these attributes should be 'true'
    for (String attribute : expectedAttributes) {
        Boolean result = (Boolean) mBeanServerConnection.getAttribute(new ObjectName("jboss:name=service-mbean-support-test-result"), attribute);
        Assert.assertTrue("Unexpected result for " + attribute + ": " + result, result);
    }
}
Also used : JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 53 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project wildfly by wildfly.

the class JNDIBindingMBeanTestCase method testMBeanStartup.

/**
     * Makes sure that the MBeans that are expected to be up and running are accessible.
     *
     * @throws Exception
     * @see https://developer.jboss.org/thread/251092
     */
@Test
public void testMBeanStartup() throws Exception {
    // get mbean server
    final JMXConnector connector = JMXConnectorFactory.connect(managementClient.getRemoteJMXURL(), DefaultConfiguration.credentials());
    try {
        final MBeanServerConnection mBeanServerConnection = connector.getMBeanServerConnection();
        // check the deployed MBeans
        for (int i = 1; i <= 9; i++) {
            final String mbeanName = "jboss:name=mbean-startup-jndi-bind-" + i;
            final Object instance = mBeanServerConnection.getObjectInstance(new ObjectName(mbeanName));
            Assert.assertNotNull("No instance returned for MBean: " + mbeanName, instance);
        }
    } finally {
        connector.close();
    }
}
Also used : JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 54 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project wildfly by wildfly.

the class SarWithinEarTestCase method testSarWithinEar.

private void testSarWithinEar(final String serviceName) throws Exception {
    final MBeanServerConnection mBeanServerConnection = this.getMBeanServerConnection();
    final ObjectName mbeanObjectName = new ObjectName(serviceName);
    final int num1 = 3;
    final int num2 = 4;
    // invoke the operation on MBean
    Integer sum = (Integer) mBeanServerConnection.invoke(mbeanObjectName, "add", new Object[] { num1, num2 }, new String[] { Integer.TYPE.getName(), Integer.TYPE.getName() });
    Assert.assertEquals("Unexpected return value from MBean: " + mbeanObjectName, num1 + num2, (int) sum);
}
Also used : MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName)

Example 55 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project wildfly by wildfly.

the class SarInjectionTestCase method testMBean.

@Test
public void testMBean() throws Exception {
    final JMXConnector connector = JMXConnectorFactory.connect(managementClient.getRemoteJMXURL());
    try {
        final MBeanServerConnection mbeanServer = connector.getMBeanServerConnection();
        final ObjectName objectName = new ObjectName("jboss:name=POJOService");
        Assert.assertTrue(2 == (Integer) mbeanServer.getAttribute(objectName, "Count"));
        Assert.assertTrue(2 == (Integer) mbeanServer.getAttribute(objectName, "InjectedCount"));
        Assert.assertTrue((Boolean) mbeanServer.getAttribute(objectName, "CreateCalled"));
        Assert.assertTrue((Boolean) mbeanServer.getAttribute(objectName, "StartCalled"));
        Assert.assertFalse((Boolean) mbeanServer.getAttribute(objectName, "StopCalled"));
        Assert.assertFalse((Boolean) mbeanServer.getAttribute(objectName, "DestroyCalled"));
    } finally {
        IoUtils.safeClose(connector);
    }
}
Also used : JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Aggregations

MBeanServerConnection (javax.management.MBeanServerConnection)125 JMXConnector (javax.management.remote.JMXConnector)84 ObjectName (javax.management.ObjectName)73 JMXServiceURL (javax.management.remote.JMXServiceURL)59 JMXConnectorServer (javax.management.remote.JMXConnectorServer)38 Test (org.junit.Test)35 IOException (java.io.IOException)31 MBeanServer (javax.management.MBeanServer)28 HashMap (java.util.HashMap)23 Attribute (javax.management.Attribute)15 NotificationListener (javax.management.NotificationListener)13 MalformedURLException (java.net.MalformedURLException)12 ArrayList (java.util.ArrayList)12 Notification (javax.management.Notification)12 MalformedObjectNameException (javax.management.MalformedObjectNameException)11 Map (java.util.Map)10 List (java.util.List)8 RemoteException (java.rmi.RemoteException)7 LocateRegistry (java.rmi.registry.LocateRegistry)7 Registry (java.rmi.registry.Registry)7