org.springframework.scheduling.support.CronTrigger Java Examples
The following examples show how to use
org.springframework.scheduling.support.CronTrigger.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JobConfiguration.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Only set next execution time if the job is not continuous. */ public void setNextExecutionTime( Date nextExecutionTime ) { if ( cronExpression == null || cronExpression.equals( "" ) || cronExpression.equals( "* * * * * ?" ) ) { return; } if ( nextExecutionTime != null ) { this.nextExecutionTime = nextExecutionTime; } else { this.nextExecutionTime = new CronTrigger( cronExpression ).nextExecutionTime( new SimpleTriggerContext() ); } }
Example #2
Source File: PipelineTaskScheduler.java From super-cloudops with Apache License 2.0 | 6 votes |
/** * Startup pipeline job. * * @param trigger * @param project * @param task * @param taskInstances */ private void startupTimingPipeline(Trigger trigger, Project project, Task task, List<TaskInstance> taskInstances) { if (log.isInfoEnabled()) { log.info("Startup timing pipeline for triggerId: {}, expression: '{}', instances: {} ", trigger.getId(), trigger.getCron(), taskInstances); } stopTimingPipeline(trigger); if (trigger.getEnable() != 1) { return; } TimingPipelineProvider provider = beanFactory.getBean(TimingPipelineProvider.class, new Object[] { trigger, project, task, taskInstances }); ScheduledFuture<?> future = taskScheduler.schedule(provider, new CronTrigger(trigger.getCron())); // TODO distributed cluster?? PipelineTaskScheduler.map.put(getTimingPipelineKey(trigger), future); }
Example #3
Source File: EngineTaskScheduler.java From super-cloudops with Apache License 2.0 | 6 votes |
/** * Startup pipeline job. * * @param trigger * @param project * @param task * @param taskInstances */ private void startupTimingPipeline(CustomEngine customEngine) { log.info("Startup timing pipeline for customEngineId: {}, expression: '{}'", customEngine.getId(), customEngine.getCron()); stopTimingPipeline(customEngine); if (Objects.isNull(customEngine.getStatus()) || customEngine.getStatus() == 0) { return; } CronTrigger cronTrigger = checkCron(customEngine.getCron()); if(Objects.isNull(cronTrigger)){ log.error("cron is error, cron={}",customEngine.getCron()); return; } TimingEngineProvider provider = beanFactory.getBean(TimingEngineProvider.class, new Object[] {customEngine}); ScheduledFuture<?> future = taskScheduler.schedule(provider, new CronTrigger(customEngine.getCron())); EngineTaskScheduler.map.put(getTimingPipelineKey(customEngine.getId()), future); }
Example #4
Source File: ScheduledSyncService.java From gravitee-gateway with Apache License 2.0 | 6 votes |
@Override protected void doStart() throws Exception { if (! localRegistryEnabled) { if (enabled) { super.doStart(); logger.info("Sync service has been initialized with cron [{}]", cronTrigger); logger.info("Associate a new HTTP handler on {}", PATH); // Create and associate handler SyncHandler syncHandler = new SyncHandler(); applicationContext.getAutowireCapableBeanFactory().autowireBean(syncHandler); router.get(PATH).produces(MediaType.APPLICATION_JSON).handler(syncHandler); // Star cron scheduler.schedule(this, new CronTrigger(cronTrigger)); } else { logger.warn("Sync service has been disabled"); } } else { logger.warn("Sync service is disabled because local registry mode is enabled"); } }
Example #5
Source File: CronScheduler.java From spring-batch-lightmin with Apache License 2.0 | 6 votes |
public CronScheduler(final SchedulerConstructorWrapper schedulerConstructorWrapper) { this.jobConfiguration = schedulerConstructorWrapper.getJobConfiguration(); this.threadPoolTaskScheduler = schedulerConstructorWrapper.getThreadPoolTaskScheduler(); this.jobSchedulerConfiguration = this.jobConfiguration.getJobSchedulerConfiguration(); this.timeZone = TimeZone.getDefault(); this.trigger = new CronTrigger(this.jobSchedulerConfiguration.getCronExpression(), this.timeZone); this.job = schedulerConstructorWrapper.getJob(); this.jobRunner = new JobRunner(this.job, schedulerConstructorWrapper.getJobLauncher(), schedulerConstructorWrapper.getJobParameters(), schedulerConstructorWrapper.getJobIncrementer()); final SchedulerStatus schedulerStatus; if (this.jobSchedulerConfiguration.getSchedulerStatus() != null) { schedulerStatus = this.jobSchedulerConfiguration.getSchedulerStatus(); } else { schedulerStatus = SchedulerStatus.INITIALIZED; } this.setStatus(schedulerStatus); }
Example #6
Source File: ScheduledSyncService.java From gravitee-management-rest-api with Apache License 2.0 | 6 votes |
@Override protected void doStart() throws Exception { if (! localRegistryEnabled) { if (enabled) { super.doStart(); logger.info("Sync service has been initialized with cron [{}]", cronTrigger); // Sync must start only when doStart() is invoked, that's the reason why we are not // using @Scheduled annotation on doSync() method. scheduler.schedule(this, new CronTrigger(cronTrigger)); } else { logger.warn("Sync service has been disabled"); } } else { logger.warn("Sync service is disabled because local registry mode is enabled"); } }
Example #7
Source File: ScheduledTask.java From blackduck-alert with Apache License 2.0 | 6 votes |
public void scheduleExecution(String cron) { if (StringUtils.isNotBlank(cron)) { try { CronTrigger cronTrigger = new CronTrigger(cron, TimeZone.getTimeZone("UTC")); unscheduleTask(); logger.info("Scheduling {} with cron : {}", getTaskName(), cron); future = taskScheduler.schedule(this, cronTrigger); } catch (IllegalArgumentException e) { logger.error(e.getMessage(), e); } } else { if (future != null) { logger.info("Un-Scheduling {}", getTaskName()); unscheduleTask(); } } }
Example #8
Source File: ScheduledTaskTest.java From blackduck-alert with Apache License 2.0 | 6 votes |
@Test public void testNextFormattedRuntime() { final Long millisecondsToNextRun = 10000L; ZonedDateTime currentUTCTime = ZonedDateTime.now(ZoneOffset.UTC); ZonedDateTime expectedDateTime = currentUTCTime.plus(millisecondsToNextRun, ChronoUnit.MILLIS); int seconds = expectedDateTime.getSecond(); if (seconds >= 30) { expectedDateTime = expectedDateTime.truncatedTo(ChronoUnit.MINUTES).plusMinutes(1); } else { expectedDateTime = expectedDateTime.truncatedTo(ChronoUnit.MINUTES); } String expectedNextRunTime = expectedDateTime.format(DateTimeFormatter.ofPattern(ScheduledTask.FORMAT_PATTERN)) + " UTC"; Mockito.doReturn(future).when(taskScheduler).schedule(Mockito.any(), Mockito.any(CronTrigger.class)); Mockito.when(future.getDelay(TimeUnit.MILLISECONDS)).thenReturn(millisecondsToNextRun); task.scheduleExecution(validCronExpression); Optional<String> nextRunTime = task.getFormatedNextRunTime(); assertTrue(nextRunTime.isPresent()); String nextTime = nextRunTime.get(); assertEquals(expectedNextRunTime, nextTime); }
Example #9
Source File: DynamicTaskApiImpl.java From es with Apache License 2.0 | 6 votes |
private synchronized void startTask(boolean forceStart, Long... taskDefinitionIds) { for(Long taskDefinitionId : taskDefinitionIds) { TaskDefinition td = taskDefinitionService.findOne(taskDefinitionId); if(td == null || (forceStart == false && Boolean.TRUE.equals(td.getStart()))) { return; } try { ScheduledFuture<?> future = taskScheduler.schedule(createTask(td), new CronTrigger(td.getCron())); taskMap.put(taskDefinitionId, future); td.setStart(Boolean.TRUE); } catch (Exception e) { logger.error("start task error, task id:" + taskDefinitionId, e); td.setDescription(e.getMessage()); } taskDefinitionService.update(td); } }
Example #10
Source File: SchedulerConfig.java From WeBASE-Transaction with Apache License 2.0 | 6 votes |
@Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { if (constants.isIfDeleteData()) { taskRegistrar.addTriggerTask(() -> scheduleService.deleteDataSchedule(), (context) -> new CronTrigger(constants.getCronDeleteData()) .nextExecutionTime(context)); } if (!constants.isIfDistributedTask()) { taskRegistrar.addTriggerTask(() -> scheduleService.deploySchedule(), (context) -> new CronTrigger(constants.getCronTrans()) .nextExecutionTime(context)); taskRegistrar.addTriggerTask(() -> scheduleService.transSchedule(), (context) -> new CronTrigger(constants.getCronTrans()) .nextExecutionTime(context)); } }
Example #11
Source File: ScheduledTaskTest.java From blackduck-alert with Apache License 2.0 | 5 votes |
@Test public void testNextRuntime() { final Long expectedNextRun = 10000L; Mockito.doReturn(future).when(taskScheduler).schedule(Mockito.any(), Mockito.any(CronTrigger.class)); Mockito.when(future.getDelay(TimeUnit.MILLISECONDS)).thenReturn(expectedNextRun); task.scheduleExecution(validCronExpression); Optional<Long> actualNextRun = task.getMillisecondsToNextRun(); assertTrue(actualNextRun.isPresent()); assertEquals(expectedNextRun, actualNextRun.get()); }
Example #12
Source File: ScheduledSearchIndexerService.java From gravitee-management-rest-api with Apache License 2.0 | 5 votes |
@Override protected void doStart() throws Exception { if (enabled) { super.doStart(); logger.info("Search Indexer service has been initialized with cron [{}]", cronTrigger); scheduler.schedule(this, new CronTrigger(cronTrigger)); } else { logger.warn("Search Indexer service has been disabled"); } }
Example #13
Source File: ScheduledTaskTest.java From blackduck-alert with Apache License 2.0 | 5 votes |
@Test public void testNextRuntimeNullFuture() { Mockito.doReturn(null).when(taskScheduler).schedule(Mockito.any(), Mockito.any(CronTrigger.class)); task.scheduleExecution(validCronExpression); Optional<Long> actualNextRun = task.getMillisecondsToNextRun(); assertFalse(actualNextRun.isPresent()); }
Example #14
Source File: DatabaseMySQLTraceLogFlushHandler.java From summerframework with Apache License 2.0 | 5 votes |
public DatabaseMySQLTraceLogFlushHandler(DataSource dataSource) { Assert.notNull(dataSource, "DatabaseTraceLogFlushHandler must have dataSource."); this.dataSource = dataSource; doWithConnection(createTableIfNotExist); tableReady = true; threadPoolTaskScheduler = new ThreadPoolTaskScheduler(); threadPoolTaskScheduler.setDaemon(true); threadPoolTaskScheduler.setThreadNamePrefix("TraceLogFlush"); threadPoolTaskScheduler.initialize(); threadPoolTaskScheduler.schedule(cleanerTask, new CronTrigger(System.getProperty("rabbit.trace.flush.trigger", "0 0 1 * * ?"))); }
Example #15
Source File: ScheduledTaskTest.java From blackduck-alert with Apache License 2.0 | 5 votes |
@Test public void testNextRuntimeFutureCancelled() { Mockito.doReturn(future).when(taskScheduler).schedule(Mockito.any(), Mockito.any(CronTrigger.class)); Mockito.when(future.isCancelled()).thenReturn(true); task.scheduleExecution(validCronExpression); Optional<Long> actualNextRun = task.getMillisecondsToNextRun(); assertFalse(actualNextRun.isPresent()); }
Example #16
Source File: AbstractJob.java From chronus with Apache License 2.0 | 5 votes |
protected void configureTasks(ScheduledTaskRegistrar taskRegistrar, Runnable task, String cron) { taskRegistrar.addTriggerTask(task, (TriggerContext triggerContext) -> { CronTrigger trigger = new CronTrigger(cron); Date nextExec = trigger.nextExecutionTime(triggerContext); return nextExec; }); }
Example #17
Source File: SpringTaskProcessor.java From lemon with Apache License 2.0 | 5 votes |
public void process(String jobClassName, String jobConfig) { try { Runnable runnable = (Runnable) applicationContext.getBean(Class .forName(jobClassName)); Trigger trigger = new CronTrigger(jobConfig); taskScheduler.schedule(runnable, trigger); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } }
Example #18
Source File: ScheduledTest.java From WeBASE-Front with Apache License 2.0 | 5 votes |
@Test public void changeCron() { // ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler(); stopCron();// 先停止,在开启. future = threadPoolTaskScheduler.schedule(new MyRunnable(), new CronTrigger("*/10 * * * * *")); System.out.println("DynamicTaskController.changeCron()"); }
Example #19
Source File: ScheduledTaskTest.java From blackduck-alert with Apache License 2.0 | 5 votes |
@Test public void testNextFormattedRuntimeFutureDone() { Mockito.doReturn(future).when(taskScheduler).schedule(Mockito.any(), Mockito.any(CronTrigger.class)); Mockito.when(future.isDone()).thenReturn(true); task.scheduleExecution(validCronExpression); Optional<String> nextRunTime = task.getFormatedNextRunTime(); assertFalse(nextRunTime.isPresent()); }
Example #20
Source File: ScheduledTaskTest.java From blackduck-alert with Apache License 2.0 | 5 votes |
@Test public void testNextRuntimeFutureDone() { Mockito.doReturn(future).when(taskScheduler).schedule(Mockito.any(), Mockito.any(CronTrigger.class)); Mockito.when(future.isDone()).thenReturn(true); task.scheduleExecution(validCronExpression); Optional<Long> actualNextRun = task.getMillisecondsToNextRun(); assertFalse(actualNextRun.isPresent()); }
Example #21
Source File: ScheduledTaskTest.java From blackduck-alert with Apache License 2.0 | 5 votes |
@Test public void testNextFormattedRuntimeNullFuture() { Mockito.doReturn(null).when(taskScheduler).schedule(Mockito.any(), Mockito.any(CronTrigger.class)); task.scheduleExecution(validCronExpression); Optional<String> nextRunTime = task.getFormatedNextRunTime(); assertFalse(nextRunTime.isPresent()); }
Example #22
Source File: ScheduledTaskTest.java From blackduck-alert with Apache License 2.0 | 5 votes |
@Test public void testScheduleCronExecutionUnschedule() { Mockito.doReturn(future).when(taskScheduler).schedule(Mockito.any(), Mockito.any(CronTrigger.class)); task.scheduleExecution(validCronExpression); Mockito.verify(taskScheduler).schedule(Mockito.any(), Mockito.any(CronTrigger.class)); task.scheduleExecution(ScheduledTask.STOP_SCHEDULE_EXPRESSION); Mockito.verify(taskScheduler, Mockito.times(1)).schedule(Mockito.any(), Mockito.any(CronTrigger.class)); }
Example #23
Source File: ScheduledSubscriptionsService.java From gravitee-management-rest-api with Apache License 2.0 | 5 votes |
@Override protected void doStart() throws Exception { if (enabled) { super.doStart(); logger.info("Subscriptions Refresher service has been initialized with cron [{}]", cronTrigger); scheduler.schedule(this, new CronTrigger(cronTrigger)); } else { logger.warn("Subscriptions Refresher service has been disabled"); } }
Example #24
Source File: TaskManager.java From Taroco-Scheduler with GNU General Public License v2.0 | 5 votes |
/** * 启动动态定时任务 * 支持: * 1 cron时间表达式,立即执行 * 2 startTime + period,指定时间,定时进行 * 3 period,定时进行,立即开始 * 4 startTime,指定时间执行 * * @param targetBean 目标bean名称 * @param targetMethod 方法 * @param cronExpression cron表达式 * @param startTime 指定执行时间 * @param period 定时进行,立即开始 * @param params 给方法传递的参数 * @param extKeySuffix 任务后缀名 */ private void scheduleTask(String targetBean, String targetMethod, String cronExpression, Date startTime, Long period, String params, String extKeySuffix) { String scheduleKey = ScheduleUtil.buildScheduleKey(targetBean, targetMethod, extKeySuffix); try { if (!SCHEDULE_FUTURES.containsKey(scheduleKey)) { ScheduledFuture<?> scheduledFuture = null; ScheduledMethodRunnable scheduledMethodRunnable = buildScheduledRunnable(targetBean, targetMethod, params, extKeySuffix); if (scheduledMethodRunnable != null) { if (StringUtils.isNotEmpty(cronExpression)) { Trigger trigger = new CronTrigger(cronExpression); scheduledFuture = zkClient.getTaskGenerator().schedule(scheduledMethodRunnable, trigger); } else if (startTime != null) { if (period > 0) { scheduledFuture = zkClient.getTaskGenerator().scheduleAtFixedRate(scheduledMethodRunnable, startTime, period); } else { scheduledFuture = zkClient.getTaskGenerator().schedule(scheduledMethodRunnable, startTime); } } else if (period > 0) { scheduledFuture = zkClient.getTaskGenerator().scheduleAtFixedRate(scheduledMethodRunnable, period); } if (null != scheduledFuture) { SCHEDULE_FUTURES.put(scheduleKey, scheduledFuture); log.info("成功启动动态任务, bean=" + targetBean + ", method=" + targetMethod + ", params=" + params + ", extKeySuffix=" + extKeySuffix); } } } } catch (Exception e) { log.error(e.getMessage(), e); } }
Example #25
Source File: DefaultSchedulingManager.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void scheduleJob( JobConfiguration jobConfiguration ) { if ( ifJobInSystemStop( jobConfiguration.getUid() ) ) { JobInstance jobInstance = new DefaultJobInstance( this, messageService, leaderManager ); if ( jobConfiguration.getUid() != null && !futures.containsKey( jobConfiguration.getUid() ) ) { log.info( String.format( "Scheduling job: %s", jobConfiguration ) ); ScheduledFuture<?> future = null; if ( jobConfiguration.getJobType().isCronSchedulingType() ) { future = jobScheduler.schedule( () -> jobInstance.execute( jobConfiguration ), new CronTrigger( jobConfiguration.getCronExpression() ) ); } else if ( jobConfiguration.getJobType().isFixedDelaySchedulingType() ) { future = jobScheduler.scheduleWithFixedDelay( () -> jobInstance.execute( jobConfiguration ), Instant.now().plusSeconds( DEFAULT_INITIAL_DELAY_S ), Duration.of( jobConfiguration.getDelay(), ChronoUnit.SECONDS ) ); } futures.put( jobConfiguration.getUid(), future ); log.info( String.format( "Scheduled job: %s", jobConfiguration ) ); } } }
Example #26
Source File: ScheduledSyncService.java From graviteeio-access-management with Apache License 2.0 | 5 votes |
@Override protected void doStart() throws Exception { if (enabled) { super.doStart(); logger.info("Sync service has been initialized with cron [{}]", cronTrigger); // Sync must start only when doStart() is invoked, that's the reason why we are not // using @Scheduled annotation on doSync() method. scheduler.schedule(this, new CronTrigger(cronTrigger)); } else { logger.warn("Sync service has been disabled"); } }
Example #27
Source File: ScheduledSyncService.java From graviteeio-access-management with Apache License 2.0 | 5 votes |
@Override protected void doStart() throws Exception { if (enabled) { super.doStart(); logger.info("Sync service has been initialized with cron [{}]", cronTrigger); // Sync must start only when doStart() is invoked, that's the reason why we are not // using @Scheduled annotation on doSync() method. scheduler.schedule(this, new CronTrigger(cronTrigger)); } else { logger.warn("Sync service has been disabled"); } }
Example #28
Source File: ZKScheduleManager.java From uncode-schedule with GNU General Public License v2.0 | 5 votes |
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) { TaskDefine taskDefine = getTaskDefine(task); if(trigger instanceof CronTrigger){ CronTrigger cronTrigger = (CronTrigger)trigger; taskDefine.setCronExpression(cronTrigger.getExpression()); LOGGER.info("spring task init------trigger:" + cronTrigger.getExpression()); } addTask(task, taskDefine); return super.schedule(taskWrapper(task), trigger); }
Example #29
Source File: DynamicTaskManager.java From uncode-schedule with GNU General Public License v2.0 | 5 votes |
/** * 启动定时任务 * 支持: * 1 cron时间表达式,立即执行 * 2 startTime + period,指定时间,定时进行 * 3 period,定时进行,立即开始 * 4 startTime,指定时间执行 * * @param targetBean * @param targetMethod * @param cronExpression * @param startTime * @param period */ public static void scheduleTask(String targetBean, String targetMethod, String cronExpression, Date startTime, long period, String params){ String scheduleKey = buildScheduleKey(targetBean, targetMethod); try { ScheduledFuture<?> scheduledFuture = null; ScheduledMethodRunnable scheduledMethodRunnable = buildScheduledRunnable(targetBean, targetMethod, params); if(scheduledMethodRunnable != null){ if (!SCHEDULE_FUTURES.containsKey(scheduleKey)) { if(StringUtils.isNotEmpty(cronExpression)){ Trigger trigger = new CronTrigger(cronExpression); scheduledFuture = ConsoleManager.getScheduleManager().schedule(scheduledMethodRunnable, trigger); }else if(startTime != null){ if(period > 0){ scheduledFuture = ConsoleManager.getScheduleManager().scheduleAtFixedRate(scheduledMethodRunnable, startTime, period); }else{ scheduledFuture = ConsoleManager.getScheduleManager().schedule(scheduledMethodRunnable, startTime); } }else if(period > 0){ scheduledFuture = ConsoleManager.getScheduleManager().scheduleAtFixedRate(scheduledMethodRunnable, period); } SCHEDULE_FUTURES.put(scheduleKey, scheduledFuture); LOGGER.debug("Building new schedule task, target bean "+ targetBean + " target method " + targetMethod + "."); } }else{ LOGGER.debug("Bean name is not exists."); } } catch (Exception e) { LOGGER.error(e.getMessage(), e); } }
Example #30
Source File: RecoverScheduleJob.java From tcc-transaction with Apache License 2.0 | 5 votes |
@PostConstruct public void init() { threadPoolTaskScheduler.schedule(new Runnable() { @Override public void run() { transactionRecovery.startRecover(); } }, new CronTrigger(tccTransactionConfigurator.getRecoverConfig().getCronExpression())); }