• Post Reply Bookmark Topic Watch Topic
  • New Topic

asynchronous processing in a servlet  RSS feed

 
Jon Wynett
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:

I have an application running in an OC4J application server. My JSP calls into an action servlet to perform a particular task and then redirects to a results JSP.

The issue I have is that the task to be done is very long and I would like to start it asynchronously from the servlet and have the results JSP retrieve data as it becomes available.

Not being particularly experienced with servlets, my main idea is to make a JMS call to start my async task and then I can redirect to my results page and wait for data.

I'm wondering if anyone has ideas on other ways to do this that may be better.

Jon
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's how I handle long running processes.
The status page refreshes every n seconds.
 
Thomas Hubschman
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there,

I had to do the same thing a couple of projects ago. What I adopted was the "Orbitz" model. In my case we were not using JMS but manually handling it via java threading. Here are the steps:

1. Process initial request
2. Spawn thread to do long task
3. Forward user to page with flashing/filling/rotating 'processing' image.
4. 'Processing' page has javascript function to ping the server every 2 seconds to see in the thread has finished. If it is unfinished it continues to flash.
5. Eventually the thread completes processing and sets a flag on a servlet/EJB.
6. The 'Processing' page then sees the data is ready and the servlet forwards the request to a results page.

I hope that helps.

Tom
 
Jon Wynett
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Those ideas seem to confirm what I was thinking.
 
Stan Sokolov
Ranch Hand
Posts: 120
Hibernate IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have database it could be also possible to have some table that stores "jobs list". So once your servlet is called it generate job id and put new record in the list of not processed jobs. Then it returns to user information about job id and probably the time when user should expect job completed (it could be determined as avarage processing time multiplied by number of unprocessed jobs). Then another task that works in batch mode will process this job and set some mark. Using this approach you will have two level operation that probably looks more similar how such kind of operations are performed in the real life.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!