Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TimerTask, Threads and Servlets

 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is a TimerTask a Thread? Reason I ask is this. I'm creating a TimerTask to execute some code on a daily basis. AND is the TimerTask outside of the Servlet container. If it is, what kind of TimerTask like object can I create so it is INSIDE of the web container?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dale,
Take a look at the following article.
It goes over several approaches for running jobs within Java apps; including java.util.timer and Quartz.
http://www.onjava.com/pub/a/onjava/2004/03/10/quartz.html
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't look like this solution provides me with a way of scheduling a task that will occur within a web container. I'm looking for something that I can control by restarting my web container. This way its safe for the company.
 
Paul Clapham
Sheriff
Pie
Posts: 20750
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't read the article, but was there something in it that said "You can't use a Timer in a web container"? Because you can use a Timer in a web container. Maybe you just didn't see an explicit example of doing that? If you want to start a Timer when a web application starts up, then you could do so in the contextInitialized() method of a ServletContextListener.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dale DeMott:
It doesn't look like this solution provides me with a way of scheduling a task that will occur within a web container. I'm looking for something that I can control by restarting my web container. This way its safe for the company.


What did you see in there that leads to to believe that this couldn't be done from within a web container?
A web container is a Java app.

You could certainly embed either solution (quartz or java.util.timer) into a context scoped object that gets instanciated via a contextListener.
There are some things to watch out for (daemon vs non-daemon threads, etc..) but these are typical issues when starting your own threads from within a webapp.

[KLUNK!!]
[ May 18, 2006: Message edited by: Ben Souther ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic