• New Feature
  • Status: Open
  • 2 Major
  • Resolution:
  • ehcache-core
  • Reporter: cedrik
  • May 12, 2011
  • 0
  • Watchers: 3
  • May 17, 2011



In a web context, the user is required to register an Ehcache-provided {{ServletListener}} to properly shut down the {{CacheManager}}. This is can, and should, be automatic, without requiring any user action.

To this effect, I propose to add an “automatic shutdown” feature to Ehcache-core.

This JIRA serves as a discussion to this new feature. If nobody objects, I will commit in a few days.


Tim Eck 2011-05-12

I’m assuming you’re talking about dynamically registering a context listener (presumably the existing ShutdownListener) somehow. I’m curious about two things with implementing this though:

1) Where does one get access to the servlet context from the cachemanager 2) I was under the assumption that adding listeners at runtime is only possible with servlet spec 3.0+

Or is this something in a framework (spring?)

As with anything new and “automatic” there should probably be a way to disable this behavior I would think

Cedrik LIME 2011-05-13

Hi Tim,

You don’t need to access the {{ServletContext}} from anywhere. This patch is based on the automatic Listener registration when declared in a TLD (see attached file, which shall be copied into {{jar:Ehcache-core.jar!/META-INF/}}). JSP 2.0 specification reference: {quote} JSP.7.1.9 Event Listeners A tag library may include classes that are event listeners (see the Servlet 2.4 specification). The listeners classes are listed in the tag library descriptor and the JSP container automatically instantiates them and registers them. A Container is required to locate all TLD files (see Section JSP.7.3.1 for details on how they are identified), read their listener elements, and treat the event listeners as extensions of those listed in web.xml. The order in which the listeners are registered is undefined, but they are registered before application start. {quote}

So no, you don’t need Servlet 3.0 to to that kind of fancy stuff! (Shameless plug: it is the very same technique I use to automatically register my Listener in [MessAdmin|http://messadmin.sourceforge.net].)

As far as I know, there is no way to disable this behaviour, but it shouldn’t matter much since: * shutting down all {{CacheManager}}’s is explicitely recommended in the documentation * shutting down a {{CacheManager}} multiple times is a noop operation, which is the only potential consequence of automatically registering the {{ShutdownListener}}

So, what do you say?

Tim Eck 2011-05-13

Neat trick with the TLD.

It’s always hard to predict all usage in the wild which is why I seek some means to disable this. Perhaps we should have a servlet context attribute someone could define in web.xml that would make the shutdown listener do nothing on contextDestroyed()

If someone has ehcache-core.jar in a shared classloader (ie. higher up the chain than WEB-INF/lib) could this become problematic? I couldn’t tell from the JSP spec if the TLD would even be discovered in that case.

Really Greg gets to decide on stuff like this.

Cedrik LIME 2011-05-17

According to my experience, having {{ehcache-core.jar}} in a shared classloader would result in no automatic tld registration (and hence no {{ShutdownListener}}). That said, I can not find anything specific in the specs for this use case, so I suspect this could be implementation-dependant.

Tim, you probably have a better vision on ehcache usage in the wild than me, so I followed your recommendation by enhancing the existing {{ShowndownListener}} to be optionally disabled by configuration (web.xml context parameter: {{ehcache.disable_automatic_shutdown}}).

Greg, are you happy with those changes? Shall I commit?

Fiona OShea 2011-05-17

Greg what do you think?