Esempio n. 1
0
  @Schedule(minute = "*/1", hour = "*")
  public void checkTimers() {
    try {
      Context context = new InitialContext();
      List<TimerParams> timerParams = service.getAll();

      for (TimerParams entity : timerParams) {
        long currentTime = Calendar.getInstance().getTimeInMillis();
        long lastTimeEffectuated = entity.getLastTimeEffectuated();

        // si el handler no se ha ejecutado nunca
        if (lastTimeEffectuated == TimerParams.NO_TIME) {
          invokeTimerHandler(context, entity, currentTime, false);
        } else {

          long nextTime = getNextExecutionTime(entity);
          long diferenceTime = currentTime - nextTime;

          if (diferenceTime >= 0) invokeTimerHandler(context, entity, currentTime, true);
        }
      }

    } catch (Exception e) {
      logger.log(Level.INFO, "Error en GHATimerService.checkTimers: ", e);
    }
  }
Esempio n. 2
0
  /**
   * Return the next excecution time for the given timer parameters
   *
   * @param entity entity with the timer params
   * @return the milliseconds of the next execution time
   */
  private long getNextExecutionTime(TimerParams entity) {
    Calendar nextCalendar = Calendar.getInstance();
    nextCalendar.setTimeInMillis(entity.getLastTimeEffectuated());

    nextCalendar.add(Calendar.SECOND, entity.getSeconds());
    nextCalendar.add(Calendar.MINUTE, entity.getMinutes());
    nextCalendar.add(Calendar.HOUR_OF_DAY, entity.getHours());
    nextCalendar.add(Calendar.DAY_OF_MONTH, entity.getDays());
    nextCalendar.add(Calendar.YEAR, entity.getYears());

    return nextCalendar.getTimeInMillis();
  }