Search in sources :

Example 1 with VirtualMachine

use of com.sun.tools.attach.VirtualMachine in project druid by alibaba.

the class DruidStat method loadManagementAgentAndGetAddress.

private static String loadManagementAgentAndGetAddress(int vmid) throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        throw new IOException(x.getMessage(), x);
    }
    String home = vm.getSystemProperties().getProperty("java.home");
    // Normally in ${java.home}/jre/lib/management-agent.jar but might
    // be in ${java.home}/lib in build environments.
    String agent = home + File.separator + "jre" + File.separator + "lib" + File.separator + "management-agent.jar";
    File f = new File(agent);
    if (!f.exists()) {
        agent = home + File.separator + "lib" + File.separator + "management-agent.jar";
        f = new File(agent);
        if (!f.exists()) {
            throw new IOException("Management agent not found");
        }
    }
    agent = f.getCanonicalPath();
    try {
        vm.loadAgent(agent, "com.sun.management.jmxremote");
    } catch (AgentLoadException x) {
        throw new IOException(x.getMessage(), x);
    } catch (AgentInitializationException x) {
        throw new IOException(x.getMessage(), x);
    }
    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    vm.detach();
    return address;
}
Also used : AgentLoadException(com.sun.tools.attach.AgentLoadException) IOException(java.io.IOException) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException) Properties(java.util.Properties) File(java.io.File) AgentInitializationException(com.sun.tools.attach.AgentInitializationException) VirtualMachine(com.sun.tools.attach.VirtualMachine)

Example 2 with VirtualMachine

use of com.sun.tools.attach.VirtualMachine in project jdk8u_jdk by JetBrains.

the class StartManagementAgent method runTests.

public static void runTests(int pid) throws Exception {
    VirtualMachine vm = VirtualMachine.attach("" + pid);
    try {
        basicTests(vm);
        testLocalAgent(vm);
        // we retry the remote case several times in case the error
        // was caused by a port conflict
        int i = 0;
        boolean success = false;
        do {
            try {
                System.err.println("Trying remote agent. Try #" + i);
                testRemoteAgent(vm);
                success = true;
            } catch (Exception ex) {
                System.err.println("testRemoteAgent failed with exception:");
                ex.printStackTrace();
                System.err.println("Retrying.");
            }
            i++;
        } while (!success && i < MAX_RETRIES);
        if (!success) {
            throw new Exception("testRemoteAgent failed after " + MAX_RETRIES + " tries");
        }
    } finally {
        vm.detach();
    }
}
Also used : AttachOperationFailedException(com.sun.tools.attach.AttachOperationFailedException) VirtualMachine(com.sun.tools.attach.VirtualMachine)

Example 3 with VirtualMachine

use of com.sun.tools.attach.VirtualMachine in project jdk8u_jdk by JetBrains.

the class TestManager method main.

public static void main(String[] args) throws Exception {
    // pid as a string
    String pid = args[0];
    System.out.println("Starting TestManager for PID = " + pid);
    System.out.flush();
    VirtualMachine vm = VirtualMachine.attach(pid);
    String agentPropLocalConnectorAddress = (String) vm.getAgentProperties().get(LOCAL_CONNECTOR_ADDRESS_PROP);
    int vmid = Integer.parseInt(pid);
    String jvmstatLocalConnectorAddress = ConnectorAddressLink.importFrom(vmid);
    if (agentPropLocalConnectorAddress == null && jvmstatLocalConnectorAddress == null) {
        // No JMX Connector address so attach to VM, and start local agent
        startManagementAgent(pid);
        agentPropLocalConnectorAddress = (String) vm.getAgentProperties().get(LOCAL_CONNECTOR_ADDRESS_PROP);
        jvmstatLocalConnectorAddress = ConnectorAddressLink.importFrom(vmid);
    }
    // Test address obtained from agent properties
    System.out.println("Testing the connector address from agent properties");
    connect(pid, agentPropLocalConnectorAddress);
    // Test address obtained from jvmstat buffer
    System.out.println("Testing the connector address from jvmstat buffer");
    connect(pid, jvmstatLocalConnectorAddress);
    // Shutdown application
    int port = Integer.parseInt(args[1]);
    System.out.println("Shutdown process via TCP port: " + port);
    Socket s = new Socket();
    s.connect(new InetSocketAddress(port));
    s.close();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) VirtualMachine(com.sun.tools.attach.VirtualMachine)

Example 4 with VirtualMachine

use of com.sun.tools.attach.VirtualMachine in project opennms by OpenNMS.

the class Controller method getJmxUrl.

