我正在寻找示例Java JMX代码来访问另一个VM的JMX属性的值。如何通过JMX以编程方式访问内存使用情况?
随着JConsole的,我没有问题,看的java.lang /内存/属性/ HeapMemory
我怎么会得到在虚拟机上运行的Java程序相同的信息?
需要任何命令行选项或其他需要启动的东西的示例。
我正在寻找示例Java JMX代码来访问另一个VM的JMX属性的值。如何通过JMX以编程方式访问内存使用情况?
随着JConsole的,我没有问题,看的java.lang /内存/属性/ HeapMemory
我怎么会得到在虚拟机上运行的Java程序相同的信息?
需要任何命令行选项或其他需要启动的东西的示例。
您需要设置一个JMXConnector。这里是一个代码片段,它将获得远程机器上已提交的堆内存使用情况。
String host ="myHost";
int port = 1234;
HashMap map = new HashMap();
String[] credentials = new String[2];
credentials[0] = user;
credentials[1] = password;
map.put("jmx.remote.credentials", credentials);
JMXConnector c = JMXConnectorFactory.newJMXConnector(createConnectionURL(host, port), map);
c.connect();
Object o = c.getMBeanServerConnection().getAttribute(new ObjectName("java.lang:type=Memory"), "HeapMemoryUsage");
CompositeData cd = (CompositeData) o;
System.out.println(cd.get("committed"));
private static JMXServiceURL createConnectionURL(String host, int port) throws MalformedURLException
{
return new JMXServiceURL("rmi", "", 0, "/jndi/rmi://" + host + ":" + port + "/jmxrmi");
}
如果您不关心安全性,可以将映射设置为空。您需要启动远程服务器;
-Dcom.sun.management.jmxremote.port=1234
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
你可能想看看wlshell这是一个小工具,允许您访问使用文本界面或脚本的远程服务器上的MBean,它可以与WebLogic一起使用,但它工作任何启用了远程监控的Java程序。
// Retrieve memory managed bean from management factory.
MemoryMXBean memBean = ManagementFactory.getMemoryMXBean() ;
MemoryUsage heap = memBean.getHeapMemoryUsage();
MemoryUsage nonHeap = memBean.getNonHeapMemoryUsage();
// Retrieve the four values stored within MemoryUsage:
// init: Amount of memory in bytes that the JVM initially requests from the OS.
// used: Amount of memory used.
// committed: Amount of memory that is committed for the JVM to use.
// max: Maximum amount of memory that can be used for memory management.
System.err.println(String.format("Heap: Init: %d, Used: %d, Committed: %d, Max.: %d",
heap.getInit(), heap.getUsed(), heap.getCommitted(), heap.getMax()));
System.err.println(String.format("Non-Heap: Init: %d, Used: %d, Committed: %d, Max.: %d",
nonHeap.getInit(), nonHeap.getUsed(), nonHeap.getCommitted(), nonHeap.getMax()));
除非我很困惑,这是访问关于同一虚拟机的信息的代码。 我想弄清楚的是如何编写一个小命令行脚本,它将从不同虚拟机中的正在运行的服务器获取信息。 – ThoughtfulHacking 2009-11-19 19:41:43
@ThoughtfulHacking你说得对,这段代码是针对同一个虚拟机,我试图在其他虚拟机上做同样的代码,在ManagementFactory中有一个名为'newPlatformMXBeanProxy'的方法,我用它来获取另一个虚拟机的线程,但是我无法做到这一点来获得内存管理。你的问题的正确答案是解决方案,但我真的需要这样做,就像这个答案... – Tarek 2015-05-14 06:44:52
@基尔的答案看起来不错,但我想我会添加一些关于我的SimpleJMX package的细节。它包含允许您轻松导出Bean的服务器支持,并且还包含一个简单的客户端界面,可用于任何导出JMX信息的JVM。
访问内存使用情况,您会做这样的事情:
JmxClient client = new JmxClient("some.host.name", somePortNumber);
// get the memory composite information
CompositeData composite =
(CompositeData)client.getAttribute(new ObjectName("java.lang:type=Memory"),
"HeapMemoryUsage");
System.out.println(composite.get("committed"));
这是你如何让设置MemoryMXBean远程(补充@亚当斯基的回答):
MemoryMXBean memoryMXBeanProxy = JMX.newMXBeanProxy(
conn, new ObjectName("java.lang:type=Memory"), MemoryMXBean.class);
这个解决方案比@Kire更好,因为不需要使用强制转换。 – 2017-11-28 11:24:45
我们如何知道用户名和密码,或者我们如何设置它。通过使用属性文件? – 2016-02-11 12:10:39
'MemoryUsage memuse = MemoryUsage.from((CompositeData)o);' – 2017-03-28 09:48:50