/** * Build a {@link CronTrigger} from the pattern provided in the given time zone. * @param expression a space-separated list of time fields, following cron * expression conventions * @param timeZone a time zone in which the trigger times will be generated */ public CronTrigger(String expression, TimeZone timeZone) { this.sequenceGenerator = new CronSequenceGenerator(expression, timeZone); }
/** * Build a {@link CronTrigger} from the pattern provided in the default time zone. * @param expression a space-separated list of time fields, following cron * expression conventions */ public CronTrigger(String expression) { this.sequenceGenerator = new CronSequenceGenerator(expression); }
/** * Build a {@link CronTrigger} from the pattern provided in the default time zone. * @param expression a space-separated list of time fields, following cron * expression conventions */ public CronTrigger(String expression) { this.sequenceGenerator = new CronSequenceGenerator(expression); }
/** * Build a {@link CronTrigger} from the pattern provided in the given time zone. * @param expression a space-separated list of time fields, following cron * expression conventions * @param timeZone a time zone in which the trigger times will be generated */ public CronTrigger(String expression, TimeZone timeZone) { this.sequenceGenerator = new CronSequenceGenerator(expression, timeZone); }
/** * Determine whether the specified expression represents a valid cron pattern. * @param expression the expression to evaluate * @return {@code true} if the given expression is a valid cron expression * @since 4.3 */ public static boolean isValidExpression(@Nullable String expression) { if (expression == null) { return false; } String[] fields = StringUtils.tokenizeToStringArray(expression, " "); if (!areValidCronFields(fields)) { return false; } try { new CronSequenceGenerator(expression, fields); return true; } catch (IllegalArgumentException ex) { return false; } }
/** * Determine whether the specified expression represents a valid cron pattern. * @param expression the expression to evaluate * @return {@code true} if the given expression is a valid cron expression * @since 4.3 */ public static boolean isValidExpression(@Nullable String expression) { if (expression == null) { return false; } String[] fields = StringUtils.tokenizeToStringArray(expression, " "); if (!areValidCronFields(fields)) { return false; } try { new CronSequenceGenerator(expression, fields); return true; } catch (IllegalArgumentException ex) { return false; } }
@Override public void init(String schedule) { if (logger.isDebugEnabled()) { logger.debug("Initializing " + this.getClass().getSimpleName() + " with " + schedule); } if (schedule == null || schedule.isEmpty()) { throw new GemFireConfigException("Missing configuration: " + SCHEDULE); } try { generator = new CronSequenceGenerator(schedule); } catch (Exception e) { throw new GemFireConfigException("Cron expression could not be parsed: " + schedule, e); } submitNext(); }
@Test(expected = IllegalArgumentException.class) public void withNegativeIncrement() { new CronSequenceGenerator("*/-1 * * * * *").next(new Date(2012, 6, 1, 9, 0)); }
@Test(expected = IllegalArgumentException.class) public void withInvertedHourRange() { new CronSequenceGenerator("* * 6-5 * * *").next(new Date(2012, 6, 1, 9, 0)); }
@Test(expected = IllegalArgumentException.class) public void with0Increment() { new CronSequenceGenerator("*/0 * * * * *").next(new Date(2012, 6, 1, 9, 0)); }
@Test(expected = IllegalArgumentException.class) public void withInvertedMinuteRange() { new CronSequenceGenerator("* 6-5 * * * *").next(new Date(2012, 6, 1, 9, 0)); }
@Test public void withSameMinuteRange() { new CronSequenceGenerator("* 6-6 * * * *").next(new Date(2012, 6, 1, 9, 0)); }
@Test public void withSameHourRange() { new CronSequenceGenerator("* * 6-6 * * *").next(new Date(2012, 6, 1, 9, 0)); }
@Test public void at50Seconds() { assertEquals(new Date(2012, 6, 2, 1, 0), new CronSequenceGenerator("*/15 * 1-4 * * *").next(new Date(2012, 6, 1, 9, 53, 50))); }
@Test public void at0Seconds() { assertEquals(new Date(2012, 6, 2, 1, 0), new CronSequenceGenerator("*/15 * 1-4 * * *").next(new Date(2012, 6, 1, 9, 53))); }
@Test public void at0Minutes() { assertEquals(new Date(2012, 6, 2, 1, 0), new CronSequenceGenerator("0 */2 1-4 * * *").next(new Date(2012, 6, 1, 9, 0))); }
/** * @param cron cron expression to validate. * @throws IllegalArgumentException if cron expression is invalid * @see CronSequenceGenerator */ public static void validateCron(String cron) { //Internally, we use the org.springframework.scheduling.support.CronTrigger which has a different syntax than the official crontab syntax. //Therefore we use the internal validation of CronTrigger new CronSequenceGenerator(cron); }
/** * Build a {@link CronTrigger} from the pattern provided in the given time zone. * @param expression a space-separated list of time fields, following cron * expression conventions * @param timeZone a time zone in which the trigger times will be generated */ public CronTrigger(String expression, TimeZone timeZone) { this.sequenceGenerator = new CronSequenceGenerator(expression, timeZone); }
/** * 得到 {@link CronSequenceGenerator} */ public CronSequenceGenerator toCron () { return new CronSequenceGenerator( this.toCronString() ); }
@Test(enabled = false) public void testCronExpression() { CronSequenceGenerator cron1 = new CronSequenceGenerator("0 0 23 31 12 *"); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, 2); // add two days to current date SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss"); log.error("current date " + sdf.format(cal.getTime())); log.error("Next cron trigger date cron1 " + cron1.next(cal.getTime())); } }