• Post Reply Bookmark Topic Watch Topic
  • New Topic

auto call to servlet

 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone help me with ideas about how to get servlet to run automatically at same time everyday? would the code need to be in some kind of thread? declaration? what?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A servlet is probably a really poor choice for whatever it is you are trying to do. They are designed to execute in response to a stimulus from a client and return a response. Background processing would be better suited to a daemon process that either schedules itself or is trigged by cron.
 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet that gets info from some web site following an event from a jsp.the info is saved to a bean then to DB. I just need for this to happen without me having to trigger the event. pls let me know more about this daemon and cron stuff. sure there isn't some other more straightfoward way
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really need to "hit" a servlet at regular intervals you could look into using wget at the command line in a shell script triggered as a cron job.

Unless of course you're on Windows which doesn't have cron.
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Bear Bibeault suggested you can use a wget command hit your servlet at a regular interval with a cron job on unix/scheduled job on windows.


WGET

wget should be the easier solution, but if you need more control and flexibility then

you can also write your own Java program using JAVA's TimerTask and Timer API to write a simple scheduler, which uses HttpURLConnection class to connect to your servlet. [JMX can also be used for this purpose, if you want change the time dynamically through server console]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might be a good time to factor your database handling and business logic out of the servlet and into a bean.
Then, that logic can be called, either from the servlet or from the timer object.
 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks so much guys for all the feedback. think i'm going to go with the java.util.Timer instead. seems to offer more flexibility. plus i'm unfortunately developing on windows... so that could make the whole cron thing more complicated than I can handle right now!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!