Using ehcache monitor
In my previous blog post serving a heavy load rss feed with spring 3 and ehcache I showed a solution for rss and caching using ehcache. After posting, Alex Miller commented about using a new product from ehcache called ehcache monitor. As curious as I am I decided to have a look.
In this blog post I give you a short introduction into the new monitor tool. I’ll use the sample from the previous post. I’ll explain the steps to take to do the test yourself, including installation. I will also show the screens to show you the actual monitoring.
Before I start with installation and screens, a very short recap of the sample. You can find the code of the sample at google code: http://code.google.com/p/gridshore/source/browse/#svn/trunk/news-feed. The sample is an application that demonstrates caching of published rss feeds. As a visitor you can subscribe to all news items and to all comments of a specific news item.
Ehcache is used to cache the feeds. I use the provided filter net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter to store the complete response using the requested uri as a key. An important part of this filter is that is only allows one request at a time to create a response for a certain request. The filter will hold the other requests for the same uri and use the cached response the moment it becomes available.
You can find more information about the monitor here: http://www.ehcache.org/modules/monitor.html. Before you can actually download it, you need to register for the beta. I used beta 2 for this demo. I’ll be checking out the next beta’s as well. Coming features are appealing. New features will be things like: History, graphs for cache efficiency and memory usage.
The monitor consists of two components, a probe that needs to be included in your application and a server component that exposes a web application that you can use to monitor the data.
All you have to do is add a jar to the classpath and slightly change the ehcache.xml configuration file. Let us start by changing the classpath.
The download comes with an installation file. Since my application uses maven, it would be logical to follow that path. Check the readme.txt file for how to do this. However, I want other people to be able to use my project without the ehcache monitor. Therefore I used the new intellij feature to add libraries to deployment units.
This is coming directly from the README.txt. Put it here just for completeness
For Maven users, to use the probe jar in a Maven project, you will need to install it to your local Maven repository. mvn install:install-file -Dfile=lib/ehcache-probe-1.00-beta2.jar \ -Dpackaging=jar \ -DgroupId=org.terracotta \ -DartifactId=ehcache-probe \ -Dversion=1.0.0-beta2 Then you can add this dependency into your pom.xml: <dependency> <groupId>org.terracotta</groupId> <artifactId>ehcache-probe</artifactId> <version>1.0.0-beta2</version> </dependency>
Create a global library eh-cache monitor and add the ehcache-probe-1.0.0-beta.jar to this library.
Now go to the artifacts section and select the war exploded one. Now drag the ehcache monitor library from the right into the WEB-INF/lib folder.
In the ehcache.xml we need to configure the probe. The following code block show my complete ehcache.xml, look for the cacheManagerPeerListenerFactory
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../main/config/ehcache.xsd"> <diskStore path="java.io.tmpdir"/> <cacheManagerEventListenerFactory class="nl.gridshore.newsfeed.web.LoggingCacheManagerListenerFactory"/> <cacheManagerPeerListenerFactory class="org.terracotta.ehcachedx.monitor.ProbePeerListenerFactory" properties="serverAddress=localhost, serverPort=9889" /> <defaultCache maxElementsInMemory="10" eternal="false" timeToIdleSeconds="5" timeToLiveSeconds="10" overflowToDisk="true" /> <cache name="RssFeedCachingFilter" maxElementsInMemory="50" eternal="false" timeToIdleSeconds="5000" timeToLiveSeconds="5000" overflowToDisk="true"> <cacheEventListenerFactory class="nl.gridshore.newsfeed.web.LoggingCacheListenerFactory"/> </cache> </ehcache>
Thats it, time to start our application and the monitor application.
Watching the monitor
Go to the bin folder of the extracted download and execute the right startup script. The last line of the console output shows the url to go to: http://localhost:9889/monitor. At first there will be no cache managers available. Now we start our own application. In our own log file we should see the following message:
Dec 22, 2009 11:27:41 AM org.terracotta.ehcachedx.monitor.DxService startBackend INFO: Started server at http://192.168.1.38:9889/monitor Dec 22, 2009 11:30:57 AM org.terracotta.ehcachedx.monitor.service.ProbeRegistrationService registerProbe INFO: The probe '192.168.1.38:61026' successfully registered with this server.
You can also check the monitoring app to see that the cache manager is available.
The statistics tab can be used to monitor the hits and misses for the cache, the configuration tab to see what is configured. I don’t think you can make changes here, but I am not sure. I want to focus on the Contents tab for now.
At first it is empty, select the cache manager and the cache. There is only one for both at the moment. Still the screen is pretty empty. Now let us use the application a bit. First I request the feed for all news items. Than for the comments of two different news items. The following image gives a screen dump. I only requested the feeds once. If you check the bottom of the screen you can find information about the selected cache item. You can see that the cache is not accessed. So let us ask it a few more times and see the changes. Check the second image to see that now the cache has been accessed.
Also have a look at the structure of the keys. These are generated by the filter. The null might look a bit strange, but this is due to the fact there are no parameters in the uri.
I like what I see so far, there are some small improvements possible in the interface. It looks like there is no autosync in the contents tab. But these are small things. Stay tuned, more to come with beta 3.