aspose file tools*
The moose likes Servlets and the fly likes TimerTask and servlet container Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "TimerTask and servlet container" Watch "TimerTask and servlet container" New topic
Author

TimerTask and servlet container

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
I developed a web application deployed to websphere container. I want to create a scheduled job running daily. I want to use JDK TimerTask API for this. But I don't want this job be triggered by the shutdown and/or start up. So, can I still use Timer task for my j2ee web application for daily job ?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Not sure I understand your question. Why would server startup/shutdown control when your TimerTask is run?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by ben oliver:
I developed a web application deployed to websphere container. I want to create a scheduled job running daily.
Is there a relationship between these two statements? If so, what is it? Like the other Paul, I don't understand your question.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

Does it really need to run as part of the web app?

For such things in the past I've created stand-alone daemon applications that I can schedule as a cron job and run independently of the web app container.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
Sorry it confused you. I want to deploy this Timer task inside the .war or .ear file, for example, create it as a sort of action class and add some properties file to specify the daily run time. What bothers me is -- when my web container starts or shuts down, will it trigger this timer task ? if my .ear gets redeployed will it trigger my timer task ?

For the time being let's assume I don't want to go with cron job type approach. Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41521
    
  53
I'm still not clear on what you're asking. The task will be triggered at the time you set it up to be triggered. In a web app, you would do this in the contextInitialized method of a ServletContextListener. If the app is shut down, the task will run no more until the app is restarted/redeployed.


Ping & DNS - my free Android networking tools app
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
Originally posted by Ulf Dittmer:
I'm still not clear on what you're asking. The task will be triggered at the time you set it up to be triggered. In a web app, you would do this in the contextInitialized method of a ServletContextListener. If the app is shut down, the task will run no more until the app is restarted/redeployed.


So if the web app is up running then everything is fine. Suppose I schedule it to run at 8AM daily. Around 6AM today the app is shutdown and it was restarted at 9AM today. So when it restarts, will it skip today's scheduled run and run again tomorrow at 8AM ?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

That would be up to you.

If it's something that needs to be run daily but your webserver isn't always running, you might want to log each call to this task in a database or file.
Then, when the app starts up, it can check to see if the task has been run today, if not run it.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TimerTask and servlet container