tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Konstantin Kolinko <>
Subject Re: Shutdown Hooks not firing when tomcat is shutdown from within a webapp
Date Wed, 08 Feb 2012 00:31:07 GMT
2012/2/7 Andrew Kujtan <>:
> 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).

> 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.

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.

> 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(


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?

Best regards,
Konstantin Kolinko

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message