• Post Reply Bookmark Topic Watch Topic
  • New Topic

worker threads in servlet

 
Dave Robbins
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I have a servlet that processes some form data
for each set of data submited, I want to go out to the weatherunderground site and download a lot of historical weather data
this take a couple of minutes
what is the proper way to do this?
so far I've built a class that implements Runnable and I launch this to go do the work right after processing the form data in the servlet
but the servlet seems to hang while the class is doing it's work (not what I expect)
needless to say I'm doing something wrong
is this basically the right approach?
if I want a servlet to launch some kind of background job, how should this be done?

Dave
 
Shu Gao
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Dave
Servlets are bascially single-threaded that they process your requests in a single-thread, so when you do something in doGet(), doPost(), well...uh service(), everything is synchronized.

That's the traditional REQUEST-WAIT-RESPONSE model that you have to wait for the servlet to complete all of its processing before it can return any responses.

If you use a background worker thread, it would no be appropriate in this case, cause that way, you won't get a chance for a servlet to return the response, since the servlet is already returned the response while your background worker is still hanging and doing its job.

I can give you two suggestions for this problem since your business logic in this case seem to be time-consuming:

1) you can cache the data somewhere so that it would be fast enough for your servlets to fetch. However, I understand that you need to pass in some parameters before you retrieve the data.

2) You can consider AJAX whereas it's not synchronized, while you can send a request to the server and the servlet will take its time to fetch the data it requires, but your browser will not be hanged, it can resume doing its job, so when the servlet finishes processing, it'll give the browser the result in xml format.
 
Dave Robbins
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shu,

thanks
you've answered my question
I have the code written to do what I want to do, I guess I need to figure out a different way to call it
can I create something like a timer (as part of the servlet) that will call this code every hour or so and do this background work?

Dave
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!