lili
lili

Reputation: 1906

TimerService: is there a way to prevent running 2 simultaneous timer instances?

I use EJB3.1 TimerService to schedule timers in Java.

Is there a common way to prevent running 2 simultaneous timer instances? Lets say my timer fires every 5 minutes. If for some reason timer started running at 5:00 and finished running at 5:07, I want to run next time at 5:12 (or at 5:10, just skip the timeout at 5:05). Is there an easy/built-in way to do it?

thank you

Upvotes: 1

Views: 560

Answers (1)

Will Hartung
Will Hartung

Reputation: 118651

Just have the process reschedule itself on completion rather than scheduling them all up at once.

Alternatively, you could have a shared semaphore that blocks one job while the other job is running. If it's rare for a job to stall the full 5 minutes, this is a simple fix. If it's not rare, then the jobs will stack up and get out of sync, as the timer will continue to start knew jobs even if there are blocked ones already.

Upvotes: 1

Related Questions