On Wed, Apr 14, 2010 at 3:17 AM, Pid <p...@pidster.com> wrote: > > > In my web application, I'm using the 'reloadable='true'' attribute to >> Context to reload the application automatically when a .class or jar files >> changes. To avoid a quickly accumulating memory leak, I need to shut down >> a >> service thread my application has started when the web application is >> reloaded. >> >> What API function can be used to notify my application that it is about to >> be shut down, so that the thread in question can exit? >> >> First, is there a standard API that would work also in other J2EE >> containers >> besides Tomcat? >> > > As Bob suggested, ServletContextListener is the way forwards. > > Thank you for your replies. I did see ServletContextListener (as well as LifeCycleListener, etc. etc.). What I couldn't find out is how to obtain an instance of the object that would support an 'addServletContextListener' method.
For instance, if you look at http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/ServletContextListener.htmlit says: "To recieve (sic) notification events, the implementation class must be configured in the deployment descriptor for the web application." This doesn't sound like an API to me, it sounds like a configuration option, so it doesn't answer my question. Is it true that there is, basically, no API a web application can call at runtime to add such listeners? The most likely candidate to which to add a ServletContextListener would be a ServletContext, but it doesn't appear to support an 'addListener' method, unless I'm overlooking it: http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html > > Second, is there a custom API in Tomcat? >> > > Actually yes, but as you might imagine, there are restrictions. > > http://tomcat.apache.org/tomcat-6.0-doc/config/context.html#LifecycleListeners > > I saw that too, but again, it doesn't answer my problem. On the page you quote it says: "The class name you specify must implement the org.apache.catalina.LifecycleListener interface, and the class must be packaged in a jar and placed in the $CATALINA_HOME/lib directory. " I am interested in a solution that does not require access to the $CATALINA_HOME/lib directory. This directory is accessible only to the Tomcat administrator, and I would like my web app - if possible - to be deployed even in scenarios where the Tomcat configuration cannot be changed. And lastly, this solution would not work because the LifecycleListener itself would not be reloaded, since (to my knowledge) jar files in $CATALINA_HOME/lib aren't reloaded. > I'm really interested in a solution that would be contained inside the web >> application (i.e., first or second above) and that would not require >> changes >> to the server configuration, so that it would work in hosted environments >> where I do not control the Tomcat configuration. I'm using the latest 6.0 >> release (6.0.20). >> > > 6.0.26? > > 6.0.24, actually. Thank you for your insights. - Godmar