如果在启动JVM时设置“com.sun.management.jmxremote”系统属性,则可以运行jconsole或visualvm并连接到该本地mBeanServer。我想做他们正在做的同样的事情,但无法弄清楚如何。
是否有可用于识别本地运行的JVM的服务URL?
我知道我可以通过在特定端口上设置jmxmp或rmi侦听器然后连接到该端口来实现此目的,但我不想这样做,因为这意味着我必须管理端口并知道哪个端口转到哪个jvm(我们在同一台服务器上运行多个jvms)。
答案 0 :(得分:7)
感谢Nicholas和Scott的发布和讨论。能够根据该信息和一些反复试验将其拼凑在一起。
分享,以便其他人不必做同样的腿部工作。这将附加到JVM并获取其内存使用情况。
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
import com.sun.tools.attach.spi.AttachProvider;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
public class AttachFun {
public static void main(String[] args) throws Exception {
final AttachProvider attachProvider = AttachProvider.providers().get(0);
VirtualMachineDescriptor descriptor = null;
for (VirtualMachineDescriptor virtualMachineDescriptor : attachProvider.listVirtualMachines()) {
if (pickThisOne(virtualMachineDescriptor)) {
descriptor = virtualMachineDescriptor;
break;
}
}
if (descriptor == null) throw new RuntimeException("You didn't pick one");
final VirtualMachine virtualMachine = attachProvider.attachVirtualMachine(descriptor);
virtualMachine.loadAgent("/System/Library/Frameworks/JavaVM.framework/Home/lib/management-agent.jar", "com.sun.management.jmxremote");
final Object portObject = virtualMachine.getAgentProperties().get("com.sun.management.jmxremote.localConnectorAddress");
final JMXServiceURL target = new JMXServiceURL(portObject + "");
final JMXConnector connector = JMXConnectorFactory.connect(target);
final MBeanServerConnection remote = connector.getMBeanServerConnection();
final ObjectName memory = new ObjectName("java.lang:type=Memory");
CompositeData cd = (CompositeData) remote.getAttribute(memory, "HeapMemoryUsage");
final Long used = (Long) cd.get("used");
System.out.println(used);
}
private static boolean pickThisOne(VirtualMachineDescriptor virtualMachineDescriptor) {
// TODO
return false;
}
}
答案 1 :(得分:1)
您需要Attach API。它将在同一主机上找到运行[Attach API-capable] JVM并返回每个JVM上的元数据以及启用JMXConnector。它基本上是您在启动JConsole时看到的,并且connect菜单显示正在运行的JVM,包括未使用任何特殊JMX命令行指令启动的Java 1.6+实例。
//尼古拉斯