Acquires a connected JMX connection - Java javax.management.remote

Java examples for javax.management.remote:JMXConnector

Description

Acquires a connected JMX connection

Demo Code

/**/*from   w  w w . j a  v  a 2s.  c  om*/
 * Helios, OpenSource Monitoring
 * Brought to you by the Helios Development Group
 *
 * Copyright 2007, Helios Development Group and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
 *
 */
//package com.java2s;

import java.util.Map;

import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class Main {
    /**
     * Acquires a connected JMX connection
     * @param jmxUrl The JMXServiceURL of the service to connec to
     * @return a JMXConnector
     */
    public static JMXConnector getJMXConnection(CharSequence jmxUrl) {
        return getJMXConnection(jmxUrl, true, null);
    }

    /**
     * Acquires a JMX connection
     * @param jmxUrl The JMXServiceURL of the service to connec to
     * @param connect If true, the returned connector will be connected
     * @param environment a set of attributes to determine how the connection is made. Can be null.
     * @return a JMXConnector
     */
    public static JMXConnector getJMXConnection(CharSequence jmxUrl,
            boolean connect, Map<String, ?> environment) {
        if (jmxUrl == null)
            throw new IllegalArgumentException(
                    "The passed JMXServiceURL was null", new Throwable());
        try {

            JMXConnector connector = JMXConnectorFactory.newJMXConnector(
                    new JMXServiceURL(jmxUrl.toString().trim()),
                    environment);
            if (connect) {
                connector.connect();
            }
            return connector;
        } catch (Exception e) {
            throw new RuntimeException(
                    "Failed to acquire JMXConnection to [" + jmxUrl + "]",
                    e);
        }
    }
}

Related Tutorials