This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Servlets and the fly likes new threads for background processing Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "new threads for background processing" Watch "new threads for background processing" New topic
Author

new threads for background processing

dean tomlinson
Ranch Hand

Joined: Jan 31, 2002
Posts: 94
dear guys,
i don't think there is any way of renaming a post, so i created a new one whichh more accurately describes my question.
my previous post was titled "sending html to the browser whilst an intensive operation proceeds ".
my question is now...
is it possible to start a new thread to handle the retrievel of the data. This time consuming process can then get underway, whilst the request is forwarded to the "Data Loading" jsp, that would have a javascript onload function that requests the final jsp. The final jsp, has a method at the top of the page, that blocks until the data retrieval has completed.
i just dont want the intermediary page to add to the time it takes for the user to see their results page.
with this approach there would be 1 additional thread for every concurrent user, so i would need to associate the session id with the thread and the data object instance it is populating.
it seems to be common to use additional threads when programming other java applications, but i haven't really heard much about it with gregards to java web development. is there any fundamental reasons why it is not mentioned in books etc ?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14568
    
    7

If you block a JSP, you're at risk for stalling the entire server or at least important parts of it.
HTTP isn't like client/server. HTTP is transaction-oriented. You want to get in and back out as quickly as possible. Among other things, each client doesn't get their own server-side processing thread that they retain between transactions. The reason there's nothing written on the subject is because it's simply not safe to do.
There ARE thread things you CAN safely do. For example, it's not uncommon to spawn a back-end "engine" off some servlet's init() method then pass long-running process requests to it. A JSP or servlet can then QUERY that thread for completion and send back a "Please Wait" page if processing hasn't yet completed. "Please Wait" pages frequently have timed refresh so that they'll cause the server to be polled (and thus in turn to poll the backend engine) every few seconds until there are results available to be returned and displayed.


Customer surveys are for companies who didn't pay proper attention to begin with.
dean tomlinson
Ranch Hand

Joined: Jan 31, 2002
Posts: 94
thanks a lot tim, your advise is greatly appreciated, but i wondered if i could glean a bit more info on your suggestion beneath
it's not uncommon to spawn a back-end "engine" off some servlet's init() method then pass long-running process requests to it. A JSP or servlet can then QUERY that thread for completion and send back a "Please Wait" page if processing hasn't yet completed.

how would the backend engine (thread) handle the data related to seperate users ?
is it possible to use observer/oberverable to notify the servlet/jsp that the data has been retrieved so it can then begin to render the page?
you say that a method blocking can crash the webserver. basically what i meant here was...
at the top of the jsp is a method call such as retrieveData(). inside here it references the backend engine that you talked about, and queries it for completion. so instead of keep reloading the page and checking the backend engine, we are looping within the retrieveData, each time checking if the data retrieval has completed.again. Is this is any different ?
Thanks again - I really appreciate your advise
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14568
    
    7

Originally posted by dean tomlinson:
thanks a lot tim, your advise is greatly appreciated, but i wondered if i could glean a bit more info on your suggestion beneath

how would the backend engine (thread) handle the data related to seperate users ?


The requester has to pass that information in - the backend engine itself can't distinguish.

is it possible to use observer/oberverable to notify the servlet/jsp that the data has been retrieved so it can then begin to render the page?

A servlet is not some sort of process that can be notified. It's better to think of it as a subroutine of the client that gets called. And MUST return befor the user's browser times out.

you say that a method blocking can crash the webserver. basically what i meant here was...
at the top of the jsp is a method call such as retrieveData(). inside here it references the backend engine that you talked about, and queries it for completion. so instead of keep reloading the page and checking the backend engine, we are looping within the retrieveData, each time checking if the data retrieval has completed.again. Is this is any different ?

Nope. An indirect block is still a block. Actually it's not so likely the server will crash as it will jam, since other people's (possibly unrelated) requests can't proceed while you're waiting.

Thanks again - I really appreciate your advise

No problem!
[ May 03, 2002: Message edited by: Tim Holloway ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: new threads for background processing
 
Similar Threads
request time out
application development
creating a new thread from servlet
sending html to the browser whilst an intensive operation proceeds
Problem with Java 2 SCJP Certification Study Guide???