java – 如何连接到另一个本地进程中的mBeanServer?

如果在启动JVM时设置“com.sun.management.jmxremote”系统属性,则可以运行jconsole或visualvm并连接到该本地mBeanServer.我想做他们正在做的同样的事情,但无法弄清楚如何做.

是否有可用于标识本地运行的JVM的服务URL?

我知道我可以通过在特定端口上设置jmxmp或rmi侦听器然后连接到该端口来实现此目的,但我不想这样做,因为这意味着我必须管理端口并知道哪个端口到哪个端口jvm(我们在同一台服务器上运行多个jvms).

解决方法:

感谢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;
    }
}
上一篇:在Java 9中以编程方式连接到JMX代理


下一篇:【Jmeter Linux环境下运行方法】