Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access JMX from inside JVM

Tags:

java

jvm

jmx

Is it possible to access a JVM's JMX server from inside that JVM instance? Or would I have to connect through the standard socket/port remote interface?

+----------------------------------------+   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          |  |
|        +----------------------------+  |
+----------------------------------------+

Context: I'm trying to implement an 'intelligent' system that responds to JVM state, specifically memory usage, shifting between caching working data to disk, and holding it in ram. Setting up a JMX listener seemed more elegant than running a background thread that does something like:

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

Desktop application, if it makes a difference.

This is my first SO post, so any hints on improvement, etc. of the question are welcome.

like image 456
SailsMan63 Avatar asked Dec 22 '12 03:12

SailsMan63


People also ask

How do I access JMX?

When the JBoss Server is running, you can get a live view of the server by going to the JMX console application at http://localhost:8080/jmx-console.

How do I enable JMX in Java application?

A common way to enable local JMX access on these JVMs is to include the -Dcom. sun. management. jmxremote option on the command line when you start the JVM.


1 Answers

You can easily get the platform MBean Server from using this code.

ManagementFactory.getPlatformMBeanServer();

There are lot of useful MBeans for collecting information about the state of the JVM. here is a simple example

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";
}
like image 53
ams Avatar answered Oct 30 '22 01:10

ams