RE: Running a thread from a JSP
-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
Re: Running a thread from a JSP
Depending on what you're trying to do, it might also be useful to run the back-end operation in a hidden frame. You can let a process run that way and use JavaScript to notify the user when it has completed. - Original Message - From: William Brogden [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, December 25, 2000 11:47 PM Subject: Re: Running a thread from a JSP "David M. Holmes" wrote: Is there a way to spawn a thread from a JSP that will live after the page goes out of scope? There is nothing magic about a JSP servlet - you can certainly create objects and start Threads that will continue after the response has been sent. -- 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
Re: Running a thread from a JSP
But when the page finishes and there is no reference to the threaded object, won't the thread die? --- William Brogden [EMAIL PROTECTED] wrote: "David M. Holmes" wrote: Is there a way to spawn a thread from a JSP that will live after the page goes out of scope? There is nothing magic about a JSP servlet - you can certainly create objects and start Threads that will continue after the response has been sent. -- 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 . JSP ISBN 0-7821-2809-2 . __ Do You Yahoo!? Yahoo! Shopping - Thousands of Stores. Millions of Products. http://shopping.yahoo.com/
Re: Running a thread from a JSP
What is it that you are actually trying to do? (The end to which keeping this thread alive is a means?) *** REPLY SEPARATOR *** On 12/26/2000 at 10:54 AM David M. Holmes wrote: But when the page finishes and there is no reference to the threaded object, won't the thread die? --- William Brogden [EMAIL PROTECTED] wrote: "David M. Holmes" wrote: Is there a way to spawn a thread from a JSP that will live after the page goes out of scope? There is nothing magic about a JSP servlet - you can certainly create objects and start Threads that will continue after the response has been sent. -- 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 . JSP ISBN 0-7821-2809-2 . __ Do You Yahoo!? Yahoo! Shopping - Thousands of Stores. Millions of Products. http://shopping.yahoo.com/
Re: Running a thread from a JSP
"David M. Holmes" wrote: But when the page finishes and there is no reference to the threaded object, won't the thread die? Well, the thread _has_ to be referenced somewhere internally - how else should the JVM be able to switch between them? :) Don't worry, it won't just die... -- Kurt Bernhard Pruenner --- Haendelstrasse 17 --- 4020 Linz --- Austria Music: http://www.mp3.com/Leak --- Work: http://www.ssw.uni-linz.ac.at ...It might be written "Mindfuck", but it's spelt "L-A-I-N"... np: Ko-Wreck Technique - Metro Dade (Rapid Transit comp.)
Re: Running a thread from a JSP
But when the page finishes and there is no reference to the threaded object, won't the thread die The thread dies when the run methods exits. It has nothing to do with references. The thread object will be garbage collected when there are no more references and the run method has exited Filip - Original Message - From: "Ted Husted" [EMAIL PROTECTED] To: "Tomcat User List" [EMAIL PROTECTED] Sent: Tuesday, December 26, 2000 11:06 AM Subject: Re: Running a thread from a JSP What is it that you are actually trying to do? (The end to which keeping this thread alive is a means?) *** REPLY SEPARATOR *** On 12/26/2000 at 10:54 AM David M. Holmes wrote: But when the page finishes and there is no reference to the threaded object, won't the thread die? --- William Brogden [EMAIL PROTECTED] wrote: "David M. Holmes" wrote: Is there a way to spawn a thread from a JSP that will live after the page goes out of scope? There is nothing magic about a JSP servlet - you can certainly create objects and start Threads that will continue after the response has been sent. -- 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 . JSP ISBN 0-7821-2809-2 . __ Do You Yahoo!? Yahoo! Shopping - Thousands of Stores. Millions of Products. http://shopping.yahoo.com/
Re: Running a thread from a JSP
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? --- Ted Husted [EMAIL PROTECTED] wrote: What is it that you are actually trying to do? (The end to which keeping this thread alive is a means?) *** REPLY SEPARATOR *** On 12/26/2000 at 10:54 AM David M. Holmes wrote: But when the page finishes and there is no reference to the threaded object, won't the thread die? --- William Brogden [EMAIL PROTECTED] wrote: "David M. Holmes" wrote: Is there a way to spawn a thread from a JSP that will live after the page goes out of scope? There is nothing magic about a JSP servlet - you can certainly create objects and start Threads that will continue after the response has been sent. -- 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 . JSP ISBN 0-7821-2809-2 . __ Do You Yahoo!? Yahoo! Shopping - Thousands of Stores. Millions of Products. http://shopping.yahoo.com/ __ Do You Yahoo!? Yahoo! Shopping - Thousands of Stores. Millions of Products. http://shopping.yahoo.com/
Re: Running a thread from a JSP
Thank you! --- Filip Hanik [EMAIL PROTECTED] wrote: But when the page finishes and there is no reference to the threaded object, won't the thread die The thread dies when the run methods exits. It has nothing to do with references. The thread object will be garbage collected when there are no more references and the run method has exited Filip - Original Message - From: "Ted Husted" [EMAIL PROTECTED] To: "Tomcat User List" [EMAIL PROTECTED] Sent: Tuesday, December 26, 2000 11:06 AM Subject: Re: Running a thread from a JSP What is it that you are actually trying to do? (The end to which keeping this thread alive is a means?) *** REPLY SEPARATOR *** On 12/26/2000 at 10:54 AM David M. Holmes wrote: But when the page finishes and there is no reference to the threaded object, won't the thread die? --- William Brogden [EMAIL PROTECTED] wrote: "David M. Holmes" wrote: Is there a way to spawn a thread from a JSP that will live after the page goes out of scope? There is nothing magic about a JSP servlet - you can certainly create objects and start Threads that will continue after the response has been sent. -- 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 . JSP ISBN 0-7821-2809-2 . __ Do You Yahoo!? Yahoo! Shopping - Thousands of Stores. Millions of Products. http://shopping.yahoo.com/ __ Do You Yahoo!? Yahoo! Shopping - Thousands of Stores. Millions of Products. http://shopping.yahoo.com/
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. -- 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
Re: Running a thread from a JSP
"David M. Holmes" wrote: Is there a way to spawn a thread from a JSP that will live after the page goes out of scope? There is nothing magic about a JSP servlet - you can certainly create objects and start Threads that will continue after the response has been sent. -- 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