private Schedule getSchedule() { return Schedule.forInterval(SCHEDULE_INTERVAL, null); }
/** * Creates a new schedule that will run once at the specified time. Jobs that are scheduled to run once * are not guaranteed to remain in the system after they are attempted, regardless of the {@link RunOutcome}. * Jobs scheduled as {@code runOnce} will be purged automatically after they have run; that is, at some * point after they have run (possibly immediately), {@link SchedulerService#getJobDetails(JobId)} will * no longer return the job's information. * * @param runTime when the job should run; may be {@code null} to indicate that the job should run * as soon as possible * @return a schedule for running once at the given time */ public static Schedule runOnce(@Nullable Date runTime) { return forInterval(0L, runTime); }
return Schedule.forInterval(intervalMillis, nextExecutionDate.getTime());
@Test public void testRunLater() { // But here we're using an exact time and there shouldn't be any mismatch to it final long now = currentTimeMillis(); assertNextRunTime(forInterval(60000L, new Date(now + 15000L)), now + 15000L); }
private Schedule getSimpleTriggerSchedule(final GenericValue jobTrigger, final String triggerName) { GenericValue simpleTrigger = getTriggerDetails("QRTZSimpleTriggers", jobTrigger); if (simpleTrigger == null) { LOG.error("Missing simple trigger data for filter subscription trigger '" + triggerName + '\''); return null; } final Long repeatInterval = simpleTrigger.getLong("repeatInterval"); if (repeatInterval == null) { LOG.error("Missing repeatInterval for filter subscription simple trigger '" + triggerName + '\''); return null; } return Schedule.forInterval(repeatInterval, null); }
@Test public void testRunInThePast() { // wide window again (see above) final long now = currentTimeMillis(); assertNextRunTime(forInterval(60000L, new Date(now - 120000L)), between(now - 100L, now + 61000L)); }
@Test public void testRunImmediately() { // We use a wide window here because Quartz's clock isn't pluggable and whether it adds the interval to // the time or not seems to be a fairly random thing. final long now = currentTimeMillis(); assertNextRunTime(forInterval(60000L, null), between(now - 100L, now + 61000L)); }
@Override public void afterPropertiesSet() throws Exception { schedulerService.registerJobRunner(CLEANUP_JOB_RUNNER_KEY, newCleanupTask()); // schedule the cleanup job once a day starting in a day schedulerService.scheduleJob(CLEANUP_JOB_ID, JobConfig.forJobRunnerKey(CLEANUP_JOB_RUNNER_KEY) .withSchedule(Schedule.forInterval(TimeUnit.DAYS.toMillis(1), DateTime.now().plusDays(1).toDate())) .withRunMode(RunMode.RUN_LOCALLY)); // cache dir is local to every application node }
@Override public void afterPropertiesSet() throws Exception { schedulerService.registerJobRunner(CLEANUP_JOB_RUNNER_KEY, newCleanupTask()); // schedule the cleanup job once a day starting in a day schedulerService.scheduleJob(CLEANUP_JOB_ID, JobConfig.forJobRunnerKey(CLEANUP_JOB_RUNNER_KEY) .withSchedule(Schedule.forInterval(TimeUnit.DAYS.toMillis(1), DateTime.now().plusDays(1).toDate())) .withRunMode(RunMode.RUN_LOCALLY)); // cache dir is local to every application node }
@Override public void onStart() { long intervalInMillis = configuration.getAlertTruncationInterval().toMillis(); long firstRunTime = System.currentTimeMillis() + intervalInMillis; schedulerService.registerJobRunner(JOB_RUNNER_KEY, new TruncateAlertsJobRunner()); try { schedulerService.scheduleJob(JOB_ID, JobConfig.forJobRunnerKey(JOB_RUNNER_KEY) .withSchedule(Schedule.forInterval(intervalInMillis, new Date(firstRunTime))) .withRunMode(RunMode.RUN_ONCE_PER_CLUSTER)); } catch (SchedulerServiceException e) { log.warn("Failed to schedule periodic alert truncation", e); } }
@Override public void onStart() { schedulerService.registerJobRunner(JOB_RUNNER_KEY, this); JobConfig config = JobConfig.forJobRunnerKey(JOB_RUNNER_KEY) .withRunMode(RUN_ONCE_PER_CLUSTER) .withSchedule(Schedule.forInterval(REMOVE_INTERVAL, new Date(System.currentTimeMillis() + REMOVE_INTERVAL))); try { schedulerService.scheduleJob(JobId.of(JOB_ID), config); } catch (SchedulerServiceException e) { log.error("Unable to schedule expired session remover job", e); } }
@Override public void start() throws Exception { final SchedulerService scheduler = ComponentAccessor.getComponent(SchedulerService.class); scheduler.registerJobRunner(JOB_RUNNER_KEY, this); // we need to scatter a bit sending events to analytics system, // lets not start this service on every instance in the same moment final Date firstRun = Date.from(Instant.now().plusMillis(random.nextLong() % FOUR_HOURS)); final JobConfig jobConfig = JobConfig.forJobRunnerKey(JOB_RUNNER_KEY) .withRunMode(RunMode.RUN_LOCALLY) .withSchedule(Schedule.forInterval(RUN_INTERVAL, firstRun)); scheduler.scheduleJob(JOB_ID, jobConfig); }
@Nonnull @Override protected Schedule getSchedule(Trigger trigger) { if (trigger instanceof CronTrigger) { final CronTrigger cron = (CronTrigger) trigger; return Schedule.forCronExpression(cron.getCronExpression(), cron.getTimeZone()); } if (trigger instanceof SimpleTrigger) { final SimpleTrigger simple = (SimpleTrigger) trigger; return Schedule.forInterval(simple.getRepeatInterval(), simple.getStartTime()); } throw new SchedulerRuntimeException("The job with jobId '" + trigger.getName() + "' has an unsupported trigger class: " + trigger.getClass().getName()); }
private void migrateSimpleTrigger(final GenericValue trigger, final Long repeatInterval) { final String jobId = trigger.getString(TRIGGER_NAME); LOG.debug("migrateSimpleTrigger: {}", jobId); final Date firstRunTime = toDate(trigger.getLong(START_TIME)); final Date nextRunTime = toDate(trigger.getLong(NEXT_FIRE_TIME)); final long intervalInMillis = (repeatInterval != null) ? repeatInterval : 0L; final Schedule schedule = Schedule.forInterval(intervalInMillis, firstRunTime); createClusteredJob(trigger, jobId, nextRunTime, schedule); }
@Override protected void startHeartbeat() throws SchedulerServiceException { super.startHeartbeat(); //Initially run explicitly to get the first run out immediately SharedHomeFileWriterJobRunner runner = new SharedHomeFileWriterJobRunner(); runner.writeFile(); schedulerService.registerJobRunner(SHARED_HOME_FILE_WRITER_JOB_RUNNER_KEY, runner); JobConfig jobConfig = JobConfig.forJobRunnerKey(SHARED_HOME_FILE_WRITER_JOB_RUNNER_KEY) .withRunMode(RunMode.RUN_LOCALLY) .withSchedule(Schedule.forInterval(TimeUnit.MINUTES.toMillis(1), null)); schedulerService.scheduleJob(JobId.of(SHARED_HOME_FILE_WRITER_JOB_RUNNER_KEY_NAME), jobConfig); }
@Override public void start() throws Exception { SchedulerService scheduler = ComponentAccessor.getComponent(SchedulerService.class); scheduler.registerJobRunner(JOB_RUNNER_KEY, this); final JobConfig jobConfig = JobConfig.forJobRunnerKey(JOB_RUNNER_KEY) .withRunMode(RunMode.RUN_LOCALLY) .withSchedule(Schedule.forInterval(FIVE_MINUTES, null)); scheduler.scheduleJob(JOB_ID, jobConfig); }
@Override public void addPoller(final DirectoryPoller poller) throws DirectoryMonitorRegistrationException { final Date oneMinuteFromNow = new DateTime().plusMinutes(1).toDate(); final JobConfig config = JobConfig.forJobRunnerKey(JOB_RUNNER_KEY) .withRunMode(RUN_ONCE_PER_CLUSTER) .withSchedule(Schedule.forInterval(poller.getPollingInterval() * 1000, oneMinuteFromNow)) .withParameters(ImmutableMap.<String, Serializable>of(DIRECTORY_ID, poller.getDirectoryID())); try { schedulerService.scheduleJob(getJobId(poller), config); } catch (SchedulerServiceException e) { throw new DirectoryMonitorRegistrationException(e); } }
@Override public void start() throws Exception { final SchedulerService scheduler = ComponentAccessor.getComponent(SchedulerService.class); scheduler.registerJobRunner(JOB_RUNNER_KEY, this); final JobConfig jobConfig = JobConfig.forJobRunnerKey(JOB_RUNNER_KEY) .withRunMode(RunMode.RUN_LOCALLY) .withSchedule(Schedule.forInterval(DAILY, null)); scheduler.scheduleJob(JOB_ID, jobConfig); } }
private void scheduleSimpleJob(final JiraServiceContainer jiraServiceContainer) throws SchedulerServiceException { // Work out the fire time delay. Date nextFireTime = null; long lastRunMillis = jiraServiceContainer.getLastRun(); final long nextRunMillis = lastRunMillis > 0 ? lastRunMillis + jiraServiceContainer.getDelay() : System.currentTimeMillis(); final boolean localService = jiraServiceContainer.isLocalService(); if (nextRunMillis > System.currentTimeMillis()) { nextFireTime = new Date(nextRunMillis); } JobConfig config = JobConfig.forJobRunnerKey(SERVICE_JOB_KEY) .withSchedule(Schedule.forInterval(jiraServiceContainer.getDelay(), nextFireTime)) .withRunMode(localService ? RunMode.RUN_LOCALLY : RunMode.RUN_ONCE_PER_CLUSTER) .withParameters(ImmutableMap.<String, Serializable>of(SERVICE_ID_KEY, jiraServiceContainer.getId())); schedulerService.scheduleJob(toJobId(jiraServiceContainer.getId()), config); }
private static Schedule getSchedule(final GenericValue genericValue) { final Type type = toScheduleType(genericValue.getString(SCHED_TYPE)); switch (type) { case INTERVAL: return Schedule.forInterval( genericValue.getLong(INTERVAL_MILLIS), toDate(genericValue.getLong(FIRST_RUN))); case CRON_EXPRESSION: return Schedule.forCronExpression( genericValue.getString(CRON_EXPRESSION), toTimeZone(genericValue.getString(TIME_ZONE))); } throw badType(type); }