You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by David Hemingway <db...@kooee.com.au> on 2003/01/10 01:49:35 UTC
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. 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
regards,
Dave
Re: Urgent problem with Singleton
Posted by David Hemingway <dh...@datafast.net.au>.
Craig,
As well as synchronizing that block and setting reloadable="false" in the
server.xml I paid close attention to when these extra instances are being
created. When the server start and no requests have been received there is
only the 1 instance. However as soon as a single request is made another 2
instances appear. From then on no more get created. I am not modifying code
on the fly either. Any other ideas?
regards,
Dave
----- Original Message -----
From: "Craig R. McClanahan" <cr...@apache.org>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Friday, January 10, 2003 12:49 PM
Subject: Re: Urgent problem with Singleton
>
>
> On Fri, 10 Jan 2003, David Hemingway wrote:
>
> > Date: Fri, 10 Jan 2003 11:49:35 +1100
> > From: David Hemingway <db...@kooee.com.au>
> > Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> > To: Tomcat Users List <to...@jakarta.apache.org>
> > 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:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Urgent problem with Singleton
Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Fri, 10 Jan 2003, David Hemingway wrote:
> Date: Fri, 10 Jan 2003 11:49:35 +1100
> From: David Hemingway <db...@kooee.com.au>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> To: Tomcat Users List <to...@jakarta.apache.org>
> 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: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>