微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 从JVM内部访问JMX

是否可以从该JVM实例内部访问JVM的JMX服务器?或者我是否必须通过标准的套接字/端口远程接口连接?
+----------------------------------------+   Option 2: Connect
|       +---------------------------+    |   through sockets like
|       | My Notification Listener  |+----->----------+ a remote
|       |                           |    |            | monitor.
|       +---------------------------+    |            |
|                           +            |            |
|          Option 1: connect|            |            |
|          to the internal  |            |            |
|          JMX server. I'm  |            |            |
|          trying to find   |            |            |
|          if this is possible.          |            |
|                           |            |            |
|                           |            |            |
|    A single JVM instance. |            |            |
|                           |            |            |
|        +------------+-----v------+--+  |            |
|        |            | GuageMXBean|<-+<--------------+
|        |            +------------+  |  |
|        | JMX MXBean Server          |  |
|        +----------------------------+  |
+----------------------------------------+

上下文:我正在尝试实现一个响应JVM状态的“智能”系统,特别是内存使用,在将工作数据缓存到磁盘之间转换,并将其保存在RAM中.设置JMX监听器似乎比运行后台线程更优雅,后台线程执行以下操作:

Runtime RTime = Runtime.getRuntime();
while(!shutdown)
{
    if((RTime.totalMemory / RTime.maxMemory) > upperThreshold) cachmode = CACHetodisK;
    if((RTime.totalMemory / RTime.maxMemory) < lowerThreshold) cachmode = CACHetoRAM;
    Sleep(1000);
}

桌面应用程序,如果它有所作为.

这是我的第一篇SO帖子,所以欢迎任何有关改进等问题的提示.

解决方法

您可以使用此代码轻松获取平台MBean Server.
ManagementFactory.getPlatformMBeanServer();

有许多有用的MBean用于收集有关JVM状态的信息.这是一个简单的例子

List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); 

// generate heap state report 
String report = "";     
for (GarbageCollectorMXBean gc : gcBeans) {
    report += "\nGC Name         : " + gc.getName();
    report += "\nCollection count: " + gc.getCollectionCount();
    report += "\nCollection Time : " + gc.getCollectionTime() + " milli seconds";
    report += "\n";
}       

List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean pool : memoryPoolMXBeans) {
    report += "\nMemory Pool: " + pool.getName();
    MemoryUsage usage = pool.getUsage();
    report += "\n   Max : " + usage.getMax() / 1024000 + "MB"; 
    report += "\n   Used: " + usage.getUsed() / 1024000 + "MB";
    report += "\n";
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