At some point after creating your caches, you can do something like this:
for (CacheManager manager : CacheManager.ALL_CACHE_MANAGERS) { for (String name : manager.getCacheNames()) { manager.getCache(name).getCacheConfiguration().setStatistics(true); } }
Of course, you will want to add error checking.
If you have caches created dynamically, you can use the Cache Manager Event Listener (see the documentation ). Basically you need to create a factory by extending the CacheManagerEventListenerFactory , and then create the actual listener by running the CacheManagerEventListener . The listener might look like this:
public class StatisticsEnabledCacheManagerListener implements CacheManagerEventListener { public void notifyCacheAdded(String cacheName) { CacheManager.getInstance().getCache(cacheName).getCacheConfiguration().setStatistics(true); } public void notifyCacheRemoved(String cacheName) {} }
To register a factory with Ehcache, add it to ehcache.xml:
<cacheManagerEventListenerFactory class="com.example.cache.MyListenerFactory" properties=""/>
It is important to note that if you set the default cache to enable statistics, then any cache you create will have default statistics, unless creating a cache disables it.
source share