如何通过了解进程ID连接到本地JMX服务器? [英] How to connect to a local JMX server by knowing the process id?
问题描述
这背后的动机是使用JMX来管理本地java服务,而不像java服务Wrapper那样更重要。
The motivation behind this is to manage local java services, using JMX, without something more heavyweight like the java service Wrapper.
每个服务都以<$ c开头$ c> -Dcom.sun.management.jmxremote 这意味着JVM配置为作为本地(仅限同一台机器)的JMX服务器。 (有关商品,请参见此处解释)。
Each service is started with -Dcom.sun.management.jmxremote
which means that "The JVM is configured to work as a local (same-machine-only) JMX server." (see here for a good explaination).
我试过附加API ,但决定反对它,因为它没有与Java SE6捆绑在一起,并且无法将其与maven集成。
I tried the Attach API, but decided against it since it is not bundled with Java SE6 and and integrating it with maven was not possible.
推荐答案
@TimBüthe
如果 ConnectorAddressLink.importFrom
返回null,尝试将management-agent.jar加载到VM中。
If ConnectorAddressLink.importFrom
returns null, try loading management-agent.jar into VM.
例如,函数 startManagementAgent
来自< a href =https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/test/sun/management/jmxremote/bootstrap/TestManager.java =nofollow> https:// github。 com / openjdk-mirror / jdk7u-jdk / blob / master / test / sun / management / jmxremote / bootstrap / TestManager.java
private static void startManagementAgent(String pid) throws IOException {
/*
* JAR file normally in ${java.home}/jre/lib but may be in ${java.home}/lib
* with development/non-images builds
*/
String home = System.getProperty("java.home");
String agent = home + File.separator + "jre" + File.separator + "lib"
+ File.separator + "management-agent.jar";
File f = new File(agent);
if (!f.exists()) {
agent = home + File.separator + "lib" + File.separator +
"management-agent.jar";
f = new File(agent);
if (!f.exists()) {
throw new RuntimeException("management-agent.jar missing");
}
}
agent = f.getCanonicalPath();
System.out.println("Loading " + agent + " into target VM ...");
try {
VirtualMachine.attach(pid).loadAgent(agent);
} catch (Exception x) {
throw new IOException(x.getMessage());
}
}
这篇关于如何通过了解进程ID连接到本地JMX服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!