How to monitor EhCache?


How do I monitor the EhCache that Play exposes either via JMX (see or programmatically? Specifically, I want to know how much RAM a given cache is using.

(Marcos Pereira) #2

Hi @mrubin,

Configuring EhCache to expose JMX mbeans should work. There is nothing in Play preventing that. What are you trying now? I’m not so sure if it is possible to just turn on a configuration in ehcache.xml file, but it should be easy to create and register a module that binds/initialize considering that we already provide a binding for CacheManager:

Finally, I don’t know how comprehensive EhCache statistics are or if it exposes how much memory the cache is using.



@marcospereira - thanks for pointing me in the right direction. I was able to get this working via something like the below:


val manager = net.sf.ehcache.CacheManager.getCacheManager(null)
val server = ManagementFactory.getPlatformMBeanServer()
ManagementService.registerMBeans(manager, server, false, false, false, true);

For what it’s worth, the JCache binding/module were not helpful - in the sense that I couldn’t figure out how to use DefaultCacheManagerProvider. Maybe javax.caching.CacheManager is being used under the hood somewhere.

(Marcos Pereira) #4

Oops, it was because I was looking at the wrong CacheManager. There is a bind for net.sf.ehcache.CacheManager too:

So you can inject it instead of creating your own (which may be disconnected from the one used by Play).



Thanks, that also works:

class MyObject @Inject()(private val cacheManagerProvider: CacheManagerProvider) {
  val ehCacheManager: CacheManager = cacheManagerProvider.get
  val server = ManagementFactory.getPlatformMBeanServer()

  ManagementService.registerMBeans(ehCacheManager, server, false, false, false, true);

When I print out ehCacheManager.get and CacheManager.getCacheManager(null), they both give me __DEFAULT__. (The documentation for the static getCacheManager() says, " If name is null, returns the default unnamed cacheManager if it has been created already") Both ways seem to behave identically when viewed via a JMX program like jconsole. FYI.

(Marcos Pereira) #6


One thing here is that you can inject CacheManager directly:

class MyObject @Inject()(private val cacheManager: CacheManager) {

No need to inject the provider and then get.