Example #1
0
  /**
   * Creates a {@link TimerImpl}
   *
   * @param id The id of this timer
   * @param service The timer service through which this timer was created
   * @param initialExpiry The first expiry of this timer. Can be null
   * @param intervalDuration The duration (in milli sec) between timeouts
   * @param nextEpiry The next expiry of this timer
   * @param info The info that will be passed on through the {@link javax.ejb.Timer} and will be
   *     available through the {@link javax.ejb.Timer#getInfo()} method
   * @param persistent True if this timer is persistent. False otherwise
   * @param timedObjectId
   */
  protected TimerImpl(Builder builder, TimerServiceImpl service) {
    assert builder.id != null : "id is null";

    this.id = builder.id;
    this.timedObjectId = builder.timedObjectId;
    this.info = builder.info;
    this.persistent = builder.persistent;
    this.initialExpiration = builder.initialDate;
    this.intervalDuration = builder.repeatInterval;
    if (builder.newTimer && builder.nextDate == null) {
      this.nextExpiration = initialExpiration;
    } else {
      this.nextExpiration = builder.nextDate;
    }
    this.previousRun = null;
    this.primaryKey = builder.primaryKey;

    this.timerState = builder.timerState;
    this.timerService = service;
    this.timedObjectInvoker = service.getInvoker();
    this.handle = new TimerHandleImpl(this.id, this.timedObjectInvoker.getTimedObjectId(), service);
  }
  private Map<String, TimerImpl> loadTimersFromFile(
      final String timedObjectId, final TimerServiceImpl timerService) {
    final Map<String, TimerImpl> timers = new HashMap<String, TimerImpl>();
    try {
      final File file = new File(getDirectory(timedObjectId));
      if (!file.exists()) {
        // no timers exist yet
        return timers;
      } else if (!file.isDirectory()) {
        ROOT_LOGGER.failToRestoreTimers(file);
        return timers;
      }
      Unmarshaller unmarshaller = factory.createUnmarshaller(configuration);
      for (File timerFile : file.listFiles()) {
        FileInputStream in = null;
        try {
          in = new FileInputStream(timerFile);
          unmarshaller.start(new InputStreamByteInput(in));

          final TimerEntity entity = unmarshaller.readObject(TimerEntity.class);

          // we load the legacy timer entity class, and turn it into a timer state

          TimerImpl.Builder builder;
          if (entity instanceof CalendarTimerEntity) {
            CalendarTimerEntity c = (CalendarTimerEntity) entity;
            builder =
                CalendarTimer.builder()
                    .setScheduleExprSecond(c.getSecond())
                    .setScheduleExprMinute(c.getMinute())
                    .setScheduleExprHour(c.getHour())
                    .setScheduleExprDayOfWeek(c.getDayOfWeek())
                    .setScheduleExprDayOfMonth(c.getDayOfMonth())
                    .setScheduleExprMonth(c.getMonth())
                    .setScheduleExprYear(c.getYear())
                    .setScheduleExprStartDate(c.getStartDate())
                    .setScheduleExprEndDate(c.getEndDate())
                    .setScheduleExprTimezone(c.getTimezone())
                    .setAutoTimer(c.isAutoTimer())
                    .setTimeoutMethod(
                        CalendarTimer.getTimeoutMethod(
                            c.getTimeoutMethod(), timerService.getTimedObjectInvoker().getValue()));
          } else {
            builder = TimerImpl.builder();
          }
          builder
              .setId(entity.getId())
              .setTimedObjectId(entity.getTimedObjectId())
              .setInitialDate(entity.getInitialDate())
              .setRepeatInterval(entity.getInterval())
              .setNextDate(entity.getNextDate())
              .setPreviousRun(entity.getPreviousRun())
              .setInfo(entity.getInfo())
              .setPrimaryKey(entity.getPrimaryKey())
              .setTimerState(entity.getTimerState())
              .setPersistent(true);

          timers.put(entity.getId(), builder.build(timerService));
          unmarshaller.finish();
        } catch (Exception e) {
          ROOT_LOGGER.failToRestoreTimersFromFile(timerFile, e);
        } finally {
          if (in != null) {
            try {
              in.close();
            } catch (IOException e) {
              ROOT_LOGGER.failToCloseFile(e);
            }
          }
        }
      }
    } catch (Exception e) {
      ROOT_LOGGER.failToRestoreTimersForObjectId(timedObjectId, e);
    }
    return timers;
  }
Example #3
0
 /**
  * Returns true if this timer is active. Else returns false.
  *
  * <p>A timer is considered to be "active", if its {@link TimerState} is neither of the following:
  *
  * <ul>
  *   <li>{@link TimerState#CANCELED}
  *   <li>{@link TimerState#EXPIRED}
  *   <li>has not been suspended
  * </ul>
  *
  * <p>And if the corresponding timer service is still up
  *
  * <p>
  *
  * @return
  */
 public boolean isActive() {
   return timerService.isStarted()
       && !isCanceled()
       && !isExpired()
       && (timerService.isScheduled(getId()) || timerState == TimerState.CREATED);
 }
Example #4
0
 /** {@inheritDoc} */
 @Override
 public void cancel() throws IllegalStateException, EJBException {
   timerService.cancelTimer(this);
 }