How to monitor EhCache?

How do I monitor the EhCache that Play exposes either via JMX (see http://www.ehcache.org/documentation/2.7/operations/jmx.html) or programmatically? Specifically, I want to know how much RAM a given cache is using.

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 net.sf.ehcache.management.ManagementService 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.

Best.

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

import java.lang.management.ManagementFactory
import net.sf.ehcache.management.ManagementService

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.

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

https://github.com/playframework/playframework/blob/2.6.x/framework/src/play-ehcache/src/main/scala/play/api/cache/ehcache/EhCacheApi.scala#L89

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

Best.

Thanks, that also works:

@Singleton
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.

Nice.

One thing here is that you can inject CacheManager directly:

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

No need to inject the provider and then get.

Best.