Victor
Victor

Reputation: 17077

Timer task does not run

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?

Upvotes: 1

Views: 5771

Answers (3)

sam
sam

Reputation: 11

I think the reason is simple but it may evade the naked eye.

firstTime.getTime() 

is in milliseconds and the following method take precedence:

schedule(TimerTask task, long delay, long period)

insead of the intended:

schedule(TimerTask task, Date firstTime, long period)

Upvotes: 1

Dead Programmer
Dead Programmer

Reputation: 12575

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

Upvotes: 0

Tim Bender
Tim Bender

Reputation: 20442

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.

Upvotes: 2

Related Questions