简体   繁体   English

如何通过pid(Java 9+)在本地机器上连接到JMX for JVM

[英]How to connect to JMX for JVM on local machine by pid (Java 9+)

I want to query an MBean on a JVM running on the local machine.我想在本地机器上运行的 JVM 上查询 MBean。

String address = "???";
JMXConnectorFactory.connect(new JMXServiceURL(address))
  .getMBeanServerConnection()
  .getAttribute(new ObjectName("<object name>"), "<attribute name>");

I have not set up com.sun.management.jmxremote.port=XXX, but since the jconsole GUI is still able to connect to a JVM and observe/interact with the MBeans there (I've tested this), I assume it's possible to craft an address string that references a JVM process running on a particular pid (since no jmxremote.port=XXX will exist).我还没有设置 com.sun.management.jmxremote.port=XXX,但由于 jconsole GUI 仍然能够连接到 JVM 并在那里观察/与 MBean 交互(我已经测试过),我认为这是可能的制作引用在特定 pid 上运行的 JVM 进程的地址字符串(因为不存在 jmxremote.port=XXX)。

However, I can find no documentation on how to construct an address for a local JVM process with a specified pid.但是,我找不到有关如何为具有指定 pid 的本地 JVM 进程构造地址的文档。

There are some answers from 5+ years ago that use the sun.management.ConnectorAddressLink class, but this class has been removed in Java 9+. 5 年前有一些答案使用 sun.management.ConnectorAddressLink class,但是这个 class 已在 Java 9+ 中删除。 I've also tried looking through the source code of several projects such as jmxlocal , but all of these projects are now out of date, and also use that same ConnectorAddressLink class which is now unavailable.我也尝试过查看jmxlocal等几个项目的源代码,但所有这些项目现在都已过时,并且还使用相同的ConnectorAddressLink class现在不可用。

(I presume you are not inquiring about the format of the string, just about how to find the port.) (我想您不是在询问字符串的格式,而是在询问如何找到端口。)

You could agree on some contract for jmx port(s).您可以就 jmx 端口的一些合同达成一致。 If you were to have 2+ jvms on the localhost, they obviously would have distinct ports so the jmx client can't just guess nor port scan (bad etiquette).如果您在本地主机上有 2 个以上的 jvm,它们显然会有不同的端口,因此 jmx 客户端不能只是猜测或端口扫描(不良礼仪)。 Maybe a port range to blindly try to connect to.也许是盲目尝试连接的端口范围。

You could rely on something else, like a broadcast packet, or multicast channel convention to request your programs to reveal they exist on some jmx port, but that you would have to program yourself and expect the jvms to start such module.您可以依靠其他东西,例如广播数据包或多播频道约定来请求您的程序显示它们存在于某个 jmx 端口上,但是您必须自己编程并期望 jvm 启动此类模块。

Otherwise, on linux by browsing /proc/... you might discover processes (filtering only java ones) and which port they have opened, or some runtime exec backflips with netstat (linux or windows) you might be able to list listening sockets.否则,在 linux 上通过浏览 /proc/... 您可能会发现进程(仅过滤 java 的)以及它们打开的端口,或者使用 netstat(linux 或 windows)的某些运行时 exec backflips 您可能能够列出侦听套接字。 But again, that doesn't mean these sockets are JMX service - you still need a convention unless you want to probe and hassle the processes arbitrary ports.但同样,这并不意味着这些套接字是 JMX 服务 - 您仍然需要一个约定,除非您想探测和麻烦进程的任意端口。

However, if you can list processes, you probably can obtain their command lines and find java ones, and their own jmx system properties revealing the port they use...但是,如果您可以列出进程,您可能可以获得它们的命令行并找到 java 的,以及它们自己的 jmx 系统属性,揭示它们使用的端口......

Few things to try.很少有事情可以尝试。 Hope this helps.希望这可以帮助。

full sample source code: https://github.com/banv-dev/DemoDumpHeap/blob/master/src/main/java/org/example/Main.java完整示例源代码: https://github.com/banv-dev/DemoDumpHeap/blob/master/src/main/java/org/example/Main.java

//attach to VM with pid
VirtualMachine vm = VirtualMachine.attach("pid");
//Starts the local JMX management agent in the target virtual machine.
String jmxUrl = vm.startLocalManagementAgent()

//connect via JMX
JMXServiceURL url = new JMXServiceURL(jmxUrl);
JMXConnector connector = JMXConnectorFactory.connect(url);
MBeanServerConnection beanServerConnection = connector.getMBeanServerConnection();

reference: https://docs.oracle.com/en/java/javase/11/docs/api/jdk.attach/com/sun/tools/attach/VirtualMachine.html参考: https://docs.oracle.com/en/java/javase/11/docs/api/jdk.attach/com/sun/tools/attach/VirtualMachine.ZFC35FDC70D5FCC69D236E

https://docs.oracle.com/en/java/javase/11/management/monitoring-and-management-using-jmx-technology.html#GUID-805517EC-2D33-4D61-81D8-4D0FA770D1B8 https://docs.oracle.com/en/java/javase/11/management/monitoring-and-management-using-jmx-technology.html#GUID-805517EC-2D33-4D601-81BFAD8-4D601-81B8Z77

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM