andbi
andbi

Reputation: 4454

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?

Upvotes: 3

Views: 7990

Answers (2)

Prashanth
Prashanth

Reputation: 1398

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

Upvotes: 3

Brett Kail
Brett Kail

Reputation: 33956

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.

Upvotes: 4

Related Questions