Search in sources :

Example 61 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project warn-report by saaavsaaa.

the class TestAgentVMAttacher method test.

// consult: http://ayufox.iteye.com/blog/653214
/*
    * HotSpotDiagnosticMXBean
    * ClassLoadingMXBean
    * CompilationMXBean
    * GarbageCollectorMXBean
    * MemoryManagerMXBean
    * MemoryPoolMXBean
    * OperatingSystemMXBean
    * RuntimeMXBean
    * ThreadMXBeanisolatering
    * */
@Test
public void test() throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
    // args[0]传入的是某个jvm进程的pid
    String targetPid = "9527";
    VirtualMachine virtualmachine = VirtualMachine.attach(targetPid);
    // 让JVM加载jmx Agent
    String javaHome = virtualmachine.getSystemProperties().getProperty("java.home");
    String jmxAgent = javaHome + File.separator + "lib" + File.separator + "management-agent.jar";
    virtualmachine.loadAgent(jmxAgent, "com.sun.management.jmxremote");
    // 获得连接地址
    Properties properties = virtualmachine.getAgentProperties();
    String address = (String) properties.get("com.sun.management.jmxremote.localConnectorAddress");
    // Detach
    virtualmachine.detach();
    // 通过jxm address来获取RuntimeMXBean对象,从而得到虚拟机运行时相关信息
    JMXServiceURL url = new JMXServiceURL(address);
    JMXConnector connector = JMXConnectorFactory.connect(url);
    RuntimeMXBean rmxb = ManagementFactory.newPlatformMXBeanProxy(connector.getMBeanServerConnection(), "java.lang:type=Runtime", RuntimeMXBean.class);
    // 得到目标虚拟机占用cpu时间
    System.out.println(rmxb.getInputArguments());
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) RuntimeMXBean(java.lang.management.RuntimeMXBean) Properties(java.util.Properties) VirtualMachine(com.sun.tools.attach.VirtualMachine) Test(org.junit.Test)

Example 62 with JMXServiceURL

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

the class MBServerConnectorFactory method createConnector.

public void createConnector() throws IOException {
    if (server == null) {
        server = MBeanServerHolder.INSTANCE;
    }
    // Create the JMX service URL.
    JMXServiceURL url = new JMXServiceURL(serviceUrl);
    // if the URL is localhost, start up an Registry
    if (serviceUrl.indexOf("localhost") > -1 && url.getProtocol().compareToIgnoreCase("rmi") == 0) {
        try {
            int port = getURLLocalHostPort(serviceUrl);
            try {
                LocateRegistry.createRegistry(port);
            } catch (Exception ex) {
                // the registry may had been created
                LocateRegistry.getRegistry(port);
            }
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, "CREATE_REGISTRY_FAULT_MSG", new Object[] { ex });
        }
    }
    // Create the connector server now.
    connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, environment, server);
    if (threaded) {
        // Start the connector server asynchronously (in a separate thread).
        Thread connectorThread = new Thread() {

            public void run() {
                try {
                    connectorServer.start();
                } catch (IOException ex) {
                    LOG.log(Level.SEVERE, "START_CONNECTOR_FAILURE_MSG", new Object[] { ex });
                }
            }
        };
        connectorThread.setName("JMX Connector Thread [" + serviceUrl + "]");
        connectorThread.setDaemon(daemon);
        connectorThread.start();
    } else {
        // Start the connector server in the same thread.
        connectorServer.start();
    }
    if (LOG.isLoggable(Level.INFO)) {
        LOG.info("JMX connector server started: " + connectorServer);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) IOException(java.io.IOException) IOException(java.io.IOException)

Example 63 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project fabric8 by jboss-fuse.

the class RestJsonSchemaJMXTest method connectToMBserver.

private void connectToMBserver() throws IOException {
    jmxServerURL = jmxServerURL == null ? DEFAULT_JMXSERVICE_URL : jmxServerURL;
    JMXServiceURL url = new JMXServiceURL(jmxServerURL);
    JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
    mbsc = jmxc.getMBeanServerConnection();
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector)

Example 64 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project fabric8 by jboss-fuse.

the class ProvisionSupport method getMBean.

public static Object getMBean(final Container container, final ObjectName mbeanName, final Class clazz, final long timeout) throws Exception {
    CompletionService<Object> completionService = new ExecutorCompletionService<Object>(EXECUTOR);
    completionService.submit(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            long time = 0;
            while (time <= timeout) {
                try {
                    JMXServiceURL url = new JMXServiceURL(container.getJmxUrl());
                    Map env = new HashMap();
                    String[] creds = { "admin", "admin" };
                    env.put(JMXConnector.CREDENTIALS, creds);
                    JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
                    MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
                    mbsc.getObjectInstance(mbeanName);
                    return JMX.newMBeanProxy(mbsc, mbeanName, clazz, true);
                } catch (Exception e) {
                    Thread.sleep(2000L);
                    time += 2000L;
                }
            }
            return null;
        }
    });
    return completionService.poll(timeout, TimeUnit.MILLISECONDS).get();
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) JMXConnector(javax.management.remote.JMXConnector) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) HashMap(java.util.HashMap) Map(java.util.Map) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 65 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project ysoserial by frohoff.

the class JBoss method makeVersionedConnection.

private static VersionedConnection makeVersionedConnection(Channel c) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, MalformedURLException {
    VersionedConnection vc;
    Class<?> vcf = Class.forName("org.jboss.remotingjmx.VersionedConectionFactory");
    Method vcCreate = vcf.getDeclaredMethod("createVersionedConnection", Channel.class, Map.class, JMXServiceURL.class);
    vcCreate.setAccessible(true);
    vc = (VersionedConnection) vcCreate.invoke(null, c, new HashMap(), new JMXServiceURL("service:jmx:remoting-jmx://"));
    return vc;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) VersionedConnection(org.jboss.remotingjmx.VersionedConnection) Method(java.lang.reflect.Method)

Aggregations

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