0

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...) ?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
internet
  • 385
  • 1
  • 8
  • 27

1 Answers1

1

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.

Brett Kail
  • 33,593
  • 2
  • 85
  • 90
  • what is the reason for this Timeout callback method? – internet Aug 04 '16 at 19:31
  • It's the target of the createTimer method. It's not possible to actually restart an automatic timer (`@Schedule`), so the best you can do is create an equivalent programmatic timer that uses the same callback method. – Brett Kail Aug 05 '16 at 20:42