title: Access JMX from inside JVM
body: <p>Is it possible to access a JVM's JMX server from <em>inside</em> that JVM instance? Or would I have to connect through the standard socket/port remote interface?</p>
  
  <pre><code>+----------------------------------------+   Option 2: Connect
  |       +---------------------------+    |   through sockets like
  |       | My Notification Listener  |+-----&gt;----------+ 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|&lt;-+&lt;--------------+
  |        |            +------------+  |  |
  |        | JMX MXBean Server          |  |
  |        +----------------------------+  |
  +----------------------------------------+
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>Runtime RTime = Runtime.getRuntime();
  while(!shutdown)
  {
      if((RTime.totalMemory / RTime.maxMemory) &gt; upperThreshold) cachmode = CACHETODISK;
      if((RTime.totalMemory / RTime.maxMemory) &lt; lowerThreshold) cachmode = CACHETORAM;
      Sleep(1000);
  }
  </code></pre>
  
  <p>Desktop application, if it makes a difference.</p>
  
  <p>This is my first SO post, so any hints on improvement, etc. of the question are welcome.</p>