Search in sources :

Example 21 with VirtualMachineDescriptor

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

the class ControllerTest method testAttach.

@Test
public void testAttach() throws Exception {
    for (VirtualMachineDescriptor vm : VirtualMachine.list()) {
        System.out.println(vm.displayName() + " [" + vm.id() + "]");
    }
    for (VirtualMachineDescriptor vmDescr : VirtualMachine.list()) {
        if (vmDescr.displayName().contains("opennms_bootstrap")) {
            // Attach to the OpenNMS application
            VirtualMachine vm = VirtualMachine.attach(vmDescr);
            // Get the local JMX connector URI
            String connectorAddress = vm.getAgentProperties().getProperty(Controller.CONNECTOR_ADDRESS);
            // JMX agent via this VirtualMachine attachment.
            if (connectorAddress == null) {
                System.out.println("Starting local management agent in JVM with ID: " + vm.id());
                // String agent = vm.getSystemProperties().getProperty("java.home") + File.separator + "lib" + File.separator + "management-agent.jar";
                // vm.loadAgent(agent);
                vm.startLocalManagementAgent();
                // agent is started, get the connector address
                connectorAddress = vm.getAgentProperties().getProperty(Controller.CONNECTOR_ADDRESS);
            }
            System.out.println(connectorAddress);
        }
    }
}
Also used : VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) VirtualMachine(com.sun.tools.attach.VirtualMachine) Test(org.junit.Test)

Example 22 with VirtualMachineDescriptor

use of com.sun.tools.attach.VirtualMachineDescriptor in project openj9 by eclipse.

the class SelfAttacher method main.

public static void main(String[] args) {
    /*
		 * This is launched as a child process by thge test process.
		 * It uses stderr to communicate its process ID to the test process
		 * and its exit code to indicate the result of the late attach attempt.
		 */
    try {
        String myId = VmIdGetter.getVmId();
        System.err.println("myId=" + myId);
        boolean found = false;
        for (int i = 0; i < 10 && !found; ++i) {
            Thread.sleep(100);
            for (VirtualMachineDescriptor v : VirtualMachine.list()) {
                if (v.id().equals(myId)) {
                    found = true;
                    break;
                }
            }
        }
        VirtualMachine vm = VirtualMachine.attach(myId);
        Properties props = vm.getSystemProperties();
        props.list(System.out);
    } catch (AttachNotSupportedException e) {
        e.printStackTrace();
        System.exit(ATTACH_NOT_SUPPORTED_CODE);
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(ATTACH_SELF_IOEXCEPTION_CODE);
    } catch (InterruptedException e) {
        e.printStackTrace();
        System.exit(ATTACH_ERROR_CODE);
    }
    System.exit(ATTACH_SELF_API_SUCCEEDED_CODE);
}
Also used : VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) IOException(java.io.IOException) Properties(java.util.Properties) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException) VirtualMachine(com.sun.tools.attach.VirtualMachine)

Example 23 with VirtualMachineDescriptor

use of com.sun.tools.attach.VirtualMachineDescriptor in project eclipse-integration-commons by spring-projects.

the class ProcessUtils method createJMXConnector.

/**
 * Creates JMX Connector to a process specified by its PID
 * @param pid the PID
 * @return JMX connector
 */
public static JMXConnector createJMXConnector(String pid) {
    List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
    VirtualMachineDescriptor vmd = vmds.stream().filter(descriptor -> descriptor.id().equals(pid)).findFirst().orElse(null);
    if (vmd != null) {
        try {
            String agentUrl = VirtualMachine.attach(vmd).startLocalManagementAgent();
            if (agentUrl != null) {
                JMXServiceURL serviceUrl = new JMXServiceURL(agentUrl);
                return JMXConnectorFactory.connect(serviceUrl, null);
            }
        } catch (AttachNotSupportedException e) {
            CorePlugin.log(e);
        } catch (IOException e) {
            CorePlugin.log(e);
        }
    }
    return null;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) IOException(java.io.IOException) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException)

Example 24 with VirtualMachineDescriptor

use of com.sun.tools.attach.VirtualMachineDescriptor in project oozie by apache.

the class TestMetricsInstrumentation method testJMXInstrumentation.

