问题
We are using the @Schedule
EJB annotation to start a scheduled job do a delta SOLR import every x seconds. Of course we want to have as less timeshift in indexing as possible. Currently I try to do the delta every 5 seconds.
So my question is: Can I make sure the job is only called once? So let's say the indexing is lasting longer than 5 seconds, will the next schedules call wait until the previous is coming back?
回答1:
Yes. You can make the bean a @Singleton
and use @Lock(LockType.Write) to make sure that timers don't run in parallel. Note that LockType.Write
is the default in a Singleton. So even the explicit mention with the annotation would not be required.
来源:https://stackoverflow.com/questions/8164176/how-to-make-schedule-method-to-be-called-only-once-at-a-time