*
The moose likes Servlets and the fly likes Wanted: Design pattern for handling time consuming HTTP requests Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Wanted: Design pattern for handling time consuming HTTP requests " Watch "Wanted: Design pattern for handling time consuming HTTP requests " New topic
Author

Wanted: Design pattern for handling time consuming HTTP requests

Johan Apelgren
Greenhorn

Joined: Jan 24, 2002
Posts: 6
Hi!
In my application the user will have to wait for the server to execute requests that sometimes will take more than 10 secs to perform. During this time, I would like the users to be able to continue with certain non-sensitive operations. When to request is done, the web application should somehow notify the user and present the result to her/him.
Has anybody a good implementation of this feature?
PS. We use WebSphere 3.5 and IBM HTTP Server.
//Johan
chanoch wiggers
Author
Ranch Hand

Joined: May 24, 2001
Posts: 245
first store a result object - an object that will hold the result of the operation - in session. This will act as a flagto the status.
spin off a thread that takes care of the time consuming operation and pass that thread the user's session (and therefore the result object0 to store the result in.
Now if the operation is literaly only 10secs, then I personally dont think that there is any point in allowing the user to continue using non-sensitive parts of the web app - what can someone do in 10s? just show a hold page, with a meta refresh tag of 10secs (and/or a javascript function that does the same plus a link in case neither works - a very unlikely situation) and put a gif of a moving clock or some such thing to show elapsed time. FOr an example see virgintrains.co.uk (search for a journey - for example hull to manchester)
if it does make sense to carry on the processing in parallel for some reason, a possible solution is to provide a template in each page that displays the state of any outstanding work belonging to the user by checking the session for the result object and checking its state. If no result object exists then nothing is shown, otherwise its state is shown together with a link to a page that can extract the result object from session and display the result:

[ December 05, 2002: Message edited by: chanoch wiggers ]

chanoch<p><a href="http://www.amazon.com/exec/obidos/ASIN/1861007736/" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
Malhar Barai
Author
Ranch Hand

Joined: Aug 17, 2001
Posts: 399
Originally posted by chanoch wiggers:
first store a result object - an object that will hold the result of the operation - in session. This will act as a flagto the status.
spin off a thread that takes care of the time consuming operation and pass that thread the user's session (and therefore the result object0 to store the result in.
Now if the operation is literaly only 10secs, then I personally dont think that there is any point in allowing the user to continue using non-sensitive parts of the web app - what can someone do in 10s? just show a hold page, with a meta refresh tag of 10secs (and/or a javascript function that does the same plus a link in case neither works - a very unlikely situation) and put a gif of a moving clock or some such thing to show elapsed time. FOr an example see virgintrains.co.uk (search for a journey - for example hull to manchester)


if it does make sense to carry on the processing in parallel for some reason, a possible solution is to provide a template in each page that displays the state of any outstanding work belonging to the user by checking the session for the result object and checking its state. If no result object exists then nothing is shown, otherwise its state is shown together with a link to a page that can extract the result object from session and display the result:

[ December 05, 2002: Message edited by: chanoch wiggers ]

hello dear
can you explain with bit more details, my operation last for about 20-25 secs & then shows the result, sometimes even more than this due to ISP's speed problem
tia
MB
Johan Apelgren
Greenhorn

Joined: Jan 24, 2002
Posts: 6
Thank's!
This sounds like a well designed solution. I have two comments:
1) You wrote:

"spin off a thread that takes care of the time consuming operation and pass that thread the user's session (and therefore the result object0 to store the result in."
Is this a safe operation to perform within a J2EE application server and/or a web server? I thought threading was one of the things the appserver ought to handle automatically?
2) You also wrote:
"a possible solution is to provide a template in each page that displays the state of any outstanding work belonging to the user by checking the session for the result object and checking its state. If no result object exists then nothing is shown, otherwise its state is shown together with a link to a page that can extract the result object from session and display the result"
Our application is a "fat" web client. That is, it is implemented with payloads of DIV tags and Javascripting, and only access the web server when it's time for bulk operations (+10 secs) that I mentioned in my previous posting.
I mention this because I think this template solution would work just fine if the application make a HTTP request to the web server every time the user "clicks" in the GUI. But for us the "template" would have to check the session in a hidden form or something. Could this be done?
Are there better alternatives, since I have got the impression that hidden forms are "dirty" design?
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
If you have MQSeries available to you, then you can avoid spinning off a thread by:
(1) Making your "handler" that executes the long business process run as a JMS MessageListener.
(2) Phrase the "Request" to the handler as putting a message on a queue
(3) Have the Servlet that is polled for the result by the client check a reply queue to see if a reply has been returned.
This is actually easiest in something like WebSphere 5.0, which has JMS built in, but if you have MQ Series, it's easy (and fully J2EE compatible) to build. This way of accomplishing long-running processes is actually one of IBM's best practices for WAS 5.0.
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wanted: Design pattern for handling time consuming HTTP requests