On Tue, 19 Mar 2002, David Shanahan wrote:

> Date: Tue, 19 Mar 2002 21:05:11 +0900
> From: David Shanahan <[EMAIL PROTECTED]>
> Reply-To: Tomcat Users List <[EMAIL PROTECTED]>,
>      David Shanahan <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED]
> 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:   <mailto:[EMAIL PROTECTED]>
For additional commands: <mailto:[EMAIL PROTECTED]>
Troubles with the list: <mailto:[EMAIL PROTECTED]>

Reply via email to