tags:

views:

48

answers:

2

I am using Timertask in my web application to launch a background thread once every 24 hrs every day at midnight. So I have a ServletContextListener and in contextInitialized, I create a Timertask object timertask(say) and a Timer object say t.

I call

t.schedule(timertask, firstTime.getTime(), rescheduleMiliSec); 

where firstTime.getTime() = midnight and rescheduleMiliSec = 24 hr. The thread launches fine and does what it is supposed to do in DIT.Every 24 hrs it launches the background task.

When it moves to PROD, the thread runs only once when context is initialised but not after that.

Is there any specific setting that might be the cause for this?

+1  A: 

Is it possible your TimerTask implementation is throwing a RuntimeException?

If not an exception, then some TimerTask being scheduled in that Timer is blocking indefinitely. Those are the only two conditions that I am aware of that could cause a Timer to fail.

BTW, you might want to look into a ScheduledExecutorService. That is the more modern way of scheduling tasks.

Tim Bender
When the context is first initialised, the TimerTask runs properly and there is no exception. Later also, there is no exception. It just does not run.
Since this issue is urgent for me to solve, if an actual solution is hard to find, a workaround like below will also work. I would still need some help implementing it though: Since the logic resides in contextinitialised(), every time the application is re-deployed, the context will initialise and the logic will run. Is there a way to automatically redeploy the application every dat at midnight? maybe a script? This is a bit untidy, but it will do for now...
@user454671, updated my answer. In regards to redeploying the application, you could probably look into a chron job... but that doesn't seem like a reasonable solution.
Tim Bender
A: 

In the contextInitialized method after scheduling a task using TimerTask , is there any code below that , may be that is causing an exception.

Suresh S