wood burning stoves 2.0*
The moose likes Servlets and the fly likes ansynchronous request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "ansynchronous request" Watch "ansynchronous request" New topic
Author

ansynchronous request

daman sidhu
Ranch Hand

Joined: Aug 19, 2000
Posts: 184
Hey Guys!
Posting here after a real long time.Well i need your advice.We want to create a asynchronous reporting module where in the JSP will call the servlet for a report to be displayed, the servlet should somehow start a seperate process for calling another servlet which will actually connect to DB and build a html page for the report. Meanwhile the JSP should not time out, the first servlet ought to return the control back to the JSP which should keep polling this servlet for a status(if the report is done) once the second servlet is finished the first servlet should change the status and JSP should then redirect to the html page generated for the report. Hope im able to make sense out of what im trying to say. The thing i want to know is that is there a way we can seperate the incoming request from the JSP-ie the control should goback to the jsp to wait meanwhile the same request object should be passed to the second servlet which will process it. We were thinking of writing a thread which can spawn the second process but that doesnot seem to be a very good idea since this thread would be running in the servlet container. All ideas welcome
Thanks!
Daman
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
I think RequestDispatcher can play an important role in your problem... Have you tried it? I'm not very clear about your problem... Is it like parallel processing on resources? If so, we need to find another approach, rather than just using RequestDispatcher...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Ko,
How will it help? Isnt that synchronous?


Groovy
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Pradeep Bhat:
Ko,
How will it help? Isnt that synchronous?

:roll: I dunnoe, I'm not quite clear with daman sidhu's situation...
daman sidhu
Ranch Hand

Joined: Aug 19, 2000
Posts: 184
hi.. well the situation was that we needed to run a asynchrounous process..normally all the requests to the servlets are synchrounous ie the response is immediate and its a online sort of thing.. we needed the request to start a process and come back later when its done and pick up the result.. we have decided today how to do it- well be starting a seperate thread which will process whatever needs to be done(its basically passing some info to other servlet using URLConnection which will create a report and dump the output back),this thread will be stored in the users session(tho that may not sound very good-but it will work) and the jsp will keep pinging the servlet which will check this thread object-if its done with the processing it can redirect the user to the dumped report output page Will code it and run it and let u guys know if it worked thanks for the suggestions
Daman
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Might I suggest that the best way to add asynchronicity into a process like this is through using JMS? So basically, put a request on a queue inside your servlet, and then have your JSP ping back to the servlet every so often (perhaps using Javascript or a META tag). Check for the response on the response queue -- if it's there, then return the result to the user, if it's not, then put up a message saying "please wait...".
The processing of the request message off of the queue and placing the result on the response queue can be done by a Message-Driven Bean, or through a JMS listener (depending on whether or not you have an EJB container).
This avoids the thread management issue you have, and solves the problem within the bounds of recommended J2EE practices.
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.
daman sidhu
Ranch Hand

Joined: Aug 19, 2000
Posts: 184
Hi Kyle,
First of all thanks for your reply appreciate getting tips from you specially. Well we had considered JMS as an option too cause thats what the specs say should be used for asnychronous behaviour, but later decided on this-the reason being we would have the learning curve for the same plus the guys here want the thing to be done pretty fast.. but ill try and suggest it again to the team and see what happens thanks for the help again
Regards,
Daman
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

If your team knows EJBs, the should know Message Driven Beans. Also I think that the Message Driven Bean implementation isn't that difficult to understand. I know in Apress's Enterprise Javabeans book, they have a really good section on Message Driven Beans and how to create them. It seemed pretty easy to me when reading about it.
I think that this is your best solution.
Now on another idea, why don't you have this Message Driven Bean create the report then e-mail it back to the user, then the user can do anything they want right after sending in the request, even going to a completely unrelated web site.
My 2 cents, from book reading.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Originally posted by daman sidhu:
Hi Kyle,
First of all thanks for your reply appreciate getting tips from you specially. Well we had considered JMS as an option too cause thats what the specs say should be used for asnychronous behaviour, but later decided on this-the reason being we would have the learning curve for the same plus the guys here want the thing to be done pretty fast.. but ill try and suggest it again to the team and see what happens thanks for the help again
Regards,
Daman

You're quite welcome, Daman. BTW, I've been working on the side on an implementation of a servlet and MDB that does this so I can officially write it up as a pattern. Perhaps this is something for December or January's JavaRanch Journal?

Kyle
daman sidhu
Ranch Hand

Joined: Aug 19, 2000
Posts: 184
Wow! That will be cool Kyle, will be looking fwd to that implementation! Mark,thanks for the suggestion, but in the current scenario we have to display the report on the web page itself.We are using a 3rd party reporting tool called web focus to generate reports.This tool has a servlet interface with which we interact to generate reports. So the reports have to be shown on the browser itself,but the user should have the option to surf other links on the site and when the report is done(in the background) it should pop up for him. MDB's may be a good option but then again using EJB's is currently ruled out. Am using the thread implementation only,will post up the code once im done.
Thanks guys!Appreciate the help
Daman
 
jQuery in Action, 2nd edition
 
subject: ansynchronous request
 
Similar Threads
Cancel request mechanism
creating a new thread from servlet
interrupting a servlet
Multiple responses to a request
First struts app help! Drop-down lists