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:org.rhq.plugins.jmx.JMXDiscoveryComponent.java

protected DiscoveredResourceDetails buildResourceDetails(ResourceDiscoveryContext context, ProcessInfo process,
        JMXServiceURL jmxServiceURL, Integer jmxRemotingPort) {
    JvmResourceKey key;/*from w  ww . j  av a 2 s  .co  m*/
    String mainClassName = getJavaMainClassName(process);
    String keyString = getSystemPropertyValue(process, SYSPROP_RHQ_RESOURCE_KEY);
    if (keyString != null && !keyString.equals("")) {
        log.debug("Using explicitly specified Resource key: [" + keyString + "]...");
        key = JvmResourceKey.fromExplicitValue(mainClassName, keyString);
    } else {
        if (jmxRemotingPort != null) {
            log.debug("Using JMX remoting port [" + jmxRemotingPort + "] as Resource key...");
            key = JvmResourceKey.fromJmxRemotingPort(mainClassName, jmxRemotingPort);
        } else {
            log.debug("Process [" + process.getPid() + "] with command line ["
                    + Arrays.asList(process.getCommandLine())
                    + "] cannot be discovered, because it does not specify either of the following system properties: "
                    + "-D" + SYSPROP_JMXREMOTE_PORT + "=12345, -D" + SYSPROP_RHQ_RESOURCE_KEY + "=UNIQUE_KEY");
            return null;
        }
    }

    String name = buildResourceName(key);

    String version;
    try {
        JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxServiceURL);
        MBeanServerConnection mbeanServerConnection = jmxConnector.getMBeanServerConnection();
        RuntimeMXBean runtimeMXBean = ManagementFactory.newPlatformMXBeanProxy(mbeanServerConnection,
                ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
        version = runtimeMXBean.getSystemProperties().get(SYSPROP_JAVA_VERSION);
        if (version == null) {
            throw new IllegalStateException("System property [" + SYSPROP_JAVA_VERSION + "] is not defined.");
        }
    } catch (Exception e) {
        log.error("Failed to determine JVM version for process [" + process.getPid() + "] with command line ["
                + Arrays.asList(process.getCommandLine()) + "].", e);
        version = null;
    }

    String description = "JVM, monitored via "
            + ((jmxRemotingPort != null) ? "JMX Remoting" : "Sun JVM Attach API");

    Configuration pluginConfig = context.getDefaultPluginConfiguration();
    pluginConfig.put(new PropertySimple(CONNECTION_TYPE, J2SE5ConnectionTypeDescriptor.class.getName()));
    if (jmxRemotingPort != null) {
        pluginConfig.put(new PropertySimple(CONNECTOR_ADDRESS_CONFIG_PROPERTY, jmxServiceURL));
    }

    return new DiscoveredResourceDetails(context.getResourceType(), key.toString(), name, version, description,
            pluginConfig, process);
}

From source file:com.betfair.testing.utils.cougar.helpers.CougarHelpers.java

public JMXConnector createJMXConnector(String id)
        throws IOException, AgentLoadException, AgentInitializationException, AttachNotSupportedException {

    // attach to the target application
    VirtualMachine vm = VirtualMachine.attach(id);

    // get the connector address
    String connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);

    // no connector address, so we start the JMX agent
    if (connectorAddress == null) {
        String agent = vm.getSystemProperties().getProperty("java.home") + File.separator + "lib"
                + File.separator + "management-agent.jar";
        vm.loadAgent(agent);/* w ww .j a  v  a2  s.  co m*/

        // agent is started, get the connector address
        connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
    }

    // establish connection to connector server
    JMXServiceURL url = new JMXServiceURL(connectorAddress);
    return JMXConnectorFactory.connect(url);
}

From source file:com.linkedin.d2.balancer.util.LoadBalancerClientCli.java

public static void resetTogglingStores(String host, boolean enabled) throws Exception {

    MonitoredHost _host = MonitoredHost.getMonitoredHost(new HostIdentifier(host));

    for (Object pidObj : _host.activeVms()) {
        int pid = (Integer) pidObj;

        System.out.println("checking pid: " + pid);

        JMXServiceURL jmxUrl = null;
        com.sun.tools.attach.VirtualMachine vm = com.sun.tools.attach.VirtualMachine.attach(pid + "");

        try {/*from w  ww  .  ja v a 2  s . c  o m*/
            // get the connector address
            String connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
            // establish connection to connector server
            if (connectorAddress != null) {
                jmxUrl = new JMXServiceURL(connectorAddress);
            }
        } finally {
            vm.detach();
        }

        if (jmxUrl != null) {
            System.out.println("got jmx url: " + jmxUrl);

            // connect to jmx
            JMXConnector connector = JMXConnectorFactory.connect(jmxUrl);

            connector.connect();

            MBeanServerConnection mbeanServer = connector.getMBeanServerConnection();

            // look for all beans in the d2 name space
            Set<ObjectInstance> objectInstances = mbeanServer.queryMBeans(new ObjectName("com.linkedin.d2:*"),
                    null);

            for (ObjectInstance objectInstance : objectInstances) {
                System.err.println("checking object: " + objectInstance.getObjectName());

                // if we've found a toggling store, then toggle it
                if (objectInstance.getObjectName().toString().endsWith("TogglingStore")) {
                    System.out.println("found toggling zk store, so toggling to: " + enabled);

                    mbeanServer.invoke(objectInstance.getObjectName(), "setEnabled", new Object[] { enabled },
                            new String[] { "boolean" });
                }
            }
        } else {
            System.out.println("pid is not a jmx process: " + pid);
        }
    }
}