3

I have few questions regarding safety and correctness of following hypothetical timer service:

@Singleton
public class MyTimerService {

  @Schedule(second = "*", minute = "*", hour = "*", persistent = false)
  public void checkTakingOneMinute() {
    // code below takes a minute or so
  }
}

All I want to do here, is to check something as soon as possible (every second in this case). As I understand it, method checkTakingOneMinute() wont start new check until it finishes previous call. That's what I want, but I'm worrying about container's internals: will method execution be just skipped when busy or it will be locked and put into a sort of queue with subsequent lock timeouts?

andbi
  • 4,426
  • 5
  • 45
  • 70

2 Answers2

4

The EJB specification requires that the container schedule "catch up" timer events if it's unable to fire a timer because it was already running. In practice, containers probably recalculate the next fire time after the method has completed, and then immediately re-execute if the next fire time is "before" the current time. I know this is how WebSphere Application Server works.

Note that the default ConcurrencyManagement(ConcurrencyManagemementType.CONTAINER) for the bean and Lock(LockType.WRITE) for the timer method will prevent any other methods from being executed. If you need other methods on that bean, you might consider using ConcurrencyManagementType.BEAN.

Brett Kail
  • 33,593
  • 2
  • 85
  • 90
  • Looks like my question is almost the same as http://stackoverflow.com/questions/2691835/timer-service-in-ejb-3-1-schedule-calling-timeout-problem , code rewritten, your solution works great, thanks. – andbi Mar 17 '12 at 11:02
  • You're probably right. I tend to write personalized/customized answers rather than search for dups (even ones that I answered and forgot about!). – Brett Kail Mar 18 '12 at 23:42
3

We use Timers in our app but inside of Weblogic. As far as I know, if the timer thread is busy processing something while it is woken up for the next schedule, the woken up process will wait for the next schedule to re-execute but this could be controlled based on some parameters. I found some documentation on how it is done in Websphere below (I am not sure what those configurable parameters are for Websphere) - may this link will help in clarifying your doubts. There they have mentioned about missed timers (Retries and missed timeouts section), which is what you are concerned about.

EJB timers in Websphere

Prashanth
  • 1,388
  • 2
  • 11
  • 26
  • good article, thanks. You might be also interested in @bkail solution, see my comment under his answer. – andbi Mar 17 '12 at 11:05