java 如何以编程方式转储 JMX 数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2336012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I programmatically dump JMX data?
提问by Stefan Kendall
I want to be able to log all JMX data accessible via jconsole. Is there a way to do this programmatically? I'm building a form of logging of the system, and I want to create intervaled data viewable with some tool similar to jconsole.
我希望能够记录所有可通过 jconsole 访问的 JMX 数据。有没有办法以编程方式做到这一点?我正在构建一种系统日志记录形式,并且我想使用类似于 jconsole 的一些工具创建可查看的间隔数据。
How would I go about doing this?
我该怎么做呢?
回答by stacker
java.lang.management.ManagementFactorygives you access to JMX data.
java.lang.management.ManagementFactory允许您访问 JMX 数据。
i.g.
伊格
List<MemoryPoolMXBean> memPoolBeans = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean mpb : memPoolBeans) {
System.out.println("Memory Pool: " + mpb.getName());
}
Some samples are available at SO query: [java]+managementfactory
SO 查询中提供了一些示例:[java]+managementfactory
A good read: https://www.ibm.com/developerworks/library/j-jtp09196/index.html
一本好书:https: //www.ibm.com/developerworks/library/j-jtp09196/index.html
For full implementation connecting to a remote VM:
对于连接到远程 VM 的完整实现:
Map<String,String[]> env = new HashMap<String, String[]>();
env.put( JMXConnector.CREDENTIALS, new String[]{"user","pass"} );
JMXServiceURL address = new JMXServiceURL("service:rmi:///jndi/rmi://host:port/jmxrmi");
JMXConnector connector = JMXConnectorFactory.connect(address,env);
MBeanServerConnection mbs = connector.getMBeanServerConnection();
//get all mbeans
Set<ObjectInstance> beans = mbs.queryMBeans(null,null);
for( ObjectInstance instance : beans )
{
MBeanInfo info = mbs.getMBeanInfo( instance.getObjectName() );
}
From the info, you can query object names and attributes as desired.
从信息中,您可以根据需要查询对象名称和属性。
回答by Mark
I used this command line JMX clientas a starting point when I wanted to do the same thing.
当我想做同样的事情时,我使用这个命令行 JMX 客户端作为起点。

