internet
internet

Reputation: 385

Start Schedule again: javax.ejb.Timer

I would like to start the schedule again with the same settings (every 10 seconds) (javax.ejb.Timer) after I have stopped the schedule:

// 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) {
        }
    }

This is my function to stop the schedule:

galleryScheduleExecutionService.cancelTimer("mySchedule");

here the schedule:

 @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();
         }
       }

How can I start the schedule again with the same settings (every 10 seconds...) ?

Upvotes: 0

Views: 544

Answers (1)

Brett Kail
Brett Kail

Reputation: 33936

Use the TimerService#createCalendarTimer method from within the bean:

@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));
}

You must declare a timeout callback method (e.g., using @Timeout), which can be the same method as the @Schedule method.

Upvotes: 1

Related Questions