A-ha - it seems the shutdown hook does not get called when tomcat is run
as a service, but does seems to get called when run as standalone. In
that case it would seem that the problem would lie with the way the
service is configured (ie what it does to stop) compared to the
standalone version, I haven't had time to look at that yet.

I was removing the shutdown hook because once the sensitive task has
run, it doesn't need to be there anymore - the idea was to allow the app
to exit gracefully if it was doing something sensitive (eg inserting a
bunch of related data into a DB) by looping through the shutdown thread
until the business object was done, rather than shutdown the app in a
way which might affect data integrity. I was trying to avoid a flurry of
unnecessary shutdown hooks staying registered even though the calling
logic had long since completed.

I'm not sure how good an idea it is to use a shutdown hook to do this in
tomcat though - I'm not sure how things work when the context gets
destroyed. From what I understand, in theory all the objects required by
the logic should be keep alive until all references to them disappear
(from the object requesting the shutdown hook for instance). In practice
though, I get a "lifecycle error: CL stopped" Exception from tomcat on
shutdown and the shutdown hook in my example keeps on running in an
infinite loop. I read somewhere that it had to do with classloader
issues in some version of tomcat 4
(http://nagoya.apache.org/bugzilla/show_bug.cgi?id=3888), but i'm not
sure that applies here. In any case it would seem that the shutdown hook
might not be the best solution here, and that i will probably have to
provide separate standalone and tomcat wrappers to the business logic :(

oh well...

thanks for your help

Elie




> 
> Hi,
> You're probably removing the shutdown hook too early.  Why are you
> removing it at all?
> 
> Yoav Shapira
> Millennium ChemInformatics
> 
> 
> >-----Original Message-----
> >From: Elie Medeiros [mailto:[EMAIL PROTECTED]
> >Sent: Wednesday, March 03, 2004 5:44 AM
> >To: [EMAIL PROTECTED]
> >Subject: addShutdownHook in Tomcat does not seem to get called on
> shutdown
> >
> >
> >Hi,
> >
> >I added a shutdown hook in my app, which works fine when I run it in
> >standalone mode, but which does not seem to get called when Tomcat
> stops.
> >
> >The shutdown hook operates according to following the following
> semantics:
> >_______________________________________________________________________
> ___
> >class MyApp{
> >
> >public void doSomething(){
> >     ShutdownHook sdh = new ShutdownHook();
> >     synchronized(Runtime.getRuntime()){
> >             Runtime.getRuntime().addShutdownHook(sdh);
> >     }
> >     //
> >     //do something here
> >     //
> >     logger.info("Finished doing something");
> >     //remove shutdown hook once process has finished
> >     sdh.setFinished();
> >     if (!sdh.isInitialised()){
> >             synchronized(Runtime.getRuntime()){
> >                     Runtime.getRuntime().removeShutdownHook(sdh);
> >             }
> >     }
> >}
> >
> >     private class ShutdownHook extends Thread {
> >             private boolean INITIALISED = false;
> >             private boolean FINISHED = false;
> >
> >             public void run() {
> >                     this.INITIALISED = true;
> >                     this.setPriority(2);
> >                     logger.debug("Shutdown hook: shutdown thread
> started - a
> >shutdown has
> >been requested");
> >                     out :
> >                     while (true) {
> >                             //keep on looping until the claaing app
> has
> >finished
> >                             synchronized(this.FINISHED){
> >                                     if (this.FINISHED == true) {
> >                                             logger.debug("Shutdown
> hook: parent
> >program finished, allowing
> >shutdown process to complete");
> >                                             return;
> >                                     }
> >                             }
> >                     }
> >             }
> >
> >             public synchronized boolean isInitialised() {
> >                             return this.INITIALISED;
> >             }
> >
> >             public synchronized void setFinished() {
> >                             this.FINISHED = true;
> >             }
> >     }
> >}
> >_______________________________________________________________________
> ___
> >
> >
> >The log does not show any trace of the shutdown hook being called, and
> >the process does indeed not complete before Tomcat shuts down, which to
> >me sounds like the hook is not getting registered properly for some
> >reason. Any ideas why this might be happening? I am running Tomcat
> >4.1.18 on Windows 2K (no advice about switching to Linux please).
> >
> >Thanks,
> >
> >Elie
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: [EMAIL PROTECTED]
> >For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 
> 
> This e-mail, including any attachments, is a confidential business
communication, and may contain information that is confidential,
proprietary and/or privileged.  This e-mail is intended only for the
individual(s) to whom it is addressed, and may not be saved, copied,
printed, disclosed or used by anyone else.  If you are not the(an)
intended recipient, please immediately delete this e-mail from your
computer system and notify the sender.  Thank you.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to