开发者

Tomcat, Singletons, & ServerContextListener

开发者 https://www.devze.com 2023-01-02 17:24 出处:网络
I\'ve been attempting to pull hard-coded configuration data from a Tomcat web-app which I\'m working on,开发者_JS百科 into JNDI (web.xml) to make the app more configurable.Instead of continuously doin

I've been attempting to pull hard-coded configuration data from a Tomcat web-app which I'm working on,开发者_JS百科 into JNDI (web.xml) to make the app more configurable. Instead of continuously doing lookups into the InitialContext, I'm caching the lookups and the InitialContext using a singleton.

Of course, when I stop and start the application, the static instance of my singleton remains alive -- therefore, the cache doesn't get "refreshed". My question is dual -- I'm isolating this singleton to a single web application -- are there any unforeseen effects in Tomcat [I've read that singleton's & tomcat don't mix]? And what is the easiest way to clear the singleton instance when the application is stopped (I was thinking of writing a separate ServerContextListener which would clear the instance).

(Note: I apologize if I used the terminology incorrectly -- please correct -- this is my first foray into programming with Tomcat)


Instead of continuously doing lookups into the InitialContext, I'm caching the lookups and the InitialContext using a singleton

Ok, so you implemented a Service Locator with a cache, that [is|was] a common practice for the web tier. Here is an example.

Of course, when I stop and start the application, the static instance of my singleton remains alive

Hmm... unless you are deploying your singleton in the Common class loader ($CATALINA_HOME/lib) it shouldn't. You're not doing this, right?

I'm isolating this singleton to a single web application -- are there any unforeseen effects in Tomcat

Nothing I'm aware of.

And what is the easiest way to clear the singleton instance when the application is stopped

Deploy your singleton in the webapp class loader (i.e. in /WEB-INF/classes or /WEB-INF/lib) and undeploy the application as suggested.


How your singleton is created?

If you load the singleton from your webapp, it will be isolated to a single webapp because it's loaded by the webapp's own classloader. And it will be destroyed when you undeploy the webapp. Instead of stopping your application, you should undeploy it.

0

精彩评论

暂无评论...
验证码 换一张
取 消