从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);
}

桌面应用程序,如果有所不同。

这是我的第一个帖子,因此欢迎有关问题的任何提示等。

您可以轻松地从使用此代码中获取平台MBEAN服务器。

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";
}

最新更新