Example usage for org.springframework.scheduling.support CronSequenceGenerator CronSequenceGenerator

List of usage examples for org.springframework.scheduling.support CronSequenceGenerator CronSequenceGenerator

Introduction

In this page you can find the example usage for org.springframework.scheduling.support CronSequenceGenerator CronSequenceGenerator.

Prototype

private CronSequenceGenerator(String expression, String[] fields) 

Source Link

Usage

From source file:org.arrow.util.TriggerUtils.java

/**
 * Indicates if the given scheduler string is in cron format.
 * /*from   ww  w  .  j a  v  a2  s  .  c  o  m*/
 * @param str the cron string
 * @return boolean
 */
public static boolean isCron(String str) {
    try {
        new CronSequenceGenerator(str, TimeZone.getDefault());
        return true;
    } catch (IllegalArgumentException ex) {
        return false;
    }
}

From source file:com.haulmont.cuba.core.app.scheduling.Scheduling.java

protected long calculateNextCronDate(ScheduledTask task, long date, long currentDate, long frame) {
    StopWatch sw = new Slf4JStopWatch("Cron next date calculations");
    CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(task.getCron(),
            getCurrentTimeZone());/*from  w w w  .  j  av  a2 s  .  c o m*/
    //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());
    sw.stop();
    return currentStart.getTime();
}

From source file:ch.algotrader.service.PortfolioServiceImpl.java

/**
 * {@inheritDoc}/*  w  w w  . jav  a2  s. co m*/
 */
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void restorePortfolioValues(final Strategy strategy, final Date fromDate, final Date toDate) {

    Validate.notNull(strategy, "Strategy is null");
    Validate.notNull(fromDate, "From date is null");
    Validate.notNull(toDate, "To date is null");

    // delete existing portfolio values;
    List<PortfolioValue> portfolioValues = this.portfolioValueDao.findByStrategyAndMinDate(strategy.getName(),
            fromDate);

    if (portfolioValues.size() > 0) {

        this.portfolioValueDao.deleteAll(portfolioValues);

        // need to flush since new portfoliovalues will be created with same date and strategy
        this.sessionFactory.getCurrentSession().flush();
    }

    // init cron
    CronSequenceGenerator cron = new CronSequenceGenerator("0 0 * * * 1-5", TimeZone.getDefault());

    // group PortfolioValues by strategyId and date
    Map<MultiKey<Long>, PortfolioValue> portfolioValueMap = new HashMap<>();

    // create portfolioValues for all cron time slots
    Date date = cron.next(DateUtils.addHours(fromDate, -1));
    while (date.compareTo(toDate) <= 0) {

        PortfolioValue portfolioValue = getPortfolioValue(strategy.getName(), date);
        if (portfolioValue.getNetLiqValueDouble() == 0) {
            date = cron.next(date);
            continue;
        } else {
            MultiKey<Long> key = new MultiKey<>(strategy.getId(), date.getTime());
            portfolioValueMap.put(key, portfolioValue);

            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("processed portfolioValue for {} {}", strategy.getName(), date);
            }

            date = cron.next(date);
        }
    }

    // save values for all cashFlows
    List<Transaction> transactions = this.transactionDao.findCashflowsByStrategyAndMinDate(strategy.getName(),
            fromDate);
    for (Transaction transaction : transactions) {

        // only process performanceRelevant transactions
        if (!transaction.isPerformanceRelevant()) {
            continue;
        }

        // do not save before fromDate
        if (transaction.getDateTime().compareTo(fromDate) < 0) {
            continue;
        }

        // if there is an existing PortfolioValue, add the cashFlow
        MultiKey<Long> key = new MultiKey<>(transaction.getStrategy().getId(),
                transaction.getDateTime().getTime());
        if (portfolioValueMap.containsKey(key)) {
            PortfolioValue portfolioValue = portfolioValueMap.get(key);
            if (portfolioValue.getCashFlow() != null) {
                portfolioValue.setCashFlow(portfolioValue.getCashFlow().add(transaction.getGrossValue()));
            } else {
                portfolioValue.setCashFlow(transaction.getGrossValue());
            }
        } else {
            PortfolioValue portfolioValue = getPortfolioValue(transaction.getStrategy().getName(),
                    transaction.getDateTime());
            portfolioValue.setCashFlow(transaction.getGrossValue());
            portfolioValueMap.put(key, portfolioValue);
        }

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("processed portfolioValue for {} {} cashflow {}", transaction.getStrategy().getName(),
                    transaction.getDateTime(), transaction.getGrossValue());
        }
    }

    // perisist the PortfolioValues
    this.portfolioValueDao.saveAll(portfolioValueMap.values());
}