Search code examples
javajakarta-eeejbejb-3.0

EJB automatic timers, locking, timeouts and long-running methods


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?


Solution

  • 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.