Being spent some part of my past life developing dashboard tools for monitoring the applications in a large enterprise, I am always interested in exposing system health and metrics to external tools for monitoring. So, naturally when Java 1.5 exposed JMX beans for JVM, I was very excited. However, in my current work, we never exposed health of the system through JMX, we had our custom ways to do things and custom monitors for it.
With the latest release coming up, now that we are finally using 1.6 jre, I enabled the in-built support for JMX using these instructions. I did not think much about how JMX client is going to connect to the VM, until today my fellow developer asked, "Ramesh what is the URL for the JMX port on the VM?"
Well, I did not knew the answer, I always used either JConsole or VisualVM to connect and did not pay attention. Looking through the instructions, I see that one could expose the JMX port, so that you can connect remotely from another machine or local machine, however I did not find any default URL I could find to connect to locally to a JVM's JMX port. I did not wanted open a specific port, because once I do that, I would need to provide security mechanism tie it down, more over the agent that is polling for the stats is supposed to live on same box.
To handle the security, I could expose the port on the VM and setup a private SSL certificate between local agent and Server, but I knew some how JConsole connects to the local processes, so started investigate how it does it, then I came across "Attach API"
Attch API, is used by debuggers and profilers, to connect to a VM and inject agents into the VM and expose inner workings of the VM. Here, I just needed to find the URL for local JMX connection, then I came across this blog, which showed how to use the Attcah API to find the JMX URL, now we can dynamically resolve the JMX URL to the local VM. Problem Solved!