Search in sources :

Example 6 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project jersey by jersey.

the class Main method main.

public static void main(String[] args) throws Exception {
    //      e.g. "service:jmx:rmi:///jndi/rmi://sysifos.cz.oracle.com:11112/jmxrmi"
    final String jmxUrl = args[0];
    //      e.g. "org.glassfish.jersey.test.performance.interceptor.dynamic:type=DynamicallyBoundInterceptorResource,name=gets"
    final String mBeanName = args[1];
    //      e.g. "OneMinuteRate"
    final String mBeanAttrName = args[2];
    //      e.g. 50
    final int sampleCount = Integer.parseInt(args[3]);
    //      e.g. "phishing.properties"
    final String propertiesFile = args[4];
    System.out.printf("JMX URL = %s\nMBean = %s\nattribute = %s\nsamples = %d\nfilename = %s\n" + "Going to connect...\n", jmxUrl, mBeanName, mBeanAttrName, sampleCount, propertiesFile);
    final JMXServiceURL url = new JMXServiceURL(jmxUrl);
    final JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
    final MBeanServerConnection mBeanServer = jmxc.getMBeanServerConnection();
    final ObjectName mBeanObjectName = new ObjectName(mBeanName);
    System.out.println("Connected...");
    double totalSum = 0;
    int samplesTaken = 0;
    for (int i = 0; i < sampleCount; i++) {
        Thread.sleep(5000);
        Double sample = (Double) mBeanServer.getAttribute(mBeanObjectName, mBeanAttrName);
        System.out.printf("OMR[%d]=%f\n", i, sample);
        totalSum += sample;
        samplesTaken++;
    }
    jmxc.close();
    final double result = totalSum / samplesTaken;
    writeResult(result, propertiesFile);
    System.out.printf("\nAverage=%f\n", result);
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName)

Example 7 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project hadoop by apache.

the class JMXGet method init.

/**
   * @throws Exception
   *           initializes MBeanServer
   */
