On Fri, 10 Jan 2003, David Hemingway wrote:
> Date: Fri, 10 Jan 2003 11:49:35 +1100
> From: David Hemingway <[EMAIL PROTECTED]>
> Reply-To: Tomcat Users List <[EMAIL PROTECTED]>
> To: Tomcat Users List <[EMAIL PROTECTED]>
> Subject: Urgent problem with Singleton
>
> Hi,
>
> I have a task scheduler thread that implements ServletContextListener and starts up
>as a listener with the following code from my web.xml:
>
> <listener>
> <listener-class>
> mypackage.MyServlet
> </listener-class>
> </listener>
>
> My problem is however I need this to be a singleton, its imperative.
> There always seems to be about 3 threads created though.
The number of threads that Tomcat creates is *totally* independent of the
number of listener instances that are created.
> I have tried
> numerous things including making this servlet call a singleton class to
> do the task scheduling but it still ends up with 3 threads. This is
> causing terrible concurrency problems that wouldn't exist if this was
> working properly. My serlvet code currently resembles the following:
>
> boolean hasBeenInitialized = false;
>
> public void contextInitialized(ServletContextEvent sce)
> {
> if(!hasBeenInitialized)
> {
> //Start up the task scheduler thread
> hasBeenInitialized = true;
> }
> }
>
>
> I am using Tomcat 4.1.18, Apache 1.3.27 under RedHat 7.3. I didn't have
> this problem under wn32 only Linux. Any help, or ideas on how I can get
> this to be a single instance would be greatly appreciated. Thank you
>
At webapp startup time, Tomcat is supposed to create one and only one
instance of each declared listener, and call its appropriate init method
(contextInitialized() in your case). However, there's a bug in 4.1.18
(and earlier 4.1.x releases) where, if you are using Tomcat's auto-reload
facility, the list of listeners doesn't get cleared so a new instance of
the listener gets added each time you reload the app.
This will be fixed in 4.1.19 -- in the mean time, be sure that you avoid
Tomcat's auto-reload facilities if you depend on the listeners being
singletons.
A class that implements ServletContextListener should *not* itself be a
Servlet, by the way. The only purpose for such a class should be to
initialize resources that your webapp needs, not to also respond to
requests.
> regards,
> Dave
>
>
Craig
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>