Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Perform timed execution

 
Joshua Elkino
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is there any way to perform an action at a timed interval? I need to do a db call every so often and then perform necessary java commands based on the condition. Is there a static servlet that runs only on the local machine that never returns?

Thanks!
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets are the wrong choice for this sort of behaviour, since they are designed to respond to HTTP requests. Have a look at Timers and TimerTasks in the standard API.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anything in a web app is poorly suited for this. Why not just write a separate daemon program that does this?
 
Joshua Elkino
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just thought it would be easier because i've integrated my app with hibernate and struts and thus all the configuration pieces are already there and even the code code that i want to run on an interval is 90% there.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilya Elkind:
....Is there a static servlet that runs only on the local machine that never returns?


If it is static, then it is not servlet it is applet.
 
Chaitanya Amol Soman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilya,

I support the advice posted by Paul & Bear.

However, this is how you can do what you want to -

1) Write a thread that sleeps for an interval of time and polls the database to do whatever business functionality that you want and then goes back to sleep.
2) Write a servlet and in its public void init() method, start the above thread.
3) In the web.xml, for the above servlet set the <load-on-startup>1</load-on-startup> property.
4) Do not create a URL Mapping for the servlet.

These steps will ensure that your thread is spawned the moment you deploy your web application and will continue till you either bring down the web server or stop your application.
Also, by not providing a URL mapping, that servlet will not be accesible to the world.

Hope that helps
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If it is static, then it is not servlet it is applet.

Well, to be pedantic, if it runs in a browser then its an Applet. If it is stand alone, then its an application.


However, this is how you can do what you want to -

1) Write a thread that sleeps for an interval of time and polls the database to do whatever business functionality that you want and then goes back to sleep.
2) Write a servlet and in its public void init() method, start the above thread.
3) In the web.xml, for the above servlet set the <load-on-startup>1</load-on-startup> property.
4) Do not create a URL Mapping for the servlet.

If its Struts being used, don't bother with another Servlet. Have a look at other mechanisms, such as a ServletContextListener, to kick off your thread.
 
Joshua Elkino
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your feedback everyone. I'll look into it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic