Java Code Examples for org.apache.logging.log4j.core.util.CronExpression#getNextValidTimeAfter()

The following examples show how to use org.apache.logging.log4j.core.util.CronExpression#getNextValidTimeAfter() . 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: OfflineDataProcessingExecutor.java    From Insights with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * Checks whether query is scheduled to run or not depending on runSchedule
 * and lastRunTime
 * 
 * @param runSchedule
 * @param lastRunTime
 * @return
 */
public Boolean isQueryScheduledToRun(Long runSchedule, String lastRunTime, String cronSchedule) {
	// if lastExecutionTime property is not added in the json file, we'll
	// execute the query by default
	if (lastRunTime == null && (cronSchedule == null || cronSchedule.trim().length() == 0)) {
		return Boolean.TRUE;
	}
	ZonedDateTime dateTime = null;
	ZonedDateTime now = ZonedDateTime.now(InsightsUtils.zoneId);
	Long timeDifferenceInMinutes = null;
	if (lastRunTime != null && !lastRunTime.isEmpty()) {
		dateTime = ZonedDateTime.parse(lastRunTime, formatter);
	}
	if(cronSchedule != null && cronSchedule.trim().length() > 0) {
		try {
			//"0 0 0 1 * ?"
			CronExpression convert = new CronExpressionConverter().convert(cronSchedule);
			if(dateTime == null) {
				dateTime = now.minusDays(1); //If the last run time not present, compare the cron time against last 24 hours.
			}
			Date cronDate = convert.getNextValidTimeAfter(Date.from(dateTime.toInstant()));
			if(cronDate.before(new Date())) {
				return Boolean.TRUE;
			}
		} catch (Exception e) {
			log.error("Unable to parse the CRON expression: "+cronSchedule, e);
		}
	}else {
		if (dateTime != null && now != null) {
			Duration d = Duration.between(dateTime, now);
			timeDifferenceInMinutes = d.abs().toMinutes();
		}
		if (timeDifferenceInMinutes > runSchedule) {
			return Boolean.TRUE;
		}
	}
	return Boolean.FALSE;
}
 
Example 2
Source File: ConfigurationScheduler.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates and executes an action that first based on a cron expression.
 * @param cronExpression the cron expression describing the schedule.
 * @param startDate The time to use as the time to begin the cron expression. Defaults to the current date and time.
 * @param command The Runnable to run,
 * @return a ScheduledFuture representing the next time the command will run.
 */
public CronScheduledFuture<?> scheduleWithCron(final CronExpression cronExpression, final Date startDate, final Runnable command) {
    final Date fireDate = cronExpression.getNextValidTimeAfter(startDate == null ? new Date() : startDate);
    final CronRunnable runnable = new CronRunnable(command, cronExpression);
    final ScheduledFuture<?> future = schedule(runnable, nextFireInterval(fireDate), TimeUnit.MILLISECONDS);
    final CronScheduledFuture<?> cronScheduledFuture = new CronScheduledFuture<>(future, fireDate);
    runnable.setScheduledFuture(cronScheduledFuture);
    LOGGER.debug("{} scheduled cron expression {} to fire at {}", name, cronExpression.getCronExpression(), fireDate);
    return cronScheduledFuture;
}