/** * Returns the next date/time <I>after</I> the given date/time which satisfies the cron * expression. * * @param date the date/time at which to begin the search for the next valid date/time * @return the next valid date/time */ public Date getNextValidTimeAfter(Date date) { return getTimeAfter(date); }
/** * Indicates whether the specified cron expression can be parsed into a valid cron expression * * @param cronExpression the expression to evaluate * @return a boolean indicating whether the given expression is a valid cron expression */ public static boolean isValidExpression(String cronExpression) { try { new CronExpression(cronExpression); } catch (ParseException pe) { return false; } return true; }
/** * Constructs a new CronThreadPoolExecutor. * * @param corePoolSize the pool size * @param handler the handler for rejected executions */ public CronThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler handler) { super(corePoolSize, handler); this.cronJobWatchDogs = new ArrayList<>(); this.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); }
/** * Constructs a new {@code CronExpression} as a copy of an existing instance. * * @param expression The existing cron expression to be copied */ public CronExpression(CronExpression expression) { /* * We don't call the other constructor here since we need to swallow the * ParseException. We also elide some of the sanity checking as it is * not logically trippable. */ this.cronExpression = expression.getCronExpression(); try { buildExpression(cronExpression); } catch (ParseException ex) { throw new AssertionError(); } if (expression.getTimeZone() != null) { setTimeZone((TimeZone) expression.getTimeZone().clone()); } }
throw new NullPointerException(); setCorePoolSize(getCorePoolSize() + 1); Runnable scheduleTask = new Runnable() { return this.submit(scheduleTask);
/** * Indicates whether the given date satisfies the cron expression. Note that milliseconds are * ignored, so two Dates falling on different milliseconds of the same second will always have the * same result here. * * @param date the date to evaluate * @return a boolean indicating whether the given date satisfies the cron expression */ public boolean isSatisfiedBy(Date date) { Calendar testDateCal = Calendar.getInstance(getTimeZone()); testDateCal.setTime(date); testDateCal.set(Calendar.MILLISECOND, 0); Date originalDate = testDateCal.getTime(); testDateCal.add(Calendar.SECOND, -1); Date timeAfter = getTimeAfter(testDateCal.getTime()); return ((timeAfter != null) && (timeAfter.equals(originalDate))); }
@Override public void scheduleCron(Runnable runnable, String cron) { if (cron == null || cron.isEmpty()) { throw new ConfigurationException("Cron parameter can't be null"); } try { CronExpression expression = new CronExpression(cron); service.schedule(runnable, expression); LOG.debug("Schedule method {} with cron {} schedule", runnable, cron); } catch (ParseException e) { LOG.error(e.getLocalizedMessage(), e); throw new ConfigurationException(e.getLocalizedMessage()); } }
private void readObject(java.io.ObjectInputStream stream) throws java.io.IOException, ClassNotFoundException { stream.defaultReadObject(); try { buildExpression(cronExpression); } catch (Exception ignore) { } // never happens }
protected int getNumericValue(String s, int i) { int endOfVal = findNextWhiteSpace(i, s); String val = s.substring(i, endOfVal); return Integer.parseInt(val); }
@Override public void suspend() throws InterruptedException { try { launcher.shutdown(); } catch (RuntimeException ex) { LOG.error("Failed to stop cron job thread pool. Cause: " + ex.getMessage()); } }
@Override public void scheduleAtFixedRate( Runnable runnable, long initialDelay, long period, TimeUnit unit) { if (period <= 0) { LOG.debug( "Method {} with fixed rate has not been scheduled (period <= 0). Initial delay {} period {} unit {}", runnable, initialDelay, period, unit); return; } service.scheduleAtFixedRate(runnable, initialDelay, period, unit); LOG.debug( "Schedule method {} with fixed rate. Initial delay {} period {} unit {}", runnable, initialDelay, period, unit); } }
@Override public void scheduleWithFixedDelay( Runnable runnable, long initialDelay, long delay, TimeUnit unit) { if (delay <= 0) { LOG.debug( "Method {} has not been scheduled (delay <= 0). Initial delay {} delay {} unit {}", runnable, initialDelay, delay, unit); return; } service.scheduleWithFixedDelay(runnable, initialDelay, delay, unit); LOG.debug( "Schedule method {} with fixed initial delay {} delay {} unit {}", runnable, initialDelay, delay, unit); }
protected int getLastDayOfMonth(int monthNum, int year) { switch (monthNum) { case 1: return 31; case 2: return (isLeapYear(year)) ? 29 : 28; case 3: return 31; case 4: return 30; case 5: return 31; case 6: return 30; case 7: return 31; case 8: return 31; case 9: return 30; case 10: return 31; case 11: return 30; case 12: return 31; default: throw new IllegalArgumentException("Illegal month number: " + monthNum); } }
protected ValueSet getValue(int v, String s, int i) { char c = s.charAt(i); StringBuilder s1 = new StringBuilder(String.valueOf(v)); while (c >= '0' && c <= '9') { s1.append(c); i++; if (i >= s.length()) { break; } c = s.charAt(i); } ValueSet val = new ValueSet(); val.pos = (i < s.length()) ? i : i + 1; val.value = Integer.parseInt(s1.toString()); return val; }
private void launch(Object object, Method method, ScheduleCron annotation) { Launcher launcher = launcherProvider.get(); launcher.scheduleCron( new LoggedRunnable(object, method), annotation.cronParameterName().isEmpty() ? annotation.cron() : getValue(String.class, annotation.cronParameterName())); }
public static void validateExpression(String cronExpression) throws ParseException { new CronExpression(cronExpression); }
/** * Constructs a new CronThreadPoolExecutor. * * @param corePoolSize the pool size */ public CronThreadPoolExecutor(int corePoolSize) { super(corePoolSize); this.cronJobWatchDogs = new ArrayList<>(); this.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); }
/** * Constructs a new <CODE>CronExpression</CODE> based on the specified parameter. * * @param cronExpression String representation of the cron expression the new object should * represent * @throws java.text.ParseException if the string expression cannot be parsed into a valid <CODE> * CronExpression</CODE> */ public CronExpression(String cronExpression) throws ParseException { if (cronExpression == null) { throw new IllegalArgumentException("cronExpression cannot be null"); } this.cronExpression = cronExpression.toUpperCase(Locale.US); buildExpression(this.cronExpression); }
@Override @Deprecated public Object clone() { return new CronExpression(this); } }
/** * Constructs a new CronThreadPoolExecutor. * * @param corePoolSize the pool size * @param threadFactory the thread factory */ public CronThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory) { super(corePoolSize, threadFactory); this.cronJobWatchDogs = new ArrayList<>(); this.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); }