public void testJMXInstrumentation() throws Exception {
    final AttachProvider attachProvider = AttachProvider.providers().get(0);
    VirtualMachineDescriptor descriptor = null;
    // Setting the id of the VM unique, so we can find it.
    String uniqueId = UUID.randomUUID().toString();
    System.setProperty("processSettings.unique.id", uniqueId);
    // Finding our own VM by the id.
    for (VirtualMachineDescriptor d : VirtualMachine.list()) {
        String remoteUniqueId = VirtualMachine.attach(d).getSystemProperties().getProperty("processSettings.unique.id");
        if (remoteUniqueId != null && remoteUniqueId.equals(uniqueId)) {
            descriptor = d;
            break;
        }
    }
    assertNotNull("Could not find own virtual machine", descriptor);
    // Attaching JMX agent to our own VM
    final VirtualMachine virtualMachine = attachProvider.attachVirtualMachine(descriptor);
    String agent = virtualMachine.getSystemProperties().getProperty("java.home") + File.separator + "lib" + File.separator + "management-agent.jar";
    virtualMachine.loadAgent(agent);
    final Object portObject = virtualMachine.getAgentProperties().get("com.sun.management.jmxremote.localConnectorAddress");
    final JMXServiceURL target = new JMXServiceURL(portObject + "");
    JMXConnector jmxc = JMXConnectorFactory.connect(target);
    MBeanServerConnection conn = jmxc.getMBeanServerConnection();
    // Query a value through JMX from our own VM
    Object value = null;
    try {
        value = conn.getAttribute(new ObjectName("metrics:name=jvm.memory.heap.committed"), "Value");
    } catch (Exception e) {
        fail("Could not fetch metric");
    }
    assertNotNull("JMX service error", value);
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) AttachProvider(com.sun.tools.attach.spi.AttachProvider) MBeanServerConnection(javax.management.MBeanServerConnection) VirtualMachine(com.sun.tools.attach.VirtualMachine) ObjectName(javax.management.ObjectName)

Example 25 with VirtualMachineDescriptor

use of com.sun.tools.attach.VirtualMachineDescriptor in project openj9 by eclipse.

the class TestAttachAPI method test_vmlist01.

@Test
public void test_vmlist01() {
    logger.debug("starting " + testName);
    final int NUM_TARGETS = 4;
    TargetManager[] targets = new TargetManager[NUM_TARGETS];
    String[] vmIds = new String[NUM_TARGETS];
    String[] vmNames = new String[NUM_TARGETS];
    boolean testComplete = false;
    try {
        for (int i = 0; i < NUM_TARGETS; ++i) {
            vmNames[i] = "my_" + testName + i;
            vmIds[i] = testName + i;
            targets[i] = new TargetManager(TestConstants.TARGET_VM_CLASS, vmIds[i], vmNames[i], null, null);
            logger.debug("vmIds[i] = " + vmIds[i] + ", vmNames[i]=" + vmNames[i]);
            targets[i].syncWithTarget();
        }
        List<VirtualMachineDescriptor> vmdList = VirtualMachine.list();
        for (int i = 0; i < NUM_TARGETS; ++i) {
            boolean found = false;
            logger.debug("looking for id = " + vmIds[i] + ", name=" + vmNames[i]);
            Iterator<VirtualMachineDescriptor> di = vmdList.iterator();
            while (di.hasNext()) {
                VirtualMachineDescriptor d = di.next();
                logger.debug("checking id = " + d.id() + ", name=" + d.displayName());
                if (d.id().equals(vmIds[i]) && d.displayName().equals(vmNames[i])) {
                    found = true;
                    break;
                }
            }
            assertTrue("VirtualMachine.list contains " + vmIds[i] + " " + vmNames[i], found);
            testComplete = true;
        }
    } catch (Exception e) {
        logger.warn(testName + " exception");
        logStackTrace(e);
    } finally {
        assertTrue(testName + " incomplete", testComplete);
        for (int i = 0; i < NUM_TARGETS; ++i) {
            targets[i].terminateTarget();
        }
    }
}
Also used : VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) AgentInitializationException(com.sun.tools.attach.AgentInitializationException) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException) AgentLoadException(com.sun.tools.attach.AgentLoadException) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Aggregations

VirtualMachineDescriptor (com.sun.tools.attach.VirtualMachineDescriptor)26 IOException (java.io.IOException)16 AttachNotSupportedException (com.sun.tools.attach.AttachNotSupportedException)13 AttachProvider (com.sun.tools.attach.spi.AttachProvider)12 VirtualMachine (com.sun.tools.attach.VirtualMachine)11 Test (org.testng.annotations.Test)7 ArrayList (java.util.ArrayList)5 Msg.getString (com.ibm.oti.util.Msg.getString)2 AgentLoadException (com.sun.tools.attach.AgentLoadException)2 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)2 JMXServiceURL (javax.management.remote.JMXServiceURL)2 MonitorException (sun.jvmstat.monitor.MonitorException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 FileLock (com.ibm.tools.attach.target.FileLock)1 Reply (com.ibm.tools.attach.target.Reply)1 AgentInitializationException (com.sun.tools.attach.AgentInitializationException)1 AttachOperationFailedException (com.sun.tools.attach.AttachOperationFailedException)1 CommonContext (io.aeron.CommonContext)1 AeronCluster (io.aeron.cluster.client.AeronCluster)1