public static void scheduleForCRON(Job job) { if (job.getClass().isAnnotationPresent(On.class)) { String cron = ((On) (job.getClass().getAnnotation(On.class))).value(); if (cron.startsWith("cron.")) { cron = Play.configuration.getProperty(cron); } if (cron != null && !cron.equals("")) { try { Date now = new Date(); Date nextDate = Time.parseCRONExpression(cron); long delay = nextDate.getTime() - now.getTime(); executor.schedule((Callable) job, delay, TimeUnit.MILLISECONDS); job.executor = executor; } catch (Exception ex) { throw new UnexpectedException(ex); } } else { Logger.info("Skipping job %s, cron expression is not defined", job.getClass().getName()); } } }
public static <V> void scheduleForCRON(Job<V> job) { if (!job.getClass().isAnnotationPresent(On.class)) { return; } String cron = job.getClass().getAnnotation(On.class).value(); if (cron.startsWith("cron.")) { cron = Play.configuration.getProperty(cron); } cron = Expression.evaluate(cron, cron).toString(); if (cron == null || "".equals(cron) || "never".equalsIgnoreCase(cron)) { if (Logger.isEnabledFor("INFO")) Logger.info("Skipping job %s, cron expression is not defined", job.getClass().getName()); return; } try { Date now = new Date(); cron = Expression.evaluate(cron, cron).toString(); CronExpression cronExp = new CronExpression(cron); Date nextDate = cronExp.getNextValidTimeAfter(now); if (nextDate == null) { if (Logger.isEnabledFor("WARN")) Logger.warn( "The cron expression for job %s doesn't have any match in the future, will never be executed", job.getClass().getName()); return; } if (nextDate.equals(job.nextPlannedExecution)) { // Bug #13: avoid running the job twice for the same time // (happens when we end up running the job a few minutes before the planned time) Date nextInvalid = cronExp.getNextInvalidTimeAfter(nextDate); nextDate = cronExp.getNextValidTimeAfter(nextInvalid); } job.nextPlannedExecution = nextDate; executor.schedule( (Callable<V>) job, nextDate.getTime() - now.getTime(), TimeUnit.MILLISECONDS); job.executor = executor; } catch (Exception ex) { throw new UnexpectedException(ex); } }