【问题标题】:EJB Schedule issueEJB 调度问题
【发布时间】:2015-03-30 18:46:52
【问题描述】:

在我的无状态会话 bean 中的一种方法上配置计划注释时,我遇到了一个外观问题。

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

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

    }
}

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

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

谢谢。

【问题讨论】:

    标签: jakarta-ee timer ejb


    【解决方案1】:

    因为这是 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.
       }
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 嗯...现在我明白为什么它突然停止执行了。即使@Schedule 方法出现故障,有没有办法让计时器保持活动状态?无论如何感谢您的回复。
      • 是的,这是因为交易。试试 maress 的解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多