我们共享一个远程 Linux 盒子来开发各种 Java 应用程序,并使用 VisualVM 通过 ssh 来分析应用程序如此处所述< /a>.有没有什么方法可以在我们的 Java 进程上启用 JMX/分析,而无需在进程/用户之间分配/提供端口号?必须始终确保指定一个(唯一的)端口号才能启用分析,这很烦人。
为了使这一切更加具体:对端口进行硬编码显然不起作用并且会发生冲突:
exec java -Dcom.sun.management.jmxremote.port=3000 ...
每当您运行进程时,我们都可以要求始终指定唯一的端口,但这很乏味——您必须确保您的端口不发生冲突与您的其他进程,也不与其他用户冲突:
exec java -Dcom.sun.management.jmxremote.port=$1 ...
目前我们使用:
exec java -Dcom.sun.management.jmxremote.port=$(( $RANDOM + 2000 )) ...
但我们仍然偶尔会遇到占用的端口号。
我们可以继续使用更高级的脚本(例如,查询netstat
以获取占用的端口号并希望没有竞争),但我们想知道是否有更好的方法/我们是否做错了。
We share a remote Linux box for developing various Java apps, and we use VisualVM over ssh to profile the apps as described here. Is there any way to enable JMX/profiling on our Java processes without needing to allocate/provision port numbers among our processes/users? It's annoying to have to always make sure you're specifying a (unique) port number just to enable profiling.
To make this all more concrete: hardcoding the port obviously doesn't work and will conflict:
exec java -Dcom.sun.management.jmxremote.port=3000 ...
We can require always specifying a unique port whenever you run a process, but this is tedious---you have to ensure your ports don't conflict with your other processes and also don't conflict with other users:
exec java -Dcom.sun.management.jmxremote.port=$1 ...
Currently we use:
exec java -Dcom.sun.management.jmxremote.port=$(( $RANDOM + 2000 )) ...
But we still occasionally bump into occupied port numbers.
We can continue with fancier scripts (e.g. querying netstat
for occupied port numbers and hoping there's no race), but we're wondering whether there's a better way / whether we're Doing It Wrong.
发布评论
评论(2)
我最好的猜测是,您应该弄清楚是否有一种方法可以在不使用
-Dcom.sun.management.jmxremote.port=3000 ...
但随后启动您自己的 MBeanServer 连接器http://www.docjar.com/html/api/sun/management/jmxremote/ConnectorBootstrap.java.html
是 sun jvm 似乎根据以下内容启动连接器的地方传入系统属性,我认为阅读那里的代码将提供足够的信息来启动您自己的 MBeanServerConnector
http://docs.oracle.com/javase /1.5.0/docs/api/javax/management/remote/JMXConnectorServerFactory.html
如果您从中读取代码,传递给 newJMXConnectorServer 的环境变量似乎只是一个 MAP在 JDK 中,您可能能够找到要在映射中放入的内容,以便它使用您自己的代码启动连接器,然后您可以编写自己的 java 代码来选择可用的端口号。我还没有尝试过这种方法,但它看起来很有希望,我真的很想得到你提出的问题的答案。
My best guess is that you should figure out if there is a way to start the VM without the
-Dcom.sun.management.jmxremote.port=3000 ...
but then start your own MBeanServer connectorshttp://www.docjar.com/html/api/sun/management/jmxremote/ConnectorBootstrap.java.html
is the place where sun jvm seems to be starting the connectors based on the passed in system properties, I think reading the code there will provide enough info to be able to start your own MBeanServerConnector
http://docs.oracle.com/javase/1.5.0/docs/api/javax/management/remote/JMXConnectorServerFactory.html
the environment variables passed to newJMXConnectorServer seems to be just a MAP if you read the code from the JDK you might be able to discover what to put in the map so that it starts a connector with your own code then that way you can write your own java code to pick available port numbers. I have not tried this approach but it seems promising and I am really want an answer to the question you raised.
一种可能的解决方案是在远程机器上运行 VisualVM,并使用 vnc 或远程 X 会话在本地计算机上显示 VisualVM。
One possible solution is to run VisualVM on remote box and use vnc or remote X session to display VisualVM on your local machine.