如何使用 JMX 连接到 localhost jvm 上的 java 程序?
我应该使用 JMX 连接到 localhost jvm 上的 java 程序。换句话说,我想开发一个 JMX 客户端来在本地主机上配置 java 程序。
不推荐使用JConsole! JConsole 不适合,因为它是通用 JMX 客户端,对主程序性能有负面影响。
oracle 站点上的示例使用 RMIConnector 和主机:端口参数,但我不知道: 应该在哪里设置 jmx 端口?
JConsole 可以选择通过 PID 连接到 java 进程。但我在 JMX api 中没有找到任何将 PID 作为输入参数的方法。
I should connect to a java program on localhost jvm using JMX. In other words I want to develop a JMX client to config a java program on localhost.
Don't recommend using JConsole! JConsole is not suitable because it is general JMX client and have negative effect on main program performance.
Samples on oracle site use RMIConnector and host:port params but I don't know:
where should set jmx port?JConsole have an option to connect to java processes by PID. But I don't find any method in JMX api that have PID as input param.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我们使用类似以下内容以编程方式连接到我们的 JMX 服务器。您应该使用类似以下参数的内容来运行您的服务器:
要绑定到特定地址,您需要添加以下 VM 参数:
然后您可以使用如下所示的 JMX 客户端代码连接到您的服务器:
我们还有可以执行以下操作的代码:以编程方式将自身发布到虚拟机参数之外的特定端口,但这比您需要的更有趣,我认为。
据我所知,就“通过 pid”连接而言,您需要使用 Java6 从 Java 领域进行操作。我没有使用过以下代码,但它似乎有效。
我也是 SimpleJMX 包 的作者,它可以轻松启动 JMX 服务器并将 beans 发布到远程客户端。
它也有一个客户端接口,但现在它没有任何通过 PID 查找进程的机制——仅支持主机/端口组合(6/2012)。
We use something like the following to programatically connect to our JMX servers. You should run your server with something like the following arguments:
To bind to a particular address you'll need to add the following VM arguments:
Then you can connect to your server using JMX client code like the following:
We also have code that can programatically publish itself to a particular port outside of the VM arguments but that's more fu than you need I think.
In terms of connecting "by pid", you need to be using Java6 to do it from Java land as far as I know. I've not used the following code but it seems to work.
I've also the author of SimpleJMX package which makes it easy to start a JMX server and publish beans to remote clients.
It does have a client interface as well but right now it doesn't have any mechanisms to find processes by PID -- only host/port combinations are supported (in 6/2012).
需要澄清的是,如果您只对获取本地 JMX 统计信息感兴趣,则无需使用远程 api。只需使用 java.lang.management.ManagementFactory 即可:
To clarify, if you are only interested in getting local JMX stats, you don't need to use the remote api. Just use
java.lang.management.ManagementFactory
:这要求您在 JVM 启动时为您尝试读取的进程使用 jmxremote 参数。
无需在启动时传递 jmxremote 参数就能够做到这一点。您必须使用 Attach api(仅适用于使用 Java 6 及更高版本的程序。
This requires you to use the jmxremote argument at JVM startup for the process you are trying to read.
TO be able to do it without having to pass a jmxremote argument at startup. You will have to use the attach api(only applicable for Programs using Java 6 and higher.
最简单的意思是:
Simplest means:
这是如何通过 Java 程序的 PID 获得 JMX 连接(仅适用于版本 <= Java 8):
This is how you can get a JMX connection to a Java Program with it's PID (for version <= Java 8 only) :