Tag Archives: java

Ehcache statistics


Here is what you need to do if you want to enable useful statistics information for the ehcache implementation in your Java application:

  1. Define two new beans – one for ehcacheManagementService and the other for mbeanServer – in the applicationContext file. Make sure that registerCacheStatistics constructor argument is set to true.
    <bean id="ehcacheManagementService" class="net.sf.ehcache.management.ManagementService">
          <constructor-arg name="cacheManager" ref="ehcache"/>
          <constructor-arg name="mBeanServer" ref="mbeanServer"/>
          <constructor-arg name="registerCacheManager" value="false"/>
          <constructor-arg name="registerCaches" value="false"/>
          <constructor-arg name="registerCacheConfigurations" value="false"/>
          <constructor-arg name="registerCacheStatistics" value="true"/>
    </bean>
    <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
          <property name="locateExistingServerIfPossible" value="true"/>
    </bean>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehcache"/>
    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
          p:configLocation="classpath:ehcache.xml" p:shared="true"/>
    <cache:annotation-driven/>
    
  2. Set statistics property of your cache in ehcache.xml to true.
    <cache name="coreCache"
             maxElementsInMemory="10000"
             eternal="false"
             timeToIdleSeconds="86400"
             timeToLiveSeconds="86400"
             overflowToDisk="true"
             maxElementsOnDisk="10000000"
             diskPersistent="false"
             diskExpiryThreadIntervalSeconds="86400"
             memoryStoreEvictionPolicy="LRU"
             statistics="true">
      </cache>
    

That’s it! Ehcache mbeans are now exposed to be consumed by any JMX-compatible monitoring tool, viz., JConsole or VisualVM (with Java Profiler plugin) to name a few.

Here is a screenshot of the Ehcache statistics in VisualVM:

Screen Shot 2014-08-08 at 23.33.18

Advertisements

Maven Surefire PermGen error


In case you are getting PermGen error while running tests using the Maven Surefire plugin, here is the solution:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>${surefire.version}</version>
  <configuration>
    <argLine>-Xmx512m -XX:MaxPermSize=256m</argLine>
  </configuration>
</plugin>

Setting the JVM args as part of the plugin did the trick!

Setting MAVEN_OPTS would not help because Maven Surefire plugin forks the JVM process while running the tests, and so the MAVEN_OPTS settings do not take effect.