Example usage for javax.management.remote JMXConnector connect

List of usage examples for javax.management.remote JMXConnector connect

Introduction

In this page you can find the example usage for javax.management.remote JMXConnector connect.

Prototype

public void connect(Map<String, ?> env) throws IOException;

Source Link

Document

Establishes the connection to the connector server.

If connect has already been called successfully on this object, calling it again has no effect.

Usage

From source file:net.sf.ehcache.management.ManagementServiceTest.java

/**
 * Creates an RMI JMXConnectorServer, connects to it and demonstrates what attributes are traversable.
 * The answer is not all.//from   w  w w  .j  a va 2  s .co  m
 *
 * Note that this test creates a Registry which will keep running until the JVM Exists. There
 * is no way to stop it but it should do no harm.
 *
 *
 */
public void testJMXConnectorServer() throws Exception {

    ManagementService.registerMBeans(manager, mBeanServer, true, true, true, true);

    LocateRegistry.createRegistry(55000);
    String serverUrl = "service:jmx:rmi:///jndi/rmi://localhost:55000/server";
    JMXServiceURL url = new JMXServiceURL(serverUrl);
    JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mBeanServer);
    cs.start();
    JMXConnector connector = cs.toJMXConnector(null);
    connector.connect(null);
    MBeanServerConnection connection = connector.getMBeanServerConnection();
    assertEquals(OBJECTS_IN_TEST_EHCACHE,
            connection.queryNames(new ObjectName("net.sf.ehcache:*"), null).size());

    Ehcache ehcache = manager.getCache("sampleCache1");

    ehcache.put(new Element("key1", "value1"));
    ehcache.put(new Element("key2", "value1"));
    assertNotNull(ehcache.get("key1"));
    assertNotNull(ehcache.get("key2"));

    //Test CacheManager
    //not all attributes are accessible due to serializability constraints
    //traverseMBeanAttributes(connection, "CacheManager");

    //Test Cache
    //not all attributes are accessible due to serializability constraints
    //traverseMBeanAttributes(connection, "Cache");

    //Test CacheStatistics
    traverseMBeanAttributes(connection, "CacheStatistics");

    //Test CacheConfiguration
    traverseMBeanAttributes(connection, "CacheConfiguration");

    cs.stop();
}

From source file:org.opennms.jmxconfiggenerator.jmxconfig.JmxDatacollectionConfiggenerator.java

/**
 * This method gets the JmxConnector to connect with the given jmxServiceURL.
 *
 * @param username may be null//from w ww  .j av a 2  s  .  c o  m
 * @param password may be null
 * @param jmxServiceURL should not be null!
 * @return a jmxConnector
 * @throws IOException if the connection to the given jmxServiceURL fails (e.g. authentication failure or not reachable)
 */
private JMXConnector getJmxConnector(String username, String password, JMXServiceURL jmxServiceURL)
        throws IOException {
    JMXConnector jmxConnector;
    if (username != null && password != null) {
        jmxConnector = JMXConnectorFactory.newJMXConnector(jmxServiceURL, null);
        HashMap<String, String[]> env = new HashMap<String, String[]>();
        String[] credentials = new String[] { username, password };
        env.put("jmx.remote.credentials", credentials);
        jmxConnector.connect(env);
    } else {
        jmxConnector = JMXConnectorFactory.connect(jmxServiceURL);
        jmxConnector.connect();
    }
    return jmxConnector;
}