This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes TimerTask, Threads and Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "TimerTask, Threads and Servlets" Watch "TimerTask, Threads and Servlets" New topic
Author

TimerTask, Threads and Servlets

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
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?


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Dec 11, 2004
Posts: 13410

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TimerTask, Threads and Servlets
 
Similar Threads
Standalone webservice clients
Thread Scheduler
TimerTask, Thread
How to stop a Timer after it's run more than once?
Canceling a TimerTask