Example usage for javax.management.remote JMXServiceURL JMXServiceURL

List of usage examples for javax.management.remote JMXServiceURL JMXServiceURL

Introduction

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

Prototype

public JMXServiceURL(String serviceURL) throws MalformedURLException 

Source Link

Document

Constructs a JMXServiceURL by parsing a Service URL string.

Usage

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  w  w. ja va  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);
        }
    }
}

From source file:org.apache.geode.admin.jmx.internal.AgentImpl.java

/**
 * Defines and starts the JMX RMIConnector and service.
 * <p>/*from  w  ww. ja  va 2 s .c  o  m*/
 * If {@link AgentConfig#isRmiEnabled} returns false, then this adaptor will not be started.
 */
private void startRMIConnectorServer() {
    if (!this.agentConfig.isRmiEnabled())
        return;

    String rmiBindAddress = this.agentConfig.getRmiBindAddress();

    // Set RMI Stubs to use the given RMI Bind Address
    // Default bindAddress is "", if none is set - ignore if not set
    // If java.rmi.server.hostname property is specified then
    // that override is not changed
    String rmiStubServerNameKey = "java.rmi.server.hostname";
    String overrideHostName = System.getProperty(rmiStubServerNameKey);
    if ((overrideHostName == null || overrideHostName.trim().length() == 0)
            && (rmiBindAddress != null && rmiBindAddress.trim().length() != 0)) {
        System.setProperty(rmiStubServerNameKey, rmiBindAddress);
        logger.info(LocalizedMessage.create(LocalizedStrings.AgentImpl_SETTING_0,
                new StringBuilder(rmiStubServerNameKey).append(" = ").append(rmiBindAddress)));
    }

    try {
        createRMIRegistry();
        ObjectName objName = getRMIConnectorServerName();

        // make sure this adaptor is not already registered...
        if (getMBeanServer().isRegistered(objName)) {
            // dunno how we got here...
            logger.info(LocalizedMessage
                    .create(LocalizedStrings.AgentImpl_RMICONNECTORSERVER_ALREADY_REGISTERED_AS__0, objName));
            return;
        }

        /*
         * url defined as: service:jmx:protocol:sap where 1. protocol: rmi 2. sap is:
         * [host[:port]][url-path] where host: rmi-binding-address port: rmi-server-port url-path:
         * /jndi/rmi://<rmi-binding-address>:<rmi-port><JNDI_NAME>
         */
        String urlString = null;
        String connectorServerHost = "";
        int connectorServerPort = this.agentConfig.getRmiServerPort();
        String rmiRegistryHost = "";
        int rmiRegistryPort = this.agentConfig.getRmiPort();

        // Set registryHost to localhost if not specified
        // RMI stubs would use a default IP if namingHost is left empty
        if (rmiBindAddress == null || rmiBindAddress.trim().length() == 0) {
            connectorServerHost = "localhost";
            rmiRegistryHost = "";
        } else {
            connectorServerHost = applyRFC2732(rmiBindAddress);
            rmiRegistryHost = connectorServerHost;
        }

        urlString = MessageFormat.format(AgentImpl.JMX_SERVICE_URL, connectorServerHost,
                String.valueOf(connectorServerPort), rmiRegistryHost, String.valueOf(rmiRegistryPort),
                JNDI_NAME);

        logger.debug("JMX Service URL string is : \"{}\"", urlString);

        // The address of the connector
        JMXServiceURL url = new JMXServiceURL(urlString);

        Map<String, Object> env = new HashMap<String, Object>();
        // env.put(Context.INITIAL_CONTEXT_FACTORY,
        // "com.sun.jndi.rmi.registry.RegistryContextFactory");
        // env.put(Context.PROVIDER_URL, "rmi://localhost:1099");

        RMIServerSocketFactory ssf = new MX4JServerSocketFactory(this.agentConfig.isAgentSSLEnabled(), // true,
                this.agentConfig.isAgentSSLRequireAuth(), // true,
                this.agentConfig.getAgentSSLProtocols(), // "any",
                this.agentConfig.getAgentSSLCiphers(), // "any",
                this.agentConfig.getRmiBindAddress(), 10, // backlog
                this.agentConfig.getGfSecurityProperties());
        env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, ssf);

        if (this.agentConfig.isAgentSSLEnabled()) {
            RMIClientSocketFactory csf = new SslRMIClientSocketFactory();
            env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, csf);
        }

        MBeanServer mbs = null; // will be set by registering w/ mbeanServer
        this.rmiConnector = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);

        // for cleanup
        this.rmiConnector.addNotificationListener(new ConnectionNotificationAdapter(),
                new ConnectionNotificationFilterImpl(), this);

        // Register the JMXConnectorServer in the MBeanServer
        getMBeanServer().registerMBean(this.rmiConnector, objName);

        // Start the JMXConnectorServer
        this.rmiConnector.start();
    } catch (VirtualMachineError err) {
        SystemFailure.initiateFailure(err);
        // If this ever returns, rethrow the error. We're poisoned
        // now, so don't let this thread continue.
        throw err;
    } catch (Throwable t) {
        // Whenever you catch Error or Throwable, you must also
        // catch VirtualMachineError (see above). However, there is
        // _still_ a possibility that you are dealing with a cascading
        // error condition, so you also need to check to see if the JVM
        // is still usable:
        SystemFailure.checkFailure();
        logger.error(LocalizedStrings.AgentImpl_FAILED_TO_START_RMICONNECTORSERVER, t);
        throw new StartupException(LocalizedStrings.AgentImpl_FAILED_TO_START_RMI_SERVICE.toLocalizedString(),
                t);
    }
}

From source file:org.kuali.test.utils.Utils.java

public static JMXConnector getJMXConnector(String url, String username, String password)
        throws MalformedURLException, IOException {
    JMXConnector retval = null;/*from  ww w  .j a v a  2s .  c  om*/

    if (StringUtils.isNotBlank(url)) {
        JMXServiceURL serviceUrl = new JMXServiceURL(url);

        Map map = null;

        if (StringUtils.isNotBlank(username)) {
            map = new HashMap();
            map.put(JMXConnector.CREDENTIALS, new String[] { username, password });
        }

        retval = JMXConnectorFactory.connect(serviceUrl, map);
    }

    return retval;
}