> -----Original Message-----
> From: William Brogden [SMTP:[EMAIL PROTECTED]]
> Sent: Tuesday, December 26, 2000 8:54 PM
> To: [EMAIL PROTECTED]
> Subject: Re: Running a thread from a JSP
>
>
>
> "David M. Holmes" wrote:
> >
> > We have a custom tag that is in every JSP that creates a socket
> connection to a target server and
> > POSTs some data do a page on the target server that writes the data to a
> database. The issue we
> > are having is that we don't want to delay the user's response by doing
> the socket connection
> > synchronously. If we do the socket connection asynchronously we risk
> having the data not being
> > sent when the server is under stress. I have not verified that this is
> occuring with the custom
> > tag yet, but we are seeing it happen using ASP with an HTTP component. I
> am pretty sure that the
> > same condition will occurr. So I would like for the JSP to start a
> thread to do the socket
> > connection, run in the background, and return the response immediately
> so there is no delay to the
> > user. I am thinking that when the response finishes the thread that it
> started will also die. Is
> > that correct?
>
> That should work just fine - I have done something very
> similar to send email. You might want to provide for logging
> the data to a file if the socket connection fails.
> When the job is done, the Thread falls out of the run
> method and dies - all very neat.
[Kitching Simon]
I have an alternative solution to suggest.
Create a class which is responsible for the data
transmission you need to do. Have this class inherit
from Thread (or implementing Runnable, whichever you prefer),
and have it maintain an input queue of messages to send.
You can then instantiate a *single* instance of this class,
start the thread, and put it in the application context as part
of your webapp startup (alternative: make it a singleton).
Your jsp pages then just place a message on this object's
input queue and return. The class's thread (using the standard
producer/consumer architecture you can find in any decent
java textbook) takes the message from the queue and
posts it off asynchronously, as you wanted.
Somehow, this seems cleaner to me than the approach you
described above. While I think you *can* spawn threads from
a jsp page, it just doesn't seem nice to me.
Just one point about your email:
I am thinking that when the response finishes the thread that it
started will also die. Is
> that correct?
no, this is not correct. If you start a thread, it will continue
running regardless of what
its parent thread does. The Thread *class* contains references to
all active threads, so
even if the only reference to a thread from *your* code gets
deleted, the thread does
not get shut down & garbage collected. The only way a thread ends is
if it returns
from its starting method, either deliberately, or as a result of an
exception propagating
out of the starting method.
Regards,
Simon
> --
> WBB - [EMAIL PROTECTED]
> Java Cert mock exams http://www.lanw.com/java/javacert/
> Author of Java Developer's Guide to Servlets and JSP
> ISBN 0-7821-2809-2