Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Servlet thread for periodic task?

Status
Not open for further replies.

p3t3

Programmer
Aug 21, 2002
46
GB
Hi,

I have a web application in which I need to run a task periodicaly at a fairly long interval, say every three hours. I dont have the ability to use any system task scheduling. I have considered starting a seperate timer thread when the application starts, which will achieve this. My googles have only revelaed doing this from within a servlet page. Can this thread be set to not be linked to any user sessions, i.e not to be killed when a session ends, but rather as a seperate thread of the application which is valid for the applications life time? Or, am I approaching this from the wrong direction?

Thanks. Pete

***************************************
SPONSER MY SIGNITURE FOR ONLY $5!!
 
If I get what I think you mean .... You could write a class which when instantiated just calls Thread.sleep(30000) (or however long), then does the task you need it to perform, and sit all this in a big while loop.

If you create a object of this in your web applications main servlet's init() method, (ie when you're webapp starts up) and also in the destroy() method (ie when you're webapp shuts down), you'd be away.
 
Thanks, yes the init() method is the place I am looking for.

One issue, I was thinking along the lines of creating java.util.Timer(boolean isDaemon) object rather than calling Thread.sleep are the two methods equivalant?



***************************************
SPONSER MY SIGNITURE FOR ONLY $5!!
 
I'm not overly sure ... read the API docs on those two.
 
Most of the application servers provide the option of specifying startup classes. They let you specify startup classes as a configuration paramter(a properties file or a XML) and start them automatically when the server is started. I'd suggest you to explore this option in your app server.
Also, if you are in a J2EE environment, you can use JMS for scheduling your job. Run the task once and put a request for the same task in the JMS queue to be processed after x minutes.

Ganesh
If you find my post really helpful, please record it by clicking the purple star below.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top