/**
     * This method uses the Java Attach API to connect to a running OpenNMS JVM
     * and fetch the dynamically assigned local JMX agent URL.
     * 
     * @see https://docs.oracle.com/javase/8/docs/jdk/api/attach/spec/index.html
     * @see https://docs.oracle.com/javase/8/docs/technotes/guides/management/agent.html
     *
     * @return a {@link java.lang.String} object.
     */
public String getJmxUrl() {
    try {
        // Check to see if the com.sun.tools.attach classes are loadable in
        // this JVM
        Class<?> clazz;
        clazz = Class.forName("com.sun.tools.attach.VirtualMachine");
        clazz = Class.forName("com.sun.tools.attach.VirtualMachineDescriptor");
        clazz = Class.forName("com.sun.tools.attach.AttachNotSupportedException");
    } catch (ClassNotFoundException e) {
        LOG.info("The Attach API is not available in this JVM, falling back to JMX over RMI");
        return m_jmxUrl;
    }
    VirtualMachine vm = null;
    StringBuffer vmNames = new StringBuffer();
    boolean first = true;
    // Use the Attach API to enumerate all of the JVMs that are running as the same
    // user on this machine
    VirtualMachineDescriptor foundVm = null;
    for (VirtualMachineDescriptor vmDescr : VirtualMachine.list()) {
        if (!first) {
            vmNames.append(", ");
        }
        vmNames.append("\"" + vmDescr.displayName() + "\"");
        first = false;
        if (vmDescr.displayName().contains(OPENNMS_JVM_DISPLAY_NAME_SUBSTRING)) {
            foundVm = vmDescr;
        }
    }
    if (foundVm == null) {
        LOG.debug("Could not find OpenNMS JVM (\"" + OPENNMS_JVM_DISPLAY_NAME_SUBSTRING + "\") among JVMs (" + vmNames + ")");
    } else {
        try {
            vm = VirtualMachine.attach(foundVm);
            LOG.debug("Attached to OpenNMS JVM: " + foundVm.id() + " (" + foundVm.displayName() + ")");
        } catch (AttachNotSupportedException e) {
            // This exception is unexpected so log a warning
            LOG.warn("Cannot attach to OpenNMS JVM", e);
        } catch (IOException e) {
            // This exception is unexpected so log a warning
            LOG.warn("IOException when attaching to OpenNMS JVM", e);
        }
    }
    if (vm == null) {
        if (m_pid == null) {
            LOG.debug("No PID specified for OpenNMS JVM");
        } else {
            try {
                vm = VirtualMachine.attach(m_pid);
                LOG.debug("Attached to OpenNMS JVM with PID: " + m_pid);
            } catch (AttachNotSupportedException e) {
                // This exception is unexpected so log a warning
                LOG.warn("Cannot attach to OpenNMS JVM at PID: " + m_pid, e);
            } catch (IOException e) {
                // This exception will occur if the PID cannot be found
                // because the process has been terminated
                LOG.debug("IOException when attaching to OpenNMS JVM at PID: " + m_pid + ": " + e.getMessage());
            }
        }
    }
    if (vm == null) {
        LOG.debug("Could not attach to JVM, falling back to JMX over RMI");
        return m_jmxUrl;
    } else {
        return getJmxUriFromVirtualMachine(vm);
    }
}
Also used : VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) IOException(java.io.IOException) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException) VirtualMachine(com.sun.tools.attach.VirtualMachine)

Example 5 with VirtualMachine

use of com.sun.tools.attach.VirtualMachine in project ACS by ACS-Community.

the class GCJMXClient method getVM.

private VirtualMachine getVM(int pid) throws AttachNotSupportedException, IOException {
    if (_vm == null) {
        VirtualMachine vm = VirtualMachine.attach(String.valueOf(pid));
        _vm = vm;
    }
    return _vm;
}
Also used : VirtualMachine(com.sun.tools.attach.VirtualMachine)

Aggregations

VirtualMachine (com.sun.tools.attach.VirtualMachine)17 IOException (java.io.IOException)8 AttachNotSupportedException (com.sun.tools.attach.AttachNotSupportedException)6 InputStream (java.io.InputStream)5 HotSpotVirtualMachine (sun.tools.attach.HotSpotVirtualMachine)5 File (java.io.File)4 AgentInitializationException (com.sun.tools.attach.AgentInitializationException)3 AgentLoadException (com.sun.tools.attach.AgentLoadException)3 VirtualMachineDescriptor (com.sun.tools.attach.VirtualMachineDescriptor)3 Properties (java.util.Properties)3 JMXServiceURL (javax.management.remote.JMXServiceURL)3 AttachOperationFailedException (com.sun.tools.attach.AttachOperationFailedException)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 Test (org.junit.Test)1