Java Code Examples for org.springframework.scheduling.support.CronSequenceGenerator#next()

The following examples show how to use org.springframework.scheduling.support.CronSequenceGenerator#next() . 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: Scheduling.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected long calculateNextCronDate(ScheduledTask task, long date, long currentDate, long frame) {
    CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(task.getCron(), getCurrentTimeZone());
    //if last start = 0 (task never has run) or to far in the past, we use (NOW - FRAME) timestamp for pivot time
    //this approach should work fine cause cron works with absolute time
    long pivotPreviousTime = Math.max(date, currentDate - frame);

    Date currentStart = null;
    Date nextDate = cronSequenceGenerator.next(new Date(pivotPreviousTime));
    while (nextDate.getTime() < currentDate) {//if next date is in past try to find next date nearest to now
        currentStart = nextDate;
        nextDate = cronSequenceGenerator.next(nextDate);
    }

    if (currentStart == null) {
        currentStart = nextDate;
    }
    log.trace("{}\n now={} frame={} currentStart={} lastStart={} cron={}",
            task, currentDate, frame, currentStart, task.getCron());
    return currentStart.getTime();
}
 
Example 2
Source File: DateService.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public boolean isTrigger(TimeAlert alert, long monitorUpdateRate, ZonedDateTime currentTime) {
    try {
        String timeZone = alert.getTimeZone();
        CronSequenceGenerator cronExpression = getCronExpression(alert.getCron());
        ZonedDateTime zonedCurrentTime = dateTimeService.getZonedDateTime(currentTime.toInstant(), timeZone);
        LocalDateTime startTimeOfTheMonitorInterval = zonedCurrentTime.toLocalDateTime().minus(monitorUpdateRate, ChronoUnit.MILLIS);
        Date startDate = Date.from(startTimeOfTheMonitorInterval.toInstant(currentTime.getOffset()));
        Date nextTime = cronExpression.next(startDate);
        ZonedDateTime zonedNextTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(nextTime.getTime()), currentTime.getZone()).atZone(ZoneId.of(timeZone));
        long interval = (zonedCurrentTime.toEpochSecond() - zonedNextTime.toEpochSecond()) * TimeUtil.SECOND_TO_MILLISEC;

        boolean triggerReady = interval >= 0L && interval < monitorUpdateRate;
        if (triggerReady) {
            LOGGER.info("Time alert '{}' firing at '{}' compared to current time '{}' in timezone '{}'",
                    alert.getName(), zonedNextTime, zonedCurrentTime, timeZone);
        }
        return triggerReady;
    } catch (ParseException e) {
        LOGGER.error("Invalid cron expression '{}', cluster '{}'", e.getMessage(), alert.getCluster().getStackCrn());
        return false;
    }
}
 
Example 3
Source File: CronUtils.java    From chronus with Apache License 2.0 4 votes vote down vote up
public static Date getNextDateAfterNow(String cronExpression, Date now) {
    CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cronExpression);
    Date tmpDate = cronSequenceGenerator.next(now);
    return tmpDate;
}