Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

servlet with a worker thread  RSS feed

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

I don't know whether to post this here or in the multi-threading section so I'm starting here
I have a servlet that is the backend for a site where people enter zipcodes as part of some data they're entering
this data goes in a database
for each zipcode I want to go to one of the weather sites (weather.com) and download a lot of weather data for that zipcode
it's a time consuming process so in my init() function I set up a Timer which periodically fires off a thread to go do this background work
when I run the servlet in a debugger (I'm using Netbeans) everything works fine, but when I stop debugging the servlet the worker thread keeps running
so, I guess my question is this

if you have a servlet that requires a bunch of "house cleaning" chores (getting weather data) to be done periodically, what's the best way to achieve this?

Dave
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
init() is definitely not the right place for this kind of work. You can go like, your handler spin a new thread and forward the original request back to the user and show up some "in progress ... " kind of page. Meanwhile your browser will keep looking whether its done and ready or not after every given time. Then show the response page to the user when done.

You can search the same forum with the words "in progress", "show progress", "progress bar" and alike.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make the thread as deamon by using the following , in that case as soon as the main thread exits , your background thread would also die and will solve your issue.

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