You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by CBy <to...@byrman.demon.nl> on 2009/06/17 10:33:45 UTC

Tomcat shutdown problem due to running threads.

O'Reilly's Tomcat The Definitive Guide advises me to invoke the 
setDaemon(true) method on any Thread object a web application creates to 
keep them from hanging the JVM when Tomcat shuts down. My web service, 
however, uses a thread pool that is created via 
java.util.concurrent.Executors.newFixedThreadPool(NTHREADS) and I don't 
know how to make them daemon threads in this case.

My new plan was to register a shutdown hook with the JVM in my web 
service and to invoke shutdown() or shutdownNow() on the ExecutorService 
in it (the method above returns an ExecutorService). Unfortunately, this 
does not seem to work.

Is there another way to be notified when Tomcat shuts down, so I can 
shutdown the thread pool accordingly?

Thanks in advance,

Carsten

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


Re: Tomcat shutdown problem due to running threads.

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Carsten,

On 6/17/2009 2:04 PM, CBy wrote:
> Thank you, Christopher. It appears that I now have to ways to solve my
> problem. Calling shutdown() stops the threads orderly, so I think I'll
> opt for the ContextListener, although I am not 100% sure.

I'd do both if I were you :)

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAko5ODwACgkQ9CaO5/Lv0PCYUgCgpSkcuXg2rFBlfNv+8It9GH34
eB0AoJwUVU7PdQ8dcejZX+FmtupmnwNP
=H87P
-----END PGP SIGNATURE-----

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


Re: Tomcat shutdown problem due to running threads.

Posted by CBy <to...@byrman.demon.nl>.
Thank you, Christopher. It appears that I now have to ways to solve my 
problem. Calling shutdown() stops the threads orderly, so I think I'll 
opt for the ContextListener, although I am not 100% sure.

Christopher Schultz wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Carsten,
>
> On 6/17/2009 4:33 AM, CBy wrote:
>   
>> O'Reilly's Tomcat The Definitive Guide advises me to invoke the
>> setDaemon(true) method on any Thread object a web application creates to
>> keep them from hanging the JVM when Tomcat shuts down. My web service,
>> however, uses a thread pool that is created via
>> java.util.concurrent.Executors.newFixedThreadPool(NTHREADS) and I don't
>> know how to make them daemon threads in this case.
>>     
>
> Can you adjust that code? If so, use the form of that method that takes
> a ThreadFactory object. Something like this ought to do it:
>
> public class DaemonThreadFactory
>     implements ThreadFactory
> {
>    public Thread newThread(Runnable r)
>    {
>       Thread t = new Thread(r);
>       t.setDaemon(true);
>
>       return t;
>    }
> }
>
>   
>> My new plan was to register a shutdown hook with the JVM in my web
>> service and to invoke shutdown() or shutdownNow() on the ExecutorService
>> in it (the method above returns an ExecutorService). Unfortunately, this
>> does not seem to work.
>>     
>
> You should do as André suggests and use a ServletContextListener. You
> should probably use the same listener to both create and teardown the
> thread pool.
>
> I recently had my first experience with Executors in Java. I have to say
> that I love 'em. So simple, yet so powerful.
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAko5KyoACgkQ9CaO5/Lv0PDgcQCfVTfVdv3xUXsEFhh+PYWy9uII
> hpoAn37qoHfLeSVot+FjaYI3XS+8deeH
> =j4WL
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> 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


Re: Tomcat shutdown problem due to running threads.

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Carsten,

On 6/17/2009 4:33 AM, CBy wrote:
> O'Reilly's Tomcat The Definitive Guide advises me to invoke the
> setDaemon(true) method on any Thread object a web application creates to
> keep them from hanging the JVM when Tomcat shuts down. My web service,
> however, uses a thread pool that is created via
> java.util.concurrent.Executors.newFixedThreadPool(NTHREADS) and I don't
> know how to make them daemon threads in this case.

Can you adjust that code? If so, use the form of that method that takes
a ThreadFactory object. Something like this ought to do it:

public class DaemonThreadFactory
    implements ThreadFactory
{
   public Thread newThread(Runnable r)
   {
      Thread t = new Thread(r);
      t.setDaemon(true);

      return t;
   }
}

> My new plan was to register a shutdown hook with the JVM in my web
> service and to invoke shutdown() or shutdownNow() on the ExecutorService
> in it (the method above returns an ExecutorService). Unfortunately, this
> does not seem to work.

You should do as André suggests and use a ServletContextListener. You
should probably use the same listener to both create and teardown the
thread pool.

I recently had my first experience with Executors in Java. I have to say
that I love 'em. So simple, yet so powerful.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAko5KyoACgkQ9CaO5/Lv0PDgcQCfVTfVdv3xUXsEFhh+PYWy9uII
hpoAn37qoHfLeSVot+FjaYI3XS+8deeH
=j4WL
-----END PGP SIGNATURE-----

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


Re: Tomcat shutdown problem due to running threads.

Posted by André Warnier <aw...@ice-sa.com>.
CBy wrote:
> Thanks for pointing me in the right direction André. A 
> ServletContextListener fixed my problem.
> 
My own contribution was minimal, and due mainly to the fact that I am 
eavesdropping on the real Tomcat experts conversations here and 
remembering some things, even if I never used them myself and would not 
recognise one if it appeared in some code.
Fortunately, a Listener is a Tomcat "thing" that has a pretty expressive 
name, and it seemed to fit your request.
Don't ask me how it works though..
;-)

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


Re: Tomcat shutdown problem due to running threads.

Posted by CBy <to...@byrman.demon.nl>.
Thanks for pointing me in the right direction André. A 
ServletContextListener fixed my problem.


André Warnier wrote:
> CBy wrote:
>> O'Reilly's Tomcat The Definitive Guide advises me to invoke the 
>> setDaemon(true) method on any Thread object a web application creates 
>> to keep them from hanging the JVM when Tomcat shuts down. My web 
>> service, however, uses a thread pool that is created via 
>> java.util.concurrent.Executors.newFixedThreadPool(NTHREADS) and I 
>> don't know how to make them daemon threads in this case.
>>
>> My new plan was to register a shutdown hook with the JVM in my web 
>> service and to invoke shutdown() or shutdownNow() on the 
>> ExecutorService in it (the method above returns an ExecutorService). 
>> Unfortunately, this does not seem to work.
>>
>> Is there another way to be notified when Tomcat shuts down, so I can 
>> shutdown the thread pool accordingly?
>>
> From a non-expert (but the expterts are mostly asleep right now) :
> maybe this ?
> http://tomcat.apache.org/tomcat-6.0-doc/config/listeners.html
>
> ---------------------------------------------------------------------
> 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


Re: Tomcat shutdown problem due to running threads.

Posted by André Warnier <aw...@ice-sa.com>.
CBy wrote:
> O'Reilly's Tomcat The Definitive Guide advises me to invoke the 
> setDaemon(true) method on any Thread object a web application creates to 
> keep them from hanging the JVM when Tomcat shuts down. My web service, 
> however, uses a thread pool that is created via 
> java.util.concurrent.Executors.newFixedThreadPool(NTHREADS) and I don't 
> know how to make them daemon threads in this case.
> 
> My new plan was to register a shutdown hook with the JVM in my web 
> service and to invoke shutdown() or shutdownNow() on the ExecutorService 
> in it (the method above returns an ExecutorService). Unfortunately, this 
> does not seem to work.
> 
> Is there another way to be notified when Tomcat shuts down, so I can 
> shutdown the thread pool accordingly?
> 
 From a non-expert (but the expterts are mostly asleep right now) :
maybe this ?
http://tomcat.apache.org/tomcat-6.0-doc/config/listeners.html

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