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);
}
}
}
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);
}
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;
}
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);
}
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();
}
}
}
Aggregations