Given an existing web application that uses OSCache, we can pretty easily add support for JMX monitoring.
A couple of things first:
- We are using Sun’s Glassfish application server
- The application is not yet spring-enabled
- The project uses maven for dependancy management
Since we are using Glassfish, we already have an MBean server and connector, so we don’t have to worry about configuring those parts of the stack. (An MBean is a Managed JavaBean)
As for Spring support, we are restricting its introduction to a small aspect of the application (for now!), so we are minimizing the impact on the rest of the application.
What we need to do:
oscache.propertiesand add an event listener
- Add a dependancy on
- Add a spring context for the JMX exporter
web.xmlto load the spring context
OSCache event listener
Firstly, we need to set a listener for OSCache that will publish the statistics we are interested in.
oscache.properties, there is a section for event listeners. We want to implement a statistical listener:
If you’re using maven, add a dependency on
spring-web. This has dependancies on
spring-beans (At the time of writing,
3.0.2.RELEASE was the latest version in the maven repositories).
Otherwise download these manually and chuck them in your
I made a separate spring context file (
cacheContext.xml) for the cache MBean exporter:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- OSCache stats listener bean --> <bean id="statisticListener" class="com.opensymphony.oscache.extra.StatisticListenerImpl"/> <!-- Export the OSCache stats beans --> <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"> <property name="beans"> <map> <entry key="oscache-bean:name=Statistics" value="statisticListener"/> </map> </property> </bean> </beans>
The map in the
exporter links the statistics bean to a key that will be visible in JConsole.
Note: The OpenSymphony link in the references below gives you code to expose an arbitrary JMX connector and port. This is unnecessary since we’re accessing via the application server.
Adding the Spring context loader
web.xml then needs to be updated to be told to load the new
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/cacheContext.xml</param-value> </context-param> ... <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
JDK 1.5 includes two JMX clients that you can use.
jconsole is the legacy monitoring application, which has been superseded by the fancy UI of
Either application will allow you to monitor the statistics bean, however in
jvisualvm you will need to install the plugin VisualVM-MBeans under
Tools > Plugins. Meanwhile
jconsole has an MBeans tab by default.
When your application is running, use one of these utilities to connect to the application server (either directly or via
localhost:8686 for glassfish). Under the
oscache-bean > Statistics, you should be able to see counts generated by cache attributes. Double-clicking the numbers will pop up a graph, and right-clicking will allow you to export the recorded values.