EJB 调度问题

EJB Schedule issue

在我的无状态会话 bean 中的一个方法上配置调度注释时,我遇到了一个表面问题。

@Schedule(minute = "*/5", hour = "*", persistent = false)
@Override
public void retrieveDoc() {
    try {

        //--- --- --- --- --- --- --- 
    } catch (InterruptedException ex) {

    }
}

我希望我的方法每 5 分钟执行一次。然而,这种执行在大约 7 天后停止。有什么我想念的吗?我希望这种方法每 5 分钟 运行 只要服务器启动并且 运行ning.

非常感谢对此的任何提示。

谢谢。

你的计时器应该永远每五分钟响一次。您是否有可能在该方法中发现异常?如果在 @Schedule 方法中抛出异常,该方法将在 5 秒后再次调用,如果失败,计时器将终止。

因为这是ejb,所以默认是事务性的。为了有效地捕获抛出的异常,将逻辑包装到不同的 ejb 调用中,专门在不同的事务中调用该方法。

@Stateless
public class MySchedules{

  @EJB
  private MyScheduleService scheduleService;

  @Schedule(minute="*/5", hour="*")
  public void scheduleMe() {
     try {
       //The logic here is that you cannot catch an exception 
       //thrown by the current transaction, but you sure can catch
       //an exception thrown by the next transaction started 
       //by the following call.
       scheduleService.doTransactionalSchedule();
     }catch(Exception ex) {//log me}
  }
}

@Stateless
@TransactionAttribute(REQUIRES_NEW)
public class MyScheduleService {

   public void doTransactionalSchedule() {
     //do something meaningful that can throw an exception.
   }
}