Search in sources :

Example 71 with JMXConnector

use of javax.management.remote.JMXConnector in project opennms by OpenNMS.

the class DefaultJmxConnector method createConnection.

public JmxServerConnectionWrapper createConnection(JmxConnectionConfig config) throws JmxServerConnectionException {
    try {
        // If we're trying to create a connection to a localhost address...
        if (config.isLocalConnection()) {
            // this JVM's MBeanServer directly.
            return new PlatformMBeanServerConnector().createConnection();
        }
        // Create URL
        final String urlString = config.getUrl();
        final JMXServiceURL url = new JMXServiceURL(urlString);
        LOG.debug("JMX: {} - {}", config.getFactory(), url);
        // Apply password strategy
        final Map<String, Object> env = new HashMap<>();
        config.getPasswordStategy().apply(env, config);
        // Create connection and connect
        final JMXConnector connector = JMXConnectorFactory.newJMXConnector(url, env);
        try {
            connector.connect(env);
        } catch (SecurityException x) {
            throw new JmxServerConnectionException("Security exception: bad credentials", x);
        }
        // Wrap Connection
        MBeanServerConnection connection = connector.getMBeanServerConnection();
        JmxServerConnectionWrapper connectionWrapper = new DefaultConnectionWrapper(connector, connection);
        return connectionWrapper;
    } catch (MalformedURLException e) {
        throw new JmxServerConnectionException(e);
    } catch (IOException e) {
        throw new JmxServerConnectionException(e);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) IOException(java.io.IOException) JmxServerConnectionException(org.opennms.netmgt.jmx.connection.JmxServerConnectionException) JMXConnector(javax.management.remote.JMXConnector) JmxServerConnectionWrapper(org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 72 with JMXConnector

use of javax.management.remote.JMXConnector in project opennms by OpenNMS.

the class JmxRemoteAdminIT method canConnect.

@Test
public void canConnect() throws Exception {
    final InetSocketAddress addr = m_testEnvironment.getServiceAddress(ContainerAlias.OPENNMS, 18980);
    final String hostString = "localhost".equals(addr.getHostString()) ? "127.0.0.1" : addr.getHostString();
    final int port = addr.getPort();
    final RMISocketFactory socketFactory = new JMXTestClientSocketFactory();
    System.setProperty("sun.rmi.transport.tcp.responseTimeout", "5000");
    final Callable<Integer> getRmiConnection = new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            LOG.debug("getRmiConnection({}:{})", hostString, port);
            try {
                final Registry registry = LocateRegistry.getRegistry(hostString, port, socketFactory);
                final String[] bound = registry.list();
                LOG.debug("bound={}", Arrays.asList(bound));
                if (bound.length > 0) {
                    return bound.length;
                }
            } catch (final Exception e) {
            }
            return null;
        }
    };
    await().atMost(5, MINUTES).pollInterval(10, SECONDS).until(getRmiConnection, greaterThanOrEqualTo(1));
    final Callable<Integer> getJmxConnection = new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            LOG.debug("getJmxConnection({}:{})", hostString, port);
            try {
                RMISocketFactory.setSocketFactory(socketFactory);
                final Map<String, Object> env = new HashMap<>();
                final String[] credentials = { "admin", "admin" };
                env.put(JMXConnector.CREDENTIALS, credentials);
                env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, socketFactory);
                final String urlString = String.format("service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi", hostString, port);
                LOG.debug("getJmxConnection(): connecting to {}", urlString);
                final JMXServiceURL url = new JMXServiceURL(urlString);
                final JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
                final MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
                LOG.debug("mbeanCount={}", mbsc.getMBeanCount());
                if (mbsc.getMBeanCount() > 0) {
                    return mbsc.getMBeanCount();
                }
            } catch (final Exception e) {
            }
            return null;
        }
    };
    await().atMost(5, MINUTES).pollInterval(10, SECONDS).until(getJmxConnection, greaterThanOrEqualTo(1));
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) RMISocketFactory(java.rmi.server.RMISocketFactory) Callable(java.util.concurrent.Callable) IOException(java.io.IOException) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) Test(org.junit.Test)

Example 73 with JMXConnector

use of javax.management.remote.JMXConnector in project wildfly by wildfly.

the class SarResourceInjectionTestCase method testMBean.

@Test
public void testMBean() throws Exception {
    final JMXConnector connector = JMXConnectorFactory.connect(managementClient.getRemoteJMXURL(), DefaultConfiguration.credentials());
    try {
        final MBeanServerConnection mbeanServer = connector.getMBeanServerConnection();
        final ObjectName objectName = new ObjectName("jboss:name=X");
        Assert.assertTrue((Boolean) mbeanServer.invoke(objectName, "resourcesInjected", null, null));
    } finally {
        IoUtils.safeClose(connector);
    }
}
Also used : JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 74 with JMXConnector

use of javax.management.remote.JMXConnector in project wildfly by wildfly.

the class SarValueFactoryInjectionTestCase 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=TargetBean");
        Assert.assertEquals("Injection using value-factory without method arguments failed", 2, ((Integer) mbeanServer.getAttribute(objectName, "SourceCount")).intValue());
        Assert.assertEquals("Injection using value-factory with method argument failed", 4, ((Integer) mbeanServer.getAttribute(objectName, "CountWithArgument")).intValue());
    } finally {
        IoUtils.safeClose(connector);
    }
}
Also used : JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 75 with JMXConnector

use of javax.management.remote.JMXConnector in project wildfly by wildfly.

the class SarTestCase method testMBean.

@Test
public void testMBean() throws Exception {
    final JMXConnector connector = JMXConnectorFactory.connect(managementClient.getRemoteJMXURL());
    try {
        MBeanServerConnection mbeanServer = connector.getMBeanServerConnection();
        ObjectName objectName = new ObjectName("jboss:name=test,type=config");
        mbeanServer.getAttribute(objectName, "IntervalSeconds");
        mbeanServer.setAttribute(objectName, new Attribute("IntervalSeconds", 2));
    } finally {
        IoUtils.safeClose(connector);
    }
}
Also used : Attribute(javax.management.Attribute) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Aggregations

JMXConnector (javax.management.remote.JMXConnector)118 MBeanServerConnection (javax.management.MBeanServerConnection)85 JMXServiceURL (javax.management.remote.JMXServiceURL)78 ObjectName (javax.management.ObjectName)54 JMXConnectorServer (javax.management.remote.JMXConnectorServer)47 MBeanServer (javax.management.MBeanServer)37 IOException (java.io.IOException)35 HashMap (java.util.HashMap)27 Test (org.junit.Test)22 Notification (javax.management.Notification)14 NotificationListener (javax.management.NotificationListener)14 Attribute (javax.management.Attribute)13 MalformedURLException (java.net.MalformedURLException)12 RemoteException (java.rmi.RemoteException)11 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 MalformedObjectNameException (javax.management.MalformedObjectNameException)9 LocateRegistry (java.rmi.registry.LocateRegistry)8 Registry (java.rmi.registry.Registry)8 Properties (java.util.Properties)7