I have a Java servlet filter that should clean up some resources whenever the filter is not longer used. This is achieved by implementing the Filter.destory
method. Now I wonder if this method is also called when the entire servlet container is shutdown. Therefore, I was wondering if I should install a shutdown hook for this purpose. Or will the Filter.destroy
method always called when the servlet container is shut down.
When I set a break point in my shutdown hook I never get to it. But I guess this is because IntelliJ already disconnected or some other reason like that. That makes it hard to find out by experimenting.
Thanks for any help!
Using Jetty 6.1.22 Filter.destroy
was called during shutdown. The spec just defines
Called by the web container to indicate to a filter that it is being taken out of service.
You should test it without IntelliJ, in a "real" environment, with a stand-alone tomcat
or jetty
.
Anyway, just to be safe, you could clean up both in Filter.destroy
and ServletContextListener.contextDestroyed
.
In that case synchronize your filter clean up. And make sure, if you clean up twice, that the 2nd run does not fail with an exception etc.