Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

new threads for background processing  RSS feed

 
dean tomlinson
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dean tomlinson
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!