tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andrew Kujtan" <akuj...@evertz.com>
Subject RE: Shutdown Hooks not firing when tomcat is shutdown from within a webapp
Date Wed, 08 Feb 2012 17:09:13 GMT
> -----Original Message-----
> From: Konstantin Kolinko [mailto:knst.kolinko@gmail.com]
> Sent: Tuesday, February 07, 2012 7:31 PM
> To: Tomcat Users List
> Subject: Re: Shutdown Hooks not firing when tomcat is shutdown from
> within a webapp
> 
> 2012/2/7 Andrew Kujtan <akujtan@evertz.com>:
> > OS: Windows 7  32bit
> >
> > Ver.: Apache Tomcat/7.0.25
> >
> > Jvm: 1.6.0_14-b08
> >
> >
> >
> > I am attaching a shutdown hook from within my webapp to log some info
> > after tomcat shuts down,
> >
> >
> >    > Runtime.getRuntime().addShutdownHook(mythread, name +
> > ":shutdownHook"));
> >
> 
> 1. Webapp classloader will be stopped when webapp stops. It happens
> long before the hook runs. It would not be able to load any classes.
> (and it will keep your web application classes in memory, which is
> known as PermGen memory leak).
> 
> 2. The contextClassLoader of your thread will reference Webapp
> classloader, unless you explicitly set it to null.
> 
> Tomcat has some logic to detect and warn about some PermGen memory
> leak patterns. You might have seen those warnings. It is better to
> honour them seriously.
> 
> 
> From all of the above adding a shutdown hook from within a web
> application is a bad idea.  The hook classes must be in
> ${catalina.base}/lib folder and you can add <Listener> to
> conf/server.xml to manage the hook.
> 
> (Or better just replace the hook with a Listener).
> 

The hook classes are indeed in the lib folder. Could you elaborate on how I would add a listener
to manage the hook? I can't find anything in the documentation about it... 

I put this into server.xml,
<server>
...
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>

Then what?

> >
> > When I run shutdown.bat from the bin folder the hook runs fine and I
> get
> > my output.
> >
> >
> >
> > In some cases however I am forced to shut down tomcat from within the
> > webapp, in this case it calls,
> >
> >
> >
> >    > org.apache.catalina.startup.Bootstrap.main(new String[] { "stop"
> > });
> >
> 
> or you can get a reference to Context, than walk up its parents chain,
> and call Engine.getService().getServer().stop().  I think its
> destroy() method will be called automatically when await thread stops.
> 
> Anyway calling Bootstrap looks better than calling System.exit() that
> someone suggested here.  When Tomcat is run as a service in Windows
> you really must not use System.exit() as it will cause problems for
> the service launcher.
> 
> You can also call stop() method through JMX.
> 
> >
> >
> > Which shuts down tomcat, but in this case the shutdown hook does not
> get
> > fired.
> >
> >
> >
> > Can anyone tell me how to programmatically shut down tomcat such that
> > the hooks will still fire?
> >
> 
> As Javadoc for shutdown hooks says, when there are several hooks, they
> all will be started at the same time and then run concurrently. The
> order between them is random.

Implying that none of the shutdown hooks are running when I am calling Bootstrap.stop?

> 
> Note that the standard hook set by Tomcat shuts down the logging
> framework. So if you try to log anything it is likely to be wasted.
> 

I changed my hook and make it start a new process (calling calc.exe) and calc only gets called
when I run shutdown.bat, not when I stop from bootstrap. So I know it is not running.

> >
> > What is the difference between running shutdown.bat and calling stop
> > programmatically?
> >
> >
> 
> >  java.lang.Thread.State: WAITING (on object monitor)
> >       at java.lang.Object.wait(Native Method)
> >       - waiting on <0x08bc3ac0> (a
> org.apache.catalina.startup.Catalina$CatalinaShutdownHook)
> >       at java.lang.Thread.join(Thread.java:1143)
> 
> Thread.join().
> 
> It looks like the calling thread (the one that called System.exit())
> just waits until the target thread (CatalinaShutdownHook) finishes.
> 
> You should look in the logs to see what is printed there with regards
> to the server shutdown sequence. Maybe it waits while some server
> component shuts down?
> 

Any place specifically?

When I set org.apache.catalina.level = FINE, all I get is,

Feb 8, 2012 12:01:03 PM com.evertz.web.worker.ShutdownManager shutDown
INFO: Calling the tomcat shutdown action.
Feb 8, 2012 12:01:03 PM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service Catalina

> Best regards,
> Konstantin Kolinko
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message