Beispiel #1
0
  private void expire() {
    if (expire == null) {
      clear();
    } else {
      if (expireTime > 0 && GungnirUtils.currentTimeMillis() >= expireTime) {
        if (LOG.isDebugEnabled()) {
          LOG.debug("expire {} >= {}", GungnirUtils.currentTimeMillis(), expireTime);
        }
        clear();

        if (expire.getType() == IntervalType.CRON) {
          try {
            CronExpression cronExpr = new CronExpression(expire.getSchedulingPattern());
            expireTime =
                cronExpr
                    .getNextValidTimeAfter(new Date(GungnirUtils.currentTimeMillis()))
                    .getTime();
          } catch (ParseException e) {
            expireTime = 0;
            LOG.error("Failed to parse pattern", e);
          }
        } else {
          long expireMs = expire.getPeriod().getTimeUnit().toMillis(expire.getPeriod().getTime());
          expireTime += ((GungnirUtils.currentTimeMillis() - expireTime) / expireMs + 1) * expireMs;
        }
      }
    }
  }
Beispiel #2
0
  @Override
  protected void prepare() {
    if (interval.getType() == IntervalType.COUNT) {
      counter = 0;
    } else {
      if (snapshotJob == null) {
        snapshotJob = new SnapshotJob();
        try {
          if (interval.getType() == IntervalType.CRON) {
            getContext()
                .getComponent()
                .getShapshotTimer()
                .cronSchedule(interval.getSchedulingPattern(), snapshotJob);
          } else {
            getContext()
                .getComponent()
                .getShapshotTimer()
                .periodSchedule(interval.getPeriod(), snapshotJob);
          }
        } catch (SchedulerException e) {
          LOG.error("Failed to add schedule", e);
        }
      }

      snapshotJob.addTask(new CommitTask());

      if (expire != null) {
        if (interval.getType() == IntervalType.CRON) {
          CronExpression cronExpr;
          try {
            cronExpr = new CronExpression(expire.getSchedulingPattern());
            expireTime =
                cronExpr
                    .getNextValidTimeAfter(new Date(GungnirUtils.currentTimeMillis()))
                    .getTime();
          } catch (ParseException e) {
            LOG.error("Failed to parse pattern", e);
          }
        } else {
          expireTime =
              GungnirUtils.currentTimeMillis()
                  + expire.getPeriod().getTimeUnit().toMillis(expire.getPeriod().getTime());
        }
      }
    }
  }