public void init() throws Exception {
    err("init: server=" + server + ";port=" + port + ";service=" + service + ";localVMUrl=" + localVMUrl);
    String url_string = null;
    // build connection url
    if (localVMUrl != null) {
        // use
        // jstat -snap <vmpid> | grep sun.management.JMXConnectorServer.address
        // to get url
        url_string = localVMUrl;
        err("url string for local pid = " + localVMUrl + " = " + url_string);
    } else if (!port.isEmpty() && !server.isEmpty()) {
        // using server and port
        url_string = "service:jmx:rmi:///jndi/rmi://" + server + ":" + port + "/jmxrmi";
    }
    if (url_string == null) {
        // assume local vm (for example for Testing)
        mbsc = ManagementFactory.getPlatformMBeanServer();
    } else {
        JMXServiceURL url = new JMXServiceURL(url_string);
        err("Create RMI connector and connect to the RMI connector server" + url);
        JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
        // Get an MBeanServerConnection
        //
        err("\nGet an MBeanServerConnection");
        mbsc = jmxc.getMBeanServerConnection();
    }
    // Get domains from MBeanServer
    //
    err("\nDomains:");
    String[] domains = mbsc.getDomains();
    Arrays.sort(domains);
    for (String domain : domains) {
        err("\tDomain = " + domain);
    }
    // Get MBeanServer's default domain
    //
    err("\nMBeanServer default domain = " + mbsc.getDefaultDomain());
    // Get MBean count
    //
    err("\nMBean count = " + mbsc.getMBeanCount());
    // Query MBean names for specific domain "hadoop" and service
    ObjectName query = new ObjectName("Hadoop:service=" + service + ",*");
    hadoopObjectNames = new ArrayList<ObjectName>(5);
    err("\nQuery MBeanServer MBeans:");
    Set<ObjectName> names = new TreeSet<ObjectName>(mbsc.queryNames(query, null));
    for (ObjectName name : names) {
        hadoopObjectNames.add(name);
        err("Hadoop service: " + name);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) TreeSet(java.util.TreeSet) ObjectName(javax.management.ObjectName)

Example 8 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project neo4j by neo4j.

the class HotspotManagementSupport method getJMXServiceURL.

@Override
protected JMXServiceURL getJMXServiceURL(KernelData kernel) {
    JMXServiceURL url = null;
    Log log = kernel.graphDatabase().getDependencyResolver().resolveDependency(LogService.class).getInternalLog(HotspotManagementSupport.class);
    try {
        Class<?> cal = Class.forName("sun.management.ConnectorAddressLink");
        try {
            Method importRemoteFrom = cal.getMethod("importRemoteFrom", int.class);
            @SuppressWarnings("unchecked") Map<String, String> remote = (Map<String, String>) importRemoteFrom.invoke(null, 0);
            url = getUrlFrom(remote);
        } catch (NoSuchMethodException ex) {
        // handled by null check
        }
        if (url == null) {
            Method importFrom = cal.getMethod("importFrom", int.class);
            url = getUrlFrom((String) importFrom.invoke(null, 0));
        }
    } catch (InvocationTargetException e) {
        log.warn("Failed to load local JMX connection URL.", e.getTargetException());
    } catch (LinkageError | Exception e) {
        log.warn("Failed to load local JMX connection URL.", e);
    }
    // No previous connection server -- create one!
    if (url == null) {
        int port = 0;
        try {
            port = kernel.getConfig().get(setting("jmx.port", INTEGER, "0"));
        } catch (NumberFormatException ok) {
        // handled by 0-check
        }
        if (port > 0) {
            boolean useSSL = kernel.getConfig().get(setting("jmx.use_ssl", BOOLEAN, "false"));
            log.debug("Creating new MBean server on port %s%s", port, useSSL ? " using ssl" : "");
            JMXConnectorServer server = createServer(port, useSSL, log);
            if (server != null) {
                try {
                    server.start();
                } catch (IOException e) {
                    log.warn("Failed to start MBean server", e);
                    server = null;
                }
                if (server != null) {
                    try {
                        server.getMBeanServer().registerMBean(server, KernelProxy.createObjectName(kernel.instanceId(), "JMX Server"));
                    } catch (Exception e) {
                        log.warn("Failed to register MBean server as JMX bean", e);
                    }
                    url = server.getAddress();
                }
            }
        }
    }
    return url;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) Log(org.neo4j.logging.Log) Method(java.lang.reflect.Method) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JMXConnectorServer(javax.management.remote.JMXConnectorServer) HashMap(java.util.HashMap) Map(java.util.Map) LogService(org.neo4j.kernel.impl.logging.LogService)

Example 9 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project neo4j by neo4j.

the class HotspotManagementSupport method createServer.

private JMXConnectorServer createServer(int port, boolean useSSL, Log log) {
    MBeanServer server = getMBeanServer();
    final JMXServiceURL url;
    try {
        url = new JMXServiceURL("rmi", null, port);
    } catch (MalformedURLException e) {
        log.warn("Failed to start JMX Server", e);
        return null;
    }
    Map<String, Object> env = new HashMap<>();
    if (useSSL) {
        env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, new SslRMIClientSocketFactory());
        env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, new SslRMIServerSocketFactory());
    }
    try {
        return JMXConnectorServerFactory.newJMXConnectorServer(url, env, server);
    } catch (IOException e) {
        log.warn("Failed to start JMX Server", e);
        return null;
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) SslRMIClientSocketFactory(javax.rmi.ssl.SslRMIClientSocketFactory) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) SslRMIServerSocketFactory(javax.rmi.ssl.SslRMIServerSocketFactory) IOException(java.io.IOException) MBeanServer(javax.management.MBeanServer)

Example 10 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project opennms by OpenNMS.

the class AbstractSystemReportPlugin method getConnection.

private MBeanServerConnection getConnection() {
    final List<Integer> ports = new ArrayList<Integer>();
    Integer p = Integer.getInteger("com.sun.management.jmxremote.port");
    if (p != null)
        ports.add(p);
    ports.add(18980);
    ports.add(1099);
    for (final Integer port : ports) {
        LOG.trace("Trying JMX at localhost:{}/jmxrmi", port);
        try {
            JMXServiceURL url = new JMXServiceURL(String.format("service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi", port));
            JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
            return jmxc.getMBeanServerConnection();
        } catch (final Exception e) {
            LOG.debug("Unable to get JMX connection to OpenNMS on port {}.", port, e);
        }
    }
    return null;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Aggregations

JMXServiceURL (javax.management.remote.JMXServiceURL)273 JMXConnector (javax.management.remote.JMXConnector)146 MBeanServerConnection (javax.management.MBeanServerConnection)107 ObjectName (javax.management.ObjectName)90 IOException (java.io.IOException)75 HashMap (java.util.HashMap)75 MBeanServer (javax.management.MBeanServer)70 JMXConnectorServer (javax.management.remote.JMXConnectorServer)64 MalformedURLException (java.net.MalformedURLException)45 RemoteException (java.rmi.RemoteException)22 Test (org.junit.Test)20 Map (java.util.Map)16 UnknownHostException (java.net.UnknownHostException)14 Notification (javax.management.Notification)14 NotificationListener (javax.management.NotificationListener)14 Properties (java.util.Properties)13 MalformedObjectNameException (javax.management.MalformedObjectNameException)13 LocateRegistry (java.rmi.registry.LocateRegistry)12 Registry (java.rmi.registry.Registry)12 ArrayList (java.util.ArrayList)11