You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by David Shanahan <da...@biosjp.com> on 2002/03/19 13:05:11 UTC

Creating threads from a servlet

Is it against the servlet specification to create threads
from inside a servlet ?

I am using java.util.Timer & java.util.TimerTask to initiate
some background work (sending email) from a servlet. The 
java.util.Timer class implicitly creates a background thread.

Right now this is working fine but I wonder if I am going 
to run into trouble with future versions of tomcat.

If creating threads is against the spec. is there a recommended
way of performing background tasks.

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Creating threads from a servlet

Posted by Bo Xu <bo...@cybershop.ca>.
----- Original Message ----- 
From: "Anthony Eden" <ae...@signaturedomains.com>
To: "'Tomcat Users List'" <to...@jakarta.apache.org>
Sent: Tuesday, March 19, 2002 12:45 PM
Subject: RE: Creating threads from a servlet


> 
> 
> > -----Original Message-----
> > From: Craig R. McClanahan [mailto:craigmcc@apache.org] 
> > Sent: Tuesday, March 19, 2002 12:15 PM
> > To: Tomcat Users List; David Shanahan
> > Subject: Re: Creating threads from a servlet
> > 
> 
> <snip>
> 
> > 
> > A couple of notes about using your own threads in servlet containers:
> > 
> > - You must properly clean up your threads when the application
> >   is shut down.  If you don't, you are very likely to cause
> >   Tomcat to hang at shutdown time.
> 
> What exactly do you mean by "properly clean up your threads"?  I believe
> I am seeing this behavior in some of my systems and I am interested in
> fixing it.  Does this mean that the application must listen for some
> sort of shutdown notification?
> 
> Sincerely,
> Anthony Eden
>[...]


good emaol for you! :-)

Bo
Mar192002


> Hi all-
>
> I have a web application that uses some static resources to make them
> available to any class in the application.  Amongst these static
> resources is a home-brewed JDBC connection pool.  What I'm seeing is
> that with Tomcat 4, when I stop/start or reload an application from the
> manager app, it isn't freeing up these static resources.  I can see this
> because my memory utilization goes up about 5 megs every time I restart,
> whereas if I stop and restart the whole tomcat process, my memory stays
> the same.  Also the database starts yelling at me about too many
> applications simultaneously etc.
>
> How can I tell Tomcat to clean up these resources when it stops the
> application? What hook/mechanism is there?  Is there some sort of
> listener interface I can implement, similar in idea to an
> HttpSessionBindingEvent, to let me know when an application is shutting
> down so I can close all of the database connections?
>
> None of this was an issue in Tomcat 3.x because I had no choice but to
> shut down the entire tomcat instance!
>
> Any help greatly appreciated!  Thanks!
>
> Best regards,
>
> Richard
>

Doing this is exactly what the new application event listeners APIs (in
Servlet 2.3) are designed for.  You need to:

* Create a class that implements javax.servlet.ServletContextListener

* Register it (in web.xml> with a <listener> element

* In the contextInitialized() method, set up your connection pool

* In the contextDestroyed() method, gracefully clean up your
  connection pool.

Craig



--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


RE: Creating threads from a servlet

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Tue, 19 Mar 2002, Anthony Eden wrote:

> Date: Tue, 19 Mar 2002 12:45:12 -0500
> From: Anthony Eden <ae...@signaturedomains.com>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> To: 'Tomcat Users List' <to...@jakarta.apache.org>
> Subject: RE: Creating threads from a servlet
>
>
>
> > -----Original Message-----
> > From: Craig R. McClanahan [mailto:craigmcc@apache.org]
> > Sent: Tuesday, March 19, 2002 12:15 PM
> > To: Tomcat Users List; David Shanahan
> > Subject: Re: Creating threads from a servlet
> >
>
> <snip>
>
> >
> > A couple of notes about using your own threads in servlet containers:
> >
> > - You must properly clean up your threads when the application
> >   is shut down.  If you don't, you are very likely to cause
> >   Tomcat to hang at shutdown time.
>
> What exactly do you mean by "properly clean up your threads"?  I believe
> I am seeing this behavior in some of my systems and I am interested in
> fixing it.  Does this mean that the application must listen for some
> sort of shutdown notification?
>

If you start a thread but never stop it, Tomcat shutdown will hang (unless
the thread was started as a daemon thread).  Somewhere in the shutdown
code of your application, it should stop all the background threads that
were started.

> Sincerely,
> Anthony Eden
>

Craig


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


RE: Creating threads from a servlet

Posted by Anthony Eden <ae...@signaturedomains.com>.

> -----Original Message-----
> From: Craig R. McClanahan [mailto:craigmcc@apache.org] 
> Sent: Tuesday, March 19, 2002 12:15 PM
> To: Tomcat Users List; David Shanahan
> Subject: Re: Creating threads from a servlet
> 

<snip>

> 
> A couple of notes about using your own threads in servlet containers:
> 
> - You must properly clean up your threads when the application
>   is shut down.  If you don't, you are very likely to cause
>   Tomcat to hang at shutdown time.

What exactly do you mean by "properly clean up your threads"?  I believe
I am seeing this behavior in some of my systems and I am interested in
fixing it.  Does this mean that the application must listen for some
sort of shutdown notification?

Sincerely,
Anthony Eden


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Creating threads from a servlet

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Tue, 19 Mar 2002, David Shanahan wrote:

> Date: Tue, 19 Mar 2002 21:05:11 +0900
> From: David Shanahan <da...@biosjp.com>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>,
>      David Shanahan <da...@lincmedia.co.jp>
> To: tomcat-user@jakarta.apache.org
> Subject: Creating threads from a servlet
>
>
> Is it against the servlet specification to create threads
> from inside a servlet ?
>

In a J2EE application server, it is against the rules to create threads in
application components.

In Tomcat, whether it's against the rules or not depends on whether you
are running under a security manager or not.  If you are, it's possible
that the security policies will prohibit thread creation.  But that is up
to whoever sets up your security policies.

A couple of notes about using your own threads in servlet containers:

- You must properly clean up your threads when the application
  is shut down.  If you don't, you are very likely to cause
  Tomcat to hang at shutdown time.

- It is not legal to reference the request and response objects
  passed to your servlet outside the boudns of the service() method
  of that servlet.  Therefore, you will need to copy anything you
  need out of the request before using it in another thread.

- For threads running requests, Tomcat sets the thread context class
  loader (Thread.currentThread().getContextClassLoader()) to point at
  the webapp class loader for that web application.  Among other things,
  this gives your code access to the classes in /WEB-INF/classes and
  /WEB-INF/lib even if a library class is loaded from somewhere else.
  However, this service is *not* provided for application threads.

> I am using java.util.Timer & java.util.TimerTask to initiate
> some background work (sending email) from a servlet. The
> java.util.Timer class implicitly creates a background thread.
>
> Right now this is working fine but I wonder if I am going
> to run into trouble with future versions of tomcat.
>
> If creating threads is against the spec. is there a recommended
> way of performing background tasks.
>

Craig


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>