How to set Dropwizard / CodaHale metrics for ehCache in Spring Boot application?

So here is my problem. Spring's download application uses Hibernate / JPA and ehCache for level 2 caching. I would like an ehCache tool with Dropwizard / Coda-Hale tags, but I'm not quite sure how to do this. If I manually created Cache instances then this is easy. You just use the decorator as shown here . But since this is Spring / Hibernate, I have no control over the caches. Any ideas on how I'm going to set this up?

+4
source share
1 answer

Well, I believe that I finally understood. It turns out that adding Spring to JavaConfig for the cacheManager that returned the Spring classes, ehCacheCacheManager could still be used to access and decorate ehCache classes. That's how I got it to work

@Bean
public EhCacheCacheManager ehCacheCacheManager() {
    final EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager(ehCacheManagerFactoryBean().getObject());
    net.sf.ehcache.CacheManager cacheManager = ehCacheCacheManager.getCacheManager();
    String[] cacheNames = cacheManager.getCacheNames();
    for (String cacheName : cacheNames) {
        Cache cache = cacheManager.getCache(cacheName);
        cacheManager.replaceCacheWithDecoratedCache(cache, InstrumentedEhcache.instrument(metricRegistry, cache));
    }
    return ehCacheCacheManager;
}


@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {

    EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();

    cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
    cacheManagerFactoryBean.setShared(true);

    return cacheManagerFactoryBean;
}
+5
source

All Articles