Java Code Examples for com.sun.tools.attach.VirtualMachine#startLocalManagementAgent()

The following examples show how to use com.sun.tools.attach.VirtualMachine#startLocalManagementAgent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StartManagementAgent.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void testLocalAgent(VirtualMachine vm) throws Exception {
    Properties agentProps = vm.getAgentProperties();
    String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address != null) {
        throw new Exception("Local management agent already started");
    }

    String result = vm.startLocalManagementAgent();

    // try to parse the return value as a JMXServiceURL
    new JMXServiceURL(result);

    agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address == null) {
        throw new Exception("Local management agent could not be started");
    }
}
 
Example 2
Source File: LocalVirtualMachine.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void loadManagementAgent() throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        IOException ioe = new IOException(x.getMessage());
        ioe.initCause(x);
        throw ioe;
    }

    vm.startLocalManagementAgent();

    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

    vm.detach();
}
 
Example 3
Source File: StartManagementAgent.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void testLocalAgent(VirtualMachine vm) throws Exception {
    Properties agentProps = vm.getAgentProperties();
    String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address != null) {
        throw new Exception("Local management agent already started");
    }

    String result = vm.startLocalManagementAgent();

    // try to parse the return value as a JMXServiceURL
    new JMXServiceURL(result);

    agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address == null) {
        throw new Exception("Local management agent could not be started");
    }
}
 
Example 4
Source File: LocalVirtualMachine.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private void loadManagementAgent() throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        IOException ioe = new IOException(x.getMessage());
        ioe.initCause(x);
        throw ioe;
    }

    vm.startLocalManagementAgent();

    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

    vm.detach();
}
 
Example 5
Source File: StartManagementAgent.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void testLocalAgent(VirtualMachine vm) throws Exception {
    System.out.println("Getting VM properties");
    Properties agentProps = vm.getAgentProperties();
    String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address != null) {
        throw new Exception("Local management agent already started");
    }

    System.out.println("Starting local agent");

    String result = vm.startLocalManagementAgent();

    System.out.println("Agent started");

    // try to parse the return value as a JMXServiceURL
    new JMXServiceURL(result);

    agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address == null) {
        throw new Exception("Local management agent could not be started");
    }
}
 
Example 6
Source File: LocalVirtualMachine.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void loadManagementAgent() throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        IOException ioe = new IOException(x.getMessage());
        ioe.initCause(x);
        throw ioe;
    }

    vm.startLocalManagementAgent();

    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

    vm.detach();
}
 
Example 7
Source File: StartManagementAgent.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void testLocalAgent(VirtualMachine vm) throws Exception {
    Properties agentProps = vm.getAgentProperties();
    String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address != null) {
        throw new Exception("Local management agent already started");
    }

    String result = vm.startLocalManagementAgent();

    // try to parse the return value as a JMXServiceURL
    new JMXServiceURL(result);

    agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address == null) {
        throw new Exception("Local management agent could not be started");
    }
}
 
Example 8
Source File: StartManagementAgent.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void testLocalAgent(VirtualMachine vm) throws Exception {
    Properties agentProps = vm.getAgentProperties();
    String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address != null) {
        throw new Exception("Local management agent already started");
    }

    String result = vm.startLocalManagementAgent();

    // try to parse the return value as a JMXServiceURL
    new JMXServiceURL(result);

    agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address == null) {
        throw new Exception("Local management agent could not be started");
    }
}
 
Example 9
Source File: VirtualMachineMetrics.java    From spring-boot-graal-feature with Apache License 2.0 6 votes vote down vote up
public static Map<String, Long> fetch(String pid) {
    if (pid == null) {
        return Collections.emptyMap();
    }
    try {
        VirtualMachine vm = VirtualMachine.attach(pid);
        vm.startLocalManagementAgent();
        String connectorAddress = vm.getAgentProperties()
                .getProperty(CONNECTOR_ADDRESS);
        JMXServiceURL url = new JMXServiceURL(connectorAddress);
        JMXConnector connector = JMXConnectorFactory.connect(url);
        MBeanServerConnection connection = connector.getMBeanServerConnection();
        gc(connection);
        Map<String, Long> metrics = new HashMap<>(
                new BufferPools(connection).getMetrics());
        metrics.putAll(new Threads(connection).getMetrics());
        metrics.putAll(new Classes(connection).getMetrics());
        vm.detach();
        return metrics;
    }
    catch (Exception e) {
        return Collections.emptyMap();
    }
}
 
Example 10
Source File: LocalVirtualMachine.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void loadManagementAgent() throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        IOException ioe = new IOException(x.getMessage());
        ioe.initCause(x);
        throw ioe;
    }

    vm.startLocalManagementAgent();

    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

    vm.detach();
}
 
Example 11
Source File: VirtualMachineMetrics.java    From spring-init with Apache License 2.0 6 votes vote down vote up
public static Map<String, Long> fetch(String pid) {
	if (pid == null) {
		return Collections.emptyMap();
	}
	try {
		VirtualMachine vm = VirtualMachine.attach(pid);
		vm.startLocalManagementAgent();
		String connectorAddress = vm.getAgentProperties()
				.getProperty(CONNECTOR_ADDRESS);
		JMXServiceURL url = new JMXServiceURL(connectorAddress);
		JMXConnector connector = JMXConnectorFactory.connect(url);
		MBeanServerConnection connection = connector.getMBeanServerConnection();
		gc(connection);
		Map<String, Long> metrics = new HashMap<>(
				new BufferPools(connection).getMetrics());
		metrics.putAll(new Threads(connection).getMetrics());
		metrics.putAll(new Classes(connection).getMetrics());
		vm.detach();
		return metrics;
	}
	catch (Exception e) {
		return Collections.emptyMap();
	}
}
 
Example 12
Source File: StartManagementAgent.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void testLocalAgent(VirtualMachine vm) throws Exception {
    Properties agentProps = vm.getAgentProperties();
    String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address != null) {
        throw new Exception("Local management agent already started");
    }

    String result = vm.startLocalManagementAgent();

    // try to parse the return value as a JMXServiceURL
    new JMXServiceURL(result);

    agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address == null) {
        throw new Exception("Local management agent could not be started");
    }
}
 
Example 13
Source File: LocalVirtualMachine.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private void loadManagementAgent() throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        IOException ioe = new IOException(x.getMessage());
        ioe.initCause(x);
        throw ioe;
    }

    vm.startLocalManagementAgent();

    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

    vm.detach();
}
 
Example 14
Source File: StartManagementAgent.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void testLocalAgent(VirtualMachine vm) throws Exception {
    Properties agentProps = vm.getAgentProperties();
    String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address != null) {
        throw new Exception("Local management agent already started");
    }

    String result = vm.startLocalManagementAgent();

    // try to parse the return value as a JMXServiceURL
    new JMXServiceURL(result);

    agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    if (address == null) {
        throw new Exception("Local management agent could not be started");
    }
}
 
Example 15
Source File: LocalVirtualMachine.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void loadManagementAgent() throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        IOException ioe = new IOException(x.getMessage());
        ioe.initCause(x);
        throw ioe;
    }

    vm.startLocalManagementAgent();

    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

    vm.detach();
}
 
Example 16
Source File: LocalVirtualMachine.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private void loadManagementAgent() throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        IOException ioe = new IOException(x.getMessage());
        ioe.initCause(x);
        throw ioe;
    }

    vm.startLocalManagementAgent();

    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

    vm.detach();
}
 
Example 17
Source File: LocalVirtualMachine.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void loadManagementAgent() throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        IOException ioe = new IOException(x.getMessage());
        ioe.initCause(x);
        throw ioe;
    }

    vm.startLocalManagementAgent();

    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

    vm.detach();
}
 
Example 18
Source File: JCozServiceImpl.java    From JCoz with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int attachToProcess(int localProcessId) throws RemoteException {
    logger.info("Attaching to process {}", localProcessId);
    try {
        for (VirtualMachineDescriptor desc : VirtualMachine.list()) {
            if (Integer.parseInt(desc.id()) == localProcessId) {
                VirtualMachine vm = VirtualMachine.attach(desc);
                vm.startLocalManagementAgent();
                Properties props = vm.getAgentProperties();
                String connectorAddress = props
                    .getProperty(CONNECTOR_ADDRESS_PROPERTY_KEY);
                JMXServiceURL url = new JMXServiceURL(connectorAddress);
                JMXConnector connector = JMXConnectorFactory.connect(url);
                MBeanServerConnection mbeanConn = connector
                    .getMBeanServerConnection();
                attachedVMs.put(localProcessId, JMX.newMXBeanProxy(mbeanConn,
                            JCozProfiler.getMBeanName(),
                            JCozProfilerMBean.class));
                return JCozProfilingErrorCodes.NORMAL_RETURN;
            }
        }
    } catch (IOException | NumberFormatException
            | AttachNotSupportedException e) {
        StringWriter stringWriter = new StringWriter();
        e.printStackTrace(new PrintWriter(stringWriter));
        logger.error("Got an exception during attachToProcess, stacktrace: {}", stringWriter);
        throw new RemoteException("", e);

            }
    return JCozProfilingErrorCodes.INVALID_JAVA_PROCESS;
}
 
Example 19
Source File: AttachUtils.java    From java-svc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Used by other examples in other modules to establish a JMX connection to a locally running
 * JVM. If the local agent is already started, the service URL will still be returned.
 * 
 * @param pid
 *            the process identifier of the process to start the local management agent on.
 * @return the {@link JMXServiceURL} to use when connecting to the local management agent.
 * @throws AttachException
 */
public static JMXServiceURL startLocalAgent(String pid) throws AttachException {
	VirtualMachine vm = null;
	try {
		vm = VirtualMachine.attach(pid);
		return new JMXServiceURL(vm.startLocalManagementAgent());
	} catch (Throwable t) {
		throw new AttachException("Failed to attach to " + pid, t);
	} finally {
		if (vm != null) {
			detach(vm);
		}
	}
}
 
Example 20
Source File: Main.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Starts a JMX agent and checks that java.management is loaded.
 */
static void startJMXAgent(VirtualMachine vm) throws Exception {
    System.out.println("Start JMX agent");
    vm.startLocalManagementAgent();

    // types in java.management should be visible
    Class.forName("javax.management.MXBean");
}