如何使用JMX连接到localhost jvm上的java程序? [英] How to connect to a java program on localhost jvm using JMX?

查看:269
本文介绍了如何使用JMX连接到localhost jvm上的java程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用JMX连接到localhost jvm上的java程序。换句话说,我想开发一个JMX客户端来配置localhost上的java程序。

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.


  • 不建议使用JConsole! JConsole不适合,因为它是一般的JMX客户端,对主程序性能有负面影响。

  • Don't recommend using JConsole! JConsole is not suitable because it is general JMX client and have negative effect on main program performance.

oracle站点上的示例使用RMIConnector和host:port params但是我不知道:
应该在哪里设置jmx端口?

Samples on oracle site use RMIConnector and host:port params but I don't know: where should set jmx port?

JConsole有一个选项可以通过PID连接到java进程。但我在JMX api中找不到任何PID作为输入参数的方法。

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.

推荐答案

我们使用以下内容以编程方式连接到我们的JMX服务器。您应该使用以下参数运行服务器:

We use something like the following to programatically connect to our JMX servers. You should run your server with something like the following arguments:

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.port=1234
-Dcom.sun.management.jmxremote.ssl=false

要绑定到特定地址,您需要添加以下VM参数:

To bind to a particular address you'll need to add the following VM arguments:

-Djava.rmi.server.hostname=A.B.C.D

然后,您可以使用以下JMX客户端代码连接到您的服务器:

Then you can connect to your server using JMX client code like the following:

String host = "localhost";  // or some A.B.C.D
int port = 1234;
String url = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi";
JMXServiceURL serviceUrl = new JMXServiceURL(url);
JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceUrl, null);
try {
   MBeanServerConnection mbeanConn = jmxConnector.getMBeanServerConnection();
   // now query to get the beans or whatever
   Set<ObjectName> beanSet = mbeanConn.queryNames(null, null);
   ...
} finally {
   jmxConnector.close();
}

我们还有代码可以编程方式将自己发布到外部的特定端口VM参数,但这比我想象的要多。

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.

在连接by pid方面,你需要据我所知,从Java土地使用Java6。我没有使用下面的代码,但似乎有效。

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.

List<VirtualMachineDescriptor> vms = VirtualMachine.list();
for (VirtualMachineDescriptor desc : vms) {
    VirtualMachine vm;
    try {
        vm = VirtualMachine.attach(desc);
    } catch (AttachNotSupportedException e) {
        continue;
    }
    Properties props = vm.getAgentProperties();
    String connectorAddress =
        props.getProperty("com.sun.management.jmxremote.localConnectorAddress");
    if (connectorAddress == null) {
        continue;
    }
    JMXServiceURL url = new JMXServiceURL(connectorAddress);
    JMXConnector connector = JMXConnectorFactory.connect(url);
    try {
        MBeanServerConnection mbeanConn = connector.getMBeanServerConnection();
        Set<ObjectName> beanSet = mbeanConn.queryNames(null, null);
        ...
    } finally {
        jmxConnector.close();
    }
}






我还是 SimpleJMX软件包的作者,它可以轻松启动JMX服务器并将bean发布到远程客户端。


I've also the author of SimpleJMX package which makes it easy to start a JMX server and publish beans to remote clients.

// create a new server listening on port 8000
JmxServer jmxServer = new JmxServer(8000);
// start our server
jmxServer.start();
// register our lookupCache object defined below
jmxServer.register(lookupCache);
jmxServer.register(someOtherObject);
// stop our server
jmxServer.stop();

它确实有一个客户端界面,但现在它没有任何查找进程的机制通过PID - 仅支持主机/端口组合(2012年6月)。

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连接到localhost jvm上的java程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