Adding JMX to your spring project is a snap. If you need to ask what JMX is, basically think of the most awesome way to manage your Java beans and applications and multiply it by 50. Then divide by 25. But seriously, JMX provides you a way to execute methods from your Java beans by connecting to your running application via RMI. It’s a great way to check on the status of objects in your app while it is running. Best of all, you get it all for almost free. Just add this bean to your application context:
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="someCategory:name=cacheManager"
value-ref="cacheManager"/>
<entry key="fooCategory:type=fooSubCategory,name=FooBar"
value-ref="fooBar"/>
</map>
</property>
</bean>
Inside the map, insert an entry for every bean you want to expose. When a bean is exposed, its public functions will be available to JMX clients. This is particularly useful for methods that take primitives as parameters and/or return primitives. The value-ref field points to a bean in your application context. The key defines a folder for your bean, an optional subfolder, and the name of your bean. This helps organize things in a visual JMX application like JConsole.
To get your JMX server running, execute your Java program with the following command line options:
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=13590
-Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
The first parameter turns on the server/RMI. The second parameter makes JMX available on a specified port (just a little useful for remote connections). Turning SSL off and authenticate off in the third and fourth parameters is useful for internal networks. If you want to expose your JMX console to the world, you’ll want to research adding some sort of authentication via these options. If you’re putting your application inside Tomcat, you can modify the JAVA_OPTS variable inside bin/catalina.sh or bin/catalina.bat. Just add the same options and restart Tomcat and you’ll be ready to go.
Finally, you’ll probably want to see the results of all this configuration. In Java 5 and 6, all you need to do is run:
jconsole
at the command prompt and it will detect all running JMX applications on your localhost. Alternatively, you can use it to connect to a remote host running JMX.
I think JMX is a great tool both for development and production and is yet another reason to develop your apps in Java. Check it out today if you haven’t already.