2016-07-30 50 views
0

我想使用相同的設置(每10秒)(javax.ejb.Timer)再次啓動時間表我已經停止了時間表後:再次啓動時間表: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

從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

+0

這種超時回調方法的原因是什麼? – internet

+0

它是createTimer方法的目標。實際上重啓一個自動定時器是不可能的('@ Schedule'),所以你可以做的最好的是創建一個等效的編程定時器,它使用相同的回調方法。 –