You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Mats Andersson <to...@yahoo.se> on 2001/10/12 21:10:13 UTC
Tomcat 4.0 manager application and Timer
Hi all!
I wonder if anyone have a good explanation to
what is happening when the manager application
is used to restart an application. I thought
nothing would remain from the "old" version,
but when I restart my application a Timer
started in the "old" version is still alive.
I tried to add a call to the cancel() method
of the Timer in the finalize() method of one of the
classes,
but it seems like the finalize method is never
run. How do I catch this restart in my application
so that I can stop the Timer object. If this
can't be done it seems impossible to use either
the restart application or the Timer.
_____________________________________________________
Nokia Game kör igång igen!
Gå till http://se.yahoo.com/nokiagame för att gå med i det nya äventyret innan den tredje November.
Re: Tomcat 4.0 manager application and Timer
Posted by Pier Fumagalli <pi...@betaversion.org>.
Mats Andersson at tomkatten2000@yahoo.se wrote:
> Ok, thanks. But how does an instance of a class
> (the instance that created the Timer) know it is
> unloaded? It is not a Servlet so I can't use the
> destroy() method. The finalize() method isn't called
> or at least it takes a while and the Timer must be
> stopped immediately to avoid unstable behavior.
> I know I can always put the code in any Servlet
> destroy() method, but that is not a nice solution
> because that servlet has nothing to do with the
> Timer.
You can register a handler in the context for the destroy event... Look at
the Servlet 2.3 specification (ServletContextEvent and
ServletContextListener classes)...
Pier
Re: Tomcat 4.0 manager application and Timer
Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Tue, 16 Oct 2001, Mats Andersson wrote:
> Ok, thanks. But how does an instance of a class
> (the instance that created the Timer) know it is
> unloaded?
That is totally up to your application to deal with. You will need to
save a reference to the thread when it is created, and be sure to kill it
at the appropriate time. Some places that make sense to implement cleanup
like this in web applications:
* In the destroy() method of a servlet (this is called
when applications are being shut down)
* In the valueUnbound() method of an HttpSessionBindingListener
(called when an attribute is removed from a session, including
when the session is invalidated)
* (Servlet 2.3 only) in the contextDestroyed() method of a
ServletContextListener (called when the application is being
shut down).
Applications are always responsible for cleaning up after their own
objects and threads. The only thing the container takes care of for you
is servlets.
It is not a Servlet so I can't use the
> destroy() method.
So save a reference to the Thread someplace that the destroy() method of a
servlet can find it.
> The finalize() method isn't called
> or at least it takes a while and the Timer must be
> stopped immediately to avoid unstable behavior.
> I know I can always put the code in any Servlet
> destroy() method, but that is not a nice solution
> because that servlet has nothing to do with the
> Timer.
>
> Mats
>
Craig
Re: Tomcat 4.0 manager application and Timer
Posted by Mats Andersson <to...@yahoo.se>.
--- "Craig R. McClanahan" <cr...@apache.org>
wrote: > On Sun, 14 Oct 2001, Mats Andersson wrote:
>
> > Date: Sun, 14 Oct 2001 07:38:50 +0200 (CEST)
> > From: Mats Andersson <to...@yahoo.se>
> > Reply-To: tomcat-user@jakarta.apache.org
> > To: tomcat-user@jakarta.apache.org
> > Subject: Re: Tomcat 4.0 manager application and
> Timer
> >
> > --- Peter Mutsaers <pe...@mutsaers.com> wrote: >
> >>
> > "Mats" == Mats Andersson <to...@yahoo.se>
> > > writes:
> > >
> > > Mats> I wonder if anyone have a good
> explanation
> > > to what is
> > > Mats> happening when the manager application
> is
> > > used to restart an
> > > Mats> application. I thought nothing would
> > > remain from the "old"
> > > Mats> version, but when I restart my
> application
> > > a Timer started
> > > Mats> in the "old" version is still alive.
> I
> > > tried to add a call
> > > Mats> to the cancel() method of the Timer in
> the
> > > finalize() method
> > >
> > > Hmm, if this is true, this explains the problem
> that
> > > occurred to me
> > > this morning. I restarted my webapp, a
> jobscheduler,
> > > yesterday
> > > twice. This morning each job was started three
> > > times, leading to
> > > horrible chaos.
> > >
> > > I hope someone knows what goes on with timers
> and
> > > threads when a
> > > webapp is reloaded through the manager.
> > >
> > > Should I first uninstall and then re-deploy
> instead?
> > >
> > > --
> > > Peter Mutsaers | Dübendorf | UNIX - Live
> free
> > > or die
> > > plm@gmx.li | Switzerland | Sent via
> FreeBSD
> > 4.3-stable
> >
> > I wonder if this is a feature or a bug. When the
> Timer
> > thread is not removed, other classes is not
> > garbage-collected, and thus the application is
> really
> > not removed, it is only not possible to reach it
> from
> > a browser. When the application is brought up
> again
> > two instances of the app run at the same time.
> >
>
> Who created the Timer thread in the first place? If
> your application
> created it, it is ***your*** responsibility to clean
> it up.
>
> Same thing goes for maintaining references to
> objects loaded by the old
> class loader (Tomcat doesn't do that). If your app
> does this, then your
> app has bugs in it.
>
Ok, thanks. But how does an instance of a class
(the instance that created the Timer) know it is
unloaded? It is not a Servlet so I can't use the
destroy() method. The finalize() method isn't called
or at least it takes a while and the Timer must be
stopped immediately to avoid unstable behavior.
I know I can always put the code in any Servlet
destroy() method, but that is not a nice solution
because that servlet has nothing to do with the
Timer.
Mats
>
> > Comments?
> >
>
> Craig
>
_____________________________________________________
Nokia Game kör igång igen!
Gå till http://se.yahoo.com/nokiagame för att gå med i det nya äventyret innan den tredje November.
Re: Tomcat 4.0 manager application and Timer
Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Sun, 14 Oct 2001, Mats Andersson wrote:
> Date: Sun, 14 Oct 2001 07:38:50 +0200 (CEST)
> From: Mats Andersson <to...@yahoo.se>
> Reply-To: tomcat-user@jakarta.apache.org
> To: tomcat-user@jakarta.apache.org
> Subject: Re: Tomcat 4.0 manager application and Timer
>
> --- Peter Mutsaers <pe...@mutsaers.com> wrote: > >>
> "Mats" == Mats Andersson <to...@yahoo.se>
> > writes:
> >
> > Mats> I wonder if anyone have a good explanation
> > to what is
> > Mats> happening when the manager application is
> > used to restart an
> > Mats> application. I thought nothing would
> > remain from the "old"
> > Mats> version, but when I restart my application
> > a Timer started
> > Mats> in the "old" version is still alive. I
> > tried to add a call
> > Mats> to the cancel() method of the Timer in the
> > finalize() method
> >
> > Hmm, if this is true, this explains the problem that
> > occurred to me
> > this morning. I restarted my webapp, a jobscheduler,
> > yesterday
> > twice. This morning each job was started three
> > times, leading to
> > horrible chaos.
> >
> > I hope someone knows what goes on with timers and
> > threads when a
> > webapp is reloaded through the manager.
> >
> > Should I first uninstall and then re-deploy instead?
> >
> > --
> > Peter Mutsaers | Dübendorf | UNIX - Live free
> > or die
> > plm@gmx.li | Switzerland | Sent via FreeBSD
> 4.3-stable
>
> I wonder if this is a feature or a bug. When the Timer
> thread is not removed, other classes is not
> garbage-collected, and thus the application is really
> not removed, it is only not possible to reach it from
> a browser. When the application is brought up again
> two instances of the app run at the same time.
>
Who created the Timer thread in the first place? If your application
created it, it is ***your*** responsibility to clean it up.
Same thing goes for maintaining references to objects loaded by the old
class loader (Tomcat doesn't do that). If your app does this, then your
app has bugs in it.
> Comments?
>
Craig
Re: Tomcat 4.0 manager application and Timer
Posted by Mats Andersson <to...@yahoo.se>.
--- Peter Mutsaers <pe...@mutsaers.com> wrote: > >>
"Mats" == Mats Andersson <to...@yahoo.se>
> writes:
>
> Mats> I wonder if anyone have a good explanation
> to what is
> Mats> happening when the manager application is
> used to restart an
> Mats> application. I thought nothing would
> remain from the "old"
> Mats> version, but when I restart my application
> a Timer started
> Mats> in the "old" version is still alive. I
> tried to add a call
> Mats> to the cancel() method of the Timer in the
> finalize() method
>
> Hmm, if this is true, this explains the problem that
> occurred to me
> this morning. I restarted my webapp, a jobscheduler,
> yesterday
> twice. This morning each job was started three
> times, leading to
> horrible chaos.
>
> I hope someone knows what goes on with timers and
> threads when a
> webapp is reloaded through the manager.
>
> Should I first uninstall and then re-deploy instead?
>
> --
> Peter Mutsaers | Dübendorf | UNIX - Live free
> or die
> plm@gmx.li | Switzerland | Sent via FreeBSD
4.3-stable
I wonder if this is a feature or a bug. When the Timer
thread is not removed, other classes is not
garbage-collected, and thus the application is really
not removed, it is only not possible to reach it from
a browser. When the application is brought up again
two instances of the app run at the same time.
Comments?
_____________________________________________________
Nokia Game kör igång igen!
Gå till http://se.yahoo.com/nokiagame för att gå med i det nya äventyret innan den tredje November.
Re: Tomcat 4.0 manager application and Timer
Posted by Peter Mutsaers <pe...@mutsaers.com>.
>> "Mats" == Mats Andersson <to...@yahoo.se> writes:
Mats> I wonder if anyone have a good explanation to what is
Mats> happening when the manager application is used to restart an
Mats> application. I thought nothing would remain from the "old"
Mats> version, but when I restart my application a Timer started
Mats> in the "old" version is still alive. I tried to add a call
Mats> to the cancel() method of the Timer in the finalize() method
Hmm, if this is true, this explains the problem that occurred to me
this morning. I restarted my webapp, a jobscheduler, yesterday
twice. This morning each job was started three times, leading to
horrible chaos.
I hope someone knows what goes on with timers and threads when a
webapp is reloaded through the manager.
Should I first uninstall and then re-deploy instead?
--
Peter Mutsaers | Dübendorf | UNIX - Live free or die
plm@gmx.li | Switzerland | Sent via FreeBSD 4.3-stable
Re: Tomcat 4.0 manager application and Timer
Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Fri, 12 Oct 2001, Mats Andersson wrote:
> Date: Fri, 12 Oct 2001 21:10:13 +0200 (CEST)
> From: Mats Andersson <to...@yahoo.se>
> Reply-To: tomcat-user@jakarta.apache.org
> To: tomcat-user@jakarta.apache.org
> Subject: Tomcat 4.0 manager application and Timer
>
> Hi all!
>
> I wonder if anyone have a good explanation to
> what is happening when the manager application
> is used to restart an application. I thought
> nothing would remain from the "old" version,
> but when I restart my application a Timer
> started in the "old" version is still alive.
> I tried to add a call to the cancel() method
> of the Timer in the finalize() method of one of the
> classes,
> but it seems like the finalize method is never
> run. How do I catch this restart in my application
> so that I can stop the Timer object. If this
> can't be done it seems impossible to use either
> the restart application or the Timer.
>
The finalize() method will not be called until the object is garbage
collected, which could be a *long* time after the reload takes place.
Applications should clean up after themselves when they are being shut
down -- either in the destroy() method of your servlets or (Servlet 2.3
only) in an application event listener that catches the contextDestroyed()
event. Tomcat doesn't know you started a thread -- that's your
application's problem.
NOTE: When you run Tomcat under a security manager, you won't be allowed
to start threads in a web app unless you explicitly tweak the security
policies. This will also be true for any J2EE application server.
Craig McClanahan