再次开始计划:javax.ejb.Timer

时间:2016-07-30 08:18:26

标签: java-ee ejb schedule

我想在停止计划后再次使用相同的设置(每10秒)(javax.ejb.Timer)启动计划:

// call this remote method with the Timer info that has to be canceled
    @AccessTimeout(value = 20, unit = TimeUnit.MINUTES)
    public void cancelTimer(String timerInfo) {
        try {
            for (Timer timer : timerService.getTimers()) {
                if (timerInfo.equals(timer.getInfo())) {
                    timer.cancel();
                }
            }
        } catch (Exception e) {
        }
    }

这是我停止时间表的功能:

galleryScheduleExecutionService.cancelTimer("mySchedule");

这里的时间表:

 @Lock(LockType.READ)
       @AccessTimeout(value = 20, unit = TimeUnit.MINUTES)
       @Schedule(second = "*/10", minute = "*", hour = "*", persistent = false, info = "mySchedule")
       public void schedule()
           throws StorageAttachmentNotFoundException, IOException,
           DuplicateStorageAttachmentException,
           CannotDuplicateStorageAttachmentException,
           ApplicationInfoNotFoundException, PrintStorageNotFoundException,
           InterruptedException {
         try {

           // start schedule
           galleryScheduleService.doStartSchedule();

         } catch (Exception e) {
           e.printStackTrace();
         }
       }

如何使用相同的设置(每10秒......)再次启动计划?

1 个答案:

答案 0 :(得分:1)

在bean中使用TimerService#createCalendarTimer方法:

@Resource
TimerService timerService;

@Schedule(...)
@Timeout
public void schedule() { ... }

public void restart() {
  TimerConfig timerConfig = new TimerConfig();
  timerConfig.setPersistent(false);
  timerService.createCalendarTimer(new ScheduleExpression()
    .second("*/10")
    .minute("*")
    .hour("*"),
    new TimerConfig("mySchedule", false));
}

您必须声明超时回调方法(例如,使用@Timeout),该方法可以与@Schedule方法相同。