@Override public boolean isProcessAlive(final int pid) { isTrue(pid > 0, "Invalid pid '" + pid + "' specified"); for (VirtualMachineDescriptor vm : VirtualMachine.list()) { if (vm.id().equals(String.valueOf(pid))) { return true; // found the vm } } return false; }
private static void getAttachableVMs(Map<Integer, LocalVirtualMachine> map, Map<Integer, LocalVirtualMachine> existingVmMap) List<VirtualMachineDescriptor> vms = VirtualMachine.list(); for (VirtualMachineDescriptor vmd : vms)
@Override public List<VMDescriptorDTO> listLocalJVMs() { List<VMDescriptorDTO> rc = new ArrayList<>(); try { List<VirtualMachineDescriptor> processes = VirtualMachine.list(); for (VirtualMachineDescriptor process : processes) { VMDescriptorDTO dto = new VMDescriptorDTO(process); dto.setAgentUrl(agentStatus(dto.getId())); String alias = getVmAlias(process.displayName(), dto.getAgentUrl()); dto.setAlias(alias); // provide fine grained url details if (dto.getAgentUrl() != null) { try { URL url = new URL(dto.getAgentUrl()); dto.setScheme(url.getProtocol()); dto.setHostname(url.getHost()); dto.setPort(url.getPort()); dto.setPath(url.getPath()); } catch (Exception e) { // ignore } } rc.add(dto); } } catch (Exception e) { LOG.warn("Failed to get local JVM processes due to: {}", e.getMessage()); throw new RuntimeException("Failed to get local JVM processes due to: " + e.getMessage(), e); } return rc; }
private synchronized List<VirtualMachineDescriptor> getVmList() { if (vmList == null || vmListEndOfLife < System.nanoTime()) { vmList = VirtualMachine.list(); vmListEndOfLife = System.nanoTime() + VM_LIST_EXPIRY; } return vmList; }
@Override public boolean isProcessAlive(final int pid) { for (VirtualMachineDescriptor vm : VirtualMachine.list()) { if (vm.id().equals(String.valueOf(pid))) { return true; // found the vm } } return false; }
@Override public boolean isProcessAlive(final int pid) { for (VirtualMachineDescriptor vm : VirtualMachine.list()) { if (vm.id().equals(String.valueOf(pid))) { return true; // found the vm } } return false; }
protected boolean isVmWithProcessIdRunning(final Integer pid) { for (final VirtualMachineDescriptor vm : VirtualMachine.list()) { if (String.valueOf(pid).equals(vm.id())) { return true; } } return false; }
protected boolean isPidAlive(int pid) { for (VirtualMachineDescriptor vm : VirtualMachine.list()) { if (vm.id().equals(String.valueOf(pid))) { return true; // found the vm } } return false; }
private static boolean pidExists(String pid) { for (VirtualMachineDescriptor vm : VirtualMachine.list()) { if (vm.id().equals(pid)) { return true; } } return false; } }
public static String list() { StringBuilder msg = new StringBuilder(); for (VirtualMachineDescriptor vm : VirtualMachine.list()) { msg.append(" ").append(vm.id()).append(" ").append(vm.displayName()).append("\n"); } return msg.toString(); }
private void refresh() { List<VirtualMachineDescriptor> currentVms = VirtualMachine.list(); ImmutableMap.Builder<VirtualMachineDescriptor, SpringBootApp> newAppsBuilder = ImmutableMap.builder(); for (VirtualMachineDescriptor vm : currentVms) { SpringBootApp existingApp = apps.get(vm); if (existingApp!=null) { newAppsBuilder.put(vm, existingApp); } else { try { LocalSpringBootApp localApp = MemoizingProxy.create(LocalSpringBootApp.class, Duration.ofMillis(4500), new Class[] {VirtualMachineDescriptor.class}, vm); newAppsBuilder.put(vm, localApp); } catch (Exception e) { //Ignore problems attaching to a VM. We will try again on next polling loop, if vm still exists. //The most likely cause is that the VM already died since we obtained a reference to it. } } } HashSet<VirtualMachineDescriptor> oldVms = new HashSet<>(apps.keySet()); ImmutableMap<VirtualMachineDescriptor, SpringBootApp> newApps = newAppsBuilder.build(); oldVms.removeAll(newApps.keySet()); for (VirtualMachineDescriptor oldVm : oldVms) { apps.get(oldVm).dispose(); } apps = newApps; nextRefreshAfter = System.currentTimeMillis() + EXPIRE_AFTER.toMillis(); } }
public static VMInfo[] availableVMs() { List<VirtualMachineDescriptor> vmds = VirtualMachine.list(); VMInfo[] vmInfo = new VMInfo[vmds.size()]; int i = 0; for (VirtualMachineDescriptor vmd : vmds) { vmInfo[i++] = new VMInfo(vmd.id(), vmd.displayName()); } return vmInfo; }
static String selectProcess() { System.out.println("Which java process to attache:"); final List<VirtualMachineDescriptor> list = VirtualMachine.list(); // remove current process for (Iterator<VirtualMachineDescriptor> iterator = list.iterator(); iterator.hasNext(); ) { VirtualMachineDescriptor vm = iterator.next(); if (vm.id().equals(pid())) iterator.remove(); } for (int i = 0; i < list.size(); i++) { final VirtualMachineDescriptor vm = list.get(i); System.out.printf("%d) %-5s %s\n", i + 1, vm.id(), vm.displayName()); } Scanner in = new Scanner(System.in); while (true) { System.out.print("?# "); final String select = in.nextLine(); try { final int idx = Integer.parseInt(select); if (idx > 0 && idx <= list.size()) { return list.get(idx - 1).id(); } System.out.println("Invalid selection!"); } catch (NumberFormatException e) { System.out.println("Invalid input, not number!"); } } }
private void refreashVMList() throws ExecutionException { List<VirtualMachineDescriptor> vmList = VirtualMachine.list(); for (VirtualMachineDescriptor vmd : vmList) { VirtualMachineWrapper wrapper = vmCache.get(vmd.id()); if (NullVirtualMachineWrapper.class.isAssignableFrom(wrapper.getClass())) { wrapper = new VirtualMachineWrapper(vmd); } // 为空不为空都put一次,以更新缓存时间 vmCache.put(vmd.id(), wrapper); } }
} catch (AttachNotSupportedException e) { VirtualMachineDescriptor match = null; for (VirtualMachineDescriptor desc : VirtualMachine.list()) { if (desc.displayName().contains("jkillthread")) { continue;
public static VirtualMachine getVirtualMachine() if (VirtualMachine.list().size() > 0)
private String getJmxUrlForProcessRegex(String processRegex) throws com.sun.tools.attach.AttachNotSupportedException, IOException { for (com.sun.tools.attach.VirtualMachineDescriptor vmd : com.sun.tools.attach.VirtualMachine.list()) { if (vmd.displayName().matches(processRegex)) { com.sun.tools.attach.VirtualMachine vm = com.sun.tools.attach.VirtualMachine.attach(vmd); String connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS); // If jmx agent is not running in VM, load it and return the connector url if (connectorAddress == null) { loadJmxAgent(vm); // agent is started, get the connector address connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS); } return connectorAddress; } } throw new IOException( "No match found. Available JVMs can be listed with the `list_jvms` command."); }
private static VirtualMachine attachToVirtualMachine(ProcessInfo process) throws AttachNotSupportedException, IOException { VirtualMachine vm = null; List<VirtualMachineDescriptor> vmDescriptors = VirtualMachine.list(); for (VirtualMachineDescriptor vmDescriptor : vmDescriptors) { if (Long.valueOf(vmDescriptor.id()) == process.getPid()) { String vmUserName = process.getCredentialsName().getUser(); String agentUserName = System.getProperty("user.name"); if (vmUserName.equals(agentUserName)) { LOG.debug("Attaching to JVM for java process with PID [" + process.getPid() + "]..."); vm = VirtualMachine.attach(vmDescriptor); LOG.debug("Attached to JVM [" + vm + "]."); } else { LOG.debug("Cannot attach to JVM for java process with PID [" + process.getPid() + "], because it is running as a different user (" + vmUserName + ") than the user the Agent is running as (" + agentUserName + ")."); } break; } } return vm; }
private void setJMXConnectionFactory() { List<VirtualMachineDescriptor> vms = VirtualMachine.list(); Boolean foundVM = false;
for(VirtualMachineDescriptor d : VirtualMachine.list()) { String remoteUniqueId = VirtualMachine.attach(d).getSystemProperties().getProperty("processSettings.unique.id"); if(remoteUniqueId != null && remoteUniqueId.equals(uniqueId))