JMX provides Java developers with a powerful set of tools for monitoring and managing their applications. Any developer who has had to dig into the memory usage of their web server or diagnose database connections will have dusted off their copy of jconsole and Googled the default JMX port to try and see what is going on.
But having to connect to JMX over a custom port and protocol can be cumbersome in the real world. I suspect many of those developers who have tried to debug their production servers with JMX quickly found out that corporate firewalls prevented accesses, and the ops guys had blank stares when asked to open up JMX ports. Today everything is done via HTTP and RESTful interfaces, so wouldn’t it be great if you could access JMX via HTTP?
This is where Jolokia comes in. Jolokia is a JMX-HTTP bridge, exposing the power of JMX via a standard RESTful interface that is much easier to consume.
To demonstrate how Jolokia makes JMX easy, I will show you how to quickly combine two JBoss projects: WildFly Swarm and Hawtio.
WildFly Swarm is a standalone version of the popular WildFly Java EE server that provides a number of useful additions above the Java EE spec. Jolokia is one such extension. To build a WildFly Swarm package that includes Jolokia, all you need to do is visit the Swarm Generator, select Jolokia from the list of dependencies, download and extract the generated ZIP file, and build and run it with the Maven command mvn wildfly-swarm:run.
You can confirm that Jolokia is running but opening http://localhost:8080/jolokia in a browser.
The JSON response means that WildFly Swarm is up and running, and that the Jolokia dependency is exposing JMX over HTTP. Which is great, but this JSON is pretty hard to read in its raw form. This is where Hawtio comes in.
To get a copy of Hawtio running, download the standalone JAR file from the Hawtio website and run it with the command java -jar hawtio-app-1.4.66.jar --port 8090. Once Hawtio is started, you can access the dashboard via http://localhost:8090/hawtio.
At this point, you can connect Hawtio to the instance of Jolokia hosted by WildFly Swarm.
And just like that, you have a fully interactive web-based dashboard that allows you to interact with all the JMX interfaces exposed by your web server.
Of course, JMX is about more than just reporting the state of your application; you can also configure your system at runtime, with most enterprise libraries exposing an incredible amount of functionality via JMX operations.
Jolokia and Hawtio are excellent additions to any Java developer's toolbox, providing a wealth of information and functionality via easy to access web dashboards.