Search in sources :

Example 1 with AttachNotSupportedException

use of com.sun.tools.attach.AttachNotSupportedException 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 AttachNotSupportedException

use of com.sun.tools.attach.AttachNotSupportedException 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 3 with AttachNotSupportedException

use of com.sun.tools.attach.AttachNotSupportedException in project geode by apache.

the class MBeanProcessController method connect.

/**
   * Connects to the JMX agent in the local process.
   * 
   * @throws ConnectionFailedException if there was a failure to connect to the local JMX connector
   *         in the process
   * @throws IOException if the JDK management agent cannot be found and loaded
   */
private void connect() throws ConnectionFailedException, IOException {
    try {
        final JMXServiceURL jmxUrl = getJMXServiceURL();
        this.jmxc = JMXConnectorFactory.connect(jmxUrl);
        this.server = this.jmxc.getMBeanServerConnection();
    } catch (AttachNotSupportedException e) {
        throw new ConnectionFailedException("Failed to connect to process '" + this.pid + "'", e);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException)

Example 4 with AttachNotSupportedException

use of com.sun.tools.attach.AttachNotSupportedException in project geode by apache.

the class LocalProcessController method connect.

/**
   * Connects to the JMX agent in the local process.
   * 
   * @throws ConnectionFailedException if there was a failure to connect to the local JMX connector
   *         in the process
   * @throws IOException if the JDK management agent cannot be found and loaded
   */
void connect() throws ConnectionFailedException, IOException {
    try {
        final JMXServiceURL jmxUrl = getJMXServiceURL();
        this.jmxc = JMXConnectorFactory.connect(jmxUrl);
        this.server = this.jmxc.getMBeanServerConnection();
    } catch (AttachNotSupportedException e) {
        throw new ConnectionFailedException("Failed to connect to process '" + this.pid + "'", e);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException)

Example 5 with AttachNotSupportedException

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

the class HotSpotAttachProvider method testAttachable.

/**
     * Test if a VM is attachable. If it's not attachable,
     * an AttachNotSupportedException will be thrown. For example,
     * 1.4.2 or 5.0 VM are not attachable. There are cases that
     * we can't determine if a VM is attachable or not and this method
     * will just return.
     *
     * This method uses the jvmstat counter to determine if a VM
     * is attachable. If the target VM does not have a jvmstat
     * share memory buffer, this method returns.
     *
     * @exception AttachNotSupportedException if it's not attachable
     */
void testAttachable(String id) throws AttachNotSupportedException {
    MonitoredVm mvm = null;
    try {
        VmIdentifier vmid = new VmIdentifier(id);
        MonitoredHost host = MonitoredHost.getMonitoredHost(vmid);
        mvm = host.getMonitoredVm(vmid);
        if (MonitoredVmUtil.isAttachable(mvm)) {
            // it's attachable; so return false
            return;
        }
    } catch (Throwable t) {
        if (t instanceof ThreadDeath) {
            ThreadDeath td = (ThreadDeath) t;
            throw td;
        }
        // we do not know what this id is
        return;
    } finally {
        if (mvm != null) {
            mvm.detach();
        }
    }
    // we're sure it's not attachable; throw exception
    throw new AttachNotSupportedException("The VM does not support the attach mechanism");
}
Also used : MonitoredVm(sun.jvmstat.monitor.MonitoredVm) VmIdentifier(sun.jvmstat.monitor.VmIdentifier) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException) MonitoredHost(sun.jvmstat.monitor.MonitoredHost)

Aggregations

AttachNotSupportedException (com.sun.tools.attach.AttachNotSupportedException)9 VirtualMachine (com.sun.tools.attach.VirtualMachine)6 IOException (java.io.IOException)6 VirtualMachineDescriptor (com.sun.tools.attach.VirtualMachineDescriptor)2 JMXServiceURL (javax.management.remote.JMXServiceURL)2 AgentInitializationException (com.sun.tools.attach.AgentInitializationException)1 AgentLoadException (com.sun.tools.attach.AgentLoadException)1 File (java.io.File)1 InputStream (java.io.InputStream)1 Properties (java.util.Properties)1 MonitoredHost (sun.jvmstat.monitor.MonitoredHost)1 MonitoredVm (sun.jvmstat.monitor.MonitoredVm)1 VmIdentifier (sun.jvmstat.monitor.VmIdentifier)1 HotSpotVirtualMachine (sun.tools.attach.HotSpotVirtualMachine)1