Example usage for javax.management.remote JMXConnectorFactory connect

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

Introduction

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

Prototype

public static JMXConnector connect(JMXServiceURL serviceURL) throws IOException 

Source Link

Document

Creates a connection to the connector server at the given address.

This method is equivalent to #connect(JMXServiceURL,Map) connect(serviceURL, null) .

Usage

From source file:com.flipkart.poseidon.jmx.PoseidonJMXInvoker.java

public static void main(String[] args) {
    if (args.length < 3) {
        System.err.println(PoseidonJMXInvoker.class.getSimpleName() + " <host> <port> <operation>");
        System.exit(-1);//  ww w . j a v a  2s. com
    }

    final String CONNECT_STRING = args[0] + ":" + args[1];
    final String OPERATION = args[2];
    try {
        System.out.println("Running " + OPERATION + " over JMX on " + CONNECT_STRING);

        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + CONNECT_STRING + "/jmxrmi");
        MBeanServerConnection connection = JMXConnectorFactory.connect(url).getMBeanServerConnection();
        connection.invoke(ObjectName.getInstance(MBEAN_NAME), OPERATION, null, null);
    } catch (Exception e) {
        if (!(ExceptionUtils.getRootCause(e) instanceof EOFException && "destroy".equals(OPERATION))) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
    System.out.println(OPERATION + " successful over JMX on " + CONNECT_STRING);
}

From source file:test.integ.be.fedict.hsm.MonitoringTest.java

@Test
public void testJMXConnection() throws Exception {
    JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:remoting-jmx://localhost:9999");
    JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxServiceURL);
    MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();
    MemoryMXBean memoryMXBean = ManagementFactory.newPlatformMXBeanProxy(connection,
            ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);
    MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
    LOG.debug("used heap memory: " + heapMemoryUsage.getUsed());
}

From source file:io.galeb.router.tests.client.JmxClientService.java

@PostConstruct
public void start() {
    try {//from  ww  w .j a  v  a2 s  .  c  o m
        String jmxUrl = ConnectorAddressLink.importFrom(Info.getPid());
        if (jmxUrl != null) {
            final JMXServiceURL url = new JMXServiceURL(jmxUrl);
            final JMXConnector jmxConn = JMXConnectorFactory.connect(url);
            client = jmxConn.getMBeanServerConnection();
            enabled.set(true);
        }
    } catch (Exception e) {
        if (logger.isDebugEnabled()) {
            logger.debug(ExceptionUtils.getStackTrace(e));
        }
    }
}

From source file:org.apache.hadoop.hbase.TestJMXListener.java

@Test
public void testStart() throws Exception {
    JMXConnector connector = JMXConnectorFactory
            .connect(JMXListener.buildJMXServiceURL(connectorPort, connectorPort));

    MBeanServerConnection mb = connector.getMBeanServerConnection();
    String domain = mb.getDefaultDomain();
    Assert.assertTrue("default domain is not correct", !domain.isEmpty());
    connector.close();//w ww . j a v  a2  s  .  c o  m

}

From source file:org.ff4j.jmx.FF4JMBeanTest.java

private void openJmxConnection() throws Exception {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

    // Make a connector server...
    JMXServiceURL jmxUrl = new JMXServiceURL("service:jmx:rmi://");
    jmxConnectionServer = JMXConnectorServerFactory.newJMXConnectorServer(jmxUrl, null, mbs);
    jmxConnectionServer.start();//from  w  w w .j a va 2 s  .  com
    JMXServiceURL jmxAddress = jmxConnectionServer.getAddress();

    // Now make a connector client using the server's address
    jmxConnectionFactory = JMXConnectorFactory.connect(jmxAddress);
    mbServConn = jmxConnectionFactory.getMBeanServerConnection();
}

From source file:org.apache.hadoop.hbase.TestJMXConnectorServer.java

/**
 * This tests to validate the HMaster's ConnectorServer after unauthorised stopMaster call.
 *//*from   ww  w  . java 2s  . co  m*/
@Test(timeout = 180000)
public void testHMConnectorServerWhenStopMaster() throws Exception {
    conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
            JMXListener.class.getName() + "," + MyAccessController.class.getName());
    conf.setInt("master.rmi.registry.port", rmiRegistryPort);
    UTIL.startMiniCluster();
    admin = UTIL.getConnection().getAdmin();

    // try to stop master
    boolean accessDenied = false;
    try {
        hasAccess = false;
        LOG.info("Stopping HMaster...");
        admin.stopMaster();
    } catch (AccessDeniedException e) {
        LOG.info("Exception occured while stopping HMaster. ", e);
        accessDenied = true;
    }
    Assert.assertTrue(accessDenied);

    // Check whether HMaster JMX Connector server can be connected
    JMXConnector connector = null;
    try {
        connector = JMXConnectorFactory
                .connect(JMXListener.buildJMXServiceURL(rmiRegistryPort, rmiRegistryPort));
    } catch (IOException e) {
        if (e.getCause() instanceof ServiceUnavailableException) {
            Assert.fail("Can't connect to HMaster ConnectorServer.");
        }
    }
    Assert.assertNotNull("JMXConnector should not be null.", connector);
    connector.close();
}

From source file:FullThreadDump.java

/**
 * Connect to a JMX agent of a given URL.
 *//*from w w  w  .ja v a 2  s . co m*/
private void connect(String urlPath) {
    try {
        JMXServiceURL url = new JMXServiceURL("rmi", "", 0, urlPath);
        this.jmxc = JMXConnectorFactory.connect(url);
        this.server = jmxc.getMBeanServerConnection();
    } catch (MalformedURLException e) {
        // should not reach here
    } catch (IOException e) {
        System.err.println("\nCommunication error: " + e.getMessage());
        System.exit(1);
    }
}

From source file:org.ngrinder.monitor.share.domain.MBeanClient.java

private JMXConnector connectWithTimeout(final JMXServiceURL jmxUrl, int timeout)
        throws NGrinderRuntimeException, TimeoutException {
    try {//from  w  w  w .  ja v a2s  .  c om
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<JMXConnector> future = executor.submit(new Callable<JMXConnector>() {
            public JMXConnector call() throws IOException {
                return JMXConnectorFactory.connect(jmxUrl);
            }
        });

        return future.get(timeout, TimeUnit.MILLISECONDS);
    } catch (TimeoutException e) {
        throw e;
    } catch (Exception e) {
        throw processException(e);
    }

}

From source file:org.apache.hadoop.hbase.TestJMXConnectorServer.java

/**
 * This tests to validate the RegionServer's ConnectorServer after unauthorised stopRegionServer
 * call.//w  w w.j a  va  2  s .  c o m
 */
@Test(timeout = 180000)
public void testRSConnectorServerWhenStopRegionServer() throws Exception {
    conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
            JMXListener.class.getName() + "," + MyAccessController.class.getName());
    conf.setInt("regionserver.rmi.registry.port", rmiRegistryPort);
    UTIL.startMiniCluster();
    admin = UTIL.getConnection().getAdmin();

    hasAccess = false;
    ServerName serverName = UTIL.getHBaseCluster().getRegionServer(0).getServerName();
    LOG.info("Stopping Region Server...");
    admin.stopRegionServer(serverName.getHostname() + ":" + serverName.getPort());

    // Check whether Region Sever JMX Connector server can be connected
    JMXConnector connector = null;
    try {
        connector = JMXConnectorFactory
                .connect(JMXListener.buildJMXServiceURL(rmiRegistryPort, rmiRegistryPort));
    } catch (IOException e) {
        if (e.getCause() instanceof ServiceUnavailableException) {
            Assert.fail("Can't connect to Region Server ConnectorServer.");
        }
    }
    Assert.assertNotNull("JMXConnector should not be null.", connector);
    connector.close();
}

From source file:com.cognifide.aet.runner.util.MessagesManager.java

protected JMXConnector getJmxConnection(String jmxUrl) throws IOException {
    JMXServiceURL url = new JMXServiceURL(jmxUrl);
    return JMXConnectorFactory.connect(url);
}