tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kim Altintop <...@deepfx.com>
Subject Multiple instances of Filter class
Date Thu, 28 Feb 2002 17:31:14 GMT
Hi,

I've got a webapp that reads almost everything from a database. As this
causes a lot of overhead, I implemented a Filter that caches whole pages
(or images or whatever requested) to the filesystem and serves these to
subsequent requests with the same URI. Additionally, all HTTP headers that
I generate from the database records are kept in memory and set every time
a cached page is requested. Naturally, if Tomcat is stopped or restarted,
the in-memory cache entries are lost and the fs is cluttered with stale
cache files. To work around this problem, I created a subclass of
java.lang.Thread to frequently check if the files are still valid (that is,
present in the cache table) and delete them if necessary. For this purpose
it is absolutely necessary, that only one instance of each the cache table
(a HashMap) and the cleanup thread is present in the webapp.

The servlet spec says [version 2.3, section SRV.6.2.1 "Filter Lifecycle"]: 

"Only one instance per filter declaration in the deployment descriptor is
instantiated per Java virtual machine of the container."


However, I discovered that this is not true with Tomcat (I'm running
4.0.2-LE under JDK1.4/Win2000), as you can easily verify by installing the
following code as a filter in your webapp:

public class DummyFilter
	implements Filter {
	private static int instance = 0;

	public void init(FilterConfig config) {
		System.out.println("DummyFilter.init [" + instance + "]");
		instance++;
	}

	public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
		throws ServletException, IOException {
		System.out.println("DummyFilter.doFilter");
		chain.doFilter(request, response);
	}

	public void destroy() {
		System.out.println("DummyFilter.destroy");
	}
}


One would expect the init() method to print "DummyFilter.init [0]" exactly
one time on stdout, when Tomcat is started (or the webapp is deployed).
However, what happens is, that the same String is printed TWICE (yes,
instance is 0 each time). I then changed the init() code to print 

  ClassLoader myloader = this.getClass().getClassLoader();
  System.out.println(myloader.getClass().getName() + "[" +
myloader.hashCode() + "]");

and the result was, that two different hash codes were printed. 
For this reason I suspect that there are actually TWO WebappClassLoaders
installed for my application, each of which instantiating DummyFilter (in
which case the behaviour of the static variable is correct, I believe...).


Am I missing something or could this be considered a bug?

Thanks in advance for any enlightenment..
Kim

--
To unsubscribe, e-mail:   <mailto:tomcat-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-dev-help@jakarta.apache.org>


Mime
View raw message