📄 crontrigger.java
字号:
* </p> */ public void updateAfterMisfire(org.quartz.Calendar cal) { int instr = getMisfireInstruction(); if (instr == MISFIRE_INSTRUCTION_SMART_POLICY) { instr = MISFIRE_INSTRUCTION_FIRE_ONCE_NOW; } if (instr == MISFIRE_INSTRUCTION_DO_NOTHING) { Date newFireTime = getFireTimeAfter(new Date()); while (newFireTime != null && cal != null && !cal.isTimeIncluded(newFireTime.getTime())) { newFireTime = getFireTimeAfter(newFireTime); } setNextFireTime(newFireTime); } else if (instr == MISFIRE_INSTRUCTION_FIRE_ONCE_NOW) { setNextFireTime(new Date()); } } /** * <p> * Determines whether the date and (optionally) time of the given Calendar * instance falls on a scheduled fire-time of this trigger. * </p> * * <p> * Equivalent to calling <code>willFireOn(cal, false)</code>. * </p> * * @param test the date to compare * * @see #willFireOn(Calendar, boolean) */ public boolean willFireOn(Calendar test) { return willFireOn(test, false); } /** * <p> * Determines whether the date and (optionally) time of the given Calendar * instance falls on a scheduled fire-time of this trigger. * </p> * * <p> * Note that the value returned is NOT validated against the related * org.quartz.Calendar (if any) * </p> * * @param test the date to compare * @param dayOnly if set to true, the method will only determine if the * trigger will fire during the day represented by the given Calendar * (hours, minutes and seconds will be ignored). * @see #willFireOn(Calendar) */ public boolean willFireOn(Calendar test, boolean dayOnly) { test = (Calendar) test.clone(); test.set(Calendar.MILLISECOND, 0); // don't compare millis. if(dayOnly) { test.set(Calendar.HOUR, 0); test.set(Calendar.MINUTE, 0); test.set(Calendar.SECOND, 0); } Date testTime = test.getTime(); Date fta = getFireTimeAfter(new Date(test.getTime().getTime() - 1000)); Calendar p = Calendar.getInstance(test.getTimeZone()); p.setTime(fta); int year = p.get(Calendar.YEAR); int month = p.get(Calendar.MONTH); int day = p.get(Calendar.DATE); if(dayOnly) { return (year == test.get(Calendar.YEAR) && month == test.get(Calendar.MONTH) && day == test.get(Calendar.DATE)); } while(fta.before(testTime)) { fta = getFireTimeAfter(fta); } if(fta.equals(testTime)) { return true; } return false; } /** * <p> * Called after the <code>{@link Scheduler}</code> has executed the * <code>{@link org.quartz.JobDetail}</code> associated with the <code>Trigger</code> * in order to get the final instruction code from the trigger. * </p> * * @param context * is the <code>JobExecutionContext</code> that was used by the * <code>Job</code>'s<code>execute(xx)</code> method. * @param result * is the <code>JobExecutionException</code> thrown by the * <code>Job</code>, if any (may be null). * @return one of the Trigger.INSTRUCTION_XXX constants. * * @see #INSTRUCTION_NOOP * @see #INSTRUCTION_RE_EXECUTE_JOB * @see #INSTRUCTION_DELETE_TRIGGER * @see #INSTRUCTION_SET_TRIGGER_COMPLETE * @see #triggered(Calendar) */ public int executionComplete(JobExecutionContext context, JobExecutionException result) { if (result != null && result.refireImmediately()) { return INSTRUCTION_RE_EXECUTE_JOB; } if (result != null && result.unscheduleFiringTrigger()) { return INSTRUCTION_SET_TRIGGER_COMPLETE; } if (result != null && result.unscheduleAllTriggers()) { return INSTRUCTION_SET_ALL_JOB_TRIGGERS_COMPLETE; } if (!mayFireAgain()) { return INSTRUCTION_DELETE_TRIGGER; } return INSTRUCTION_NOOP; } /** * <p> * Called when the <code>{@link Scheduler}</code> has decided to 'fire' * the trigger (execute the associated <code>Job</code>), in order to * give the <code>Trigger</code> a chance to update itself for its next * triggering (if any). * </p> * * @see #executionComplete(JobExecutionContext, JobExecutionException) */ public void triggered(org.quartz.Calendar calendar) { previousFireTime = nextFireTime; nextFireTime = getFireTimeAfter(nextFireTime); while (nextFireTime != null && calendar != null && !calendar.isTimeIncluded(nextFireTime.getTime())) { nextFireTime = getFireTimeAfter(nextFireTime); } } /** * * @see org.quartz.Trigger#updateWithNewCalendar(org.quartz.Calendar, long) */ public void updateWithNewCalendar(org.quartz.Calendar calendar, long misfireThreshold) { nextFireTime = getFireTimeAfter(previousFireTime); Date now = new Date(); do { while (nextFireTime != null && calendar != null && !calendar.isTimeIncluded(nextFireTime.getTime())) { nextFireTime = getFireTimeAfter(nextFireTime); } if(nextFireTime != null && nextFireTime.before(now)) { long diff = now.getTime() - nextFireTime.getTime(); if(diff >= misfireThreshold) { nextFireTime = getFireTimeAfter(nextFireTime); continue; } } }while(false); } /** * <p> * Called by the scheduler at the time a <code>Trigger</code> is first * added to the scheduler, in order to have the <code>Trigger</code> * compute its first fire time, based on any associated calendar. * </p> * * <p> * After this method has been called, <code>getNextFireTime()</code> * should return a valid answer. * </p> * * @return the first time at which the <code>Trigger</code> will be fired * by the scheduler, which is also the same value <code>getNextFireTime()</code> * will return (until after the first firing of the <code>Trigger</code>). * </p> */ public Date computeFirstFireTime(org.quartz.Calendar calendar) { nextFireTime = getFireTimeAfter(new Date(getStartTime().getTime() - 1000l)); while (nextFireTime != null && calendar != null && !calendar.isTimeIncluded(nextFireTime.getTime())) { nextFireTime = getFireTimeAfter(nextFireTime); } return nextFireTime; } public String getExpressionSummary() { return cronEx == null ? null : cronEx.getExpressionSummary(); } //////////////////////////////////////////////////////////////////////////// // // Computation Functions // //////////////////////////////////////////////////////////////////////////// protected Date getTimeAfter(Date afterTime) { return (cronEx == null) ? null : cronEx.getTimeAfter(afterTime); } /** * NOT YET IMPLEMENTED: Returns the time before the given time * that this <code>CronTrigger</code> will fire. */ protected Date getTimeBefore(Date endTime) { return (cronEx == null) ? null : cronEx.getTimeBefore(endTime); } public static void main(String[] args) // TODO: remove method after good // unit testing throws Exception { String expr = "15 10 0/4 * * ?"; if(args != null && args.length > 0 && args[0] != null) { expr = args[0]; } CronTrigger ct = new CronTrigger("t", "g", "j", "g", new Date(), null, expr); ct.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); System.err.println(ct.getExpressionSummary()); System.err.println("tz=" + ct.getTimeZone().getID()); System.err.println(); java.util.List times = TriggerUtils.computeFireTimes(ct, null, 25); for (int i = 0; i < times.size(); i++) { System.err.println("firetime = " + times.get(i)); } Calendar tt = Calendar.getInstance(); tt.set(Calendar.DATE, 17); tt.set(Calendar.MONTH, 5 - 1); tt.set(Calendar.HOUR, 11); tt.set(Calendar.MINUTE, 0); tt.set(Calendar.SECOND, 7); System.err.println("\nWill fire on: " + tt.getTime() + " -- " + ct.willFireOn(tt, false)); // CRON Expression: 0 0 9 * * ?//// TimeZone.getDefault().getDisplayName() = Central African Time// TimeZone.getDefault().getID() = Africa/Harare ////// System.err.println();//// System.err.println();//// System.err.println();//// System.err.println("Daylight test:");//////// CronTrigger trigger = new CronTrigger();//////// TimeZone timeZone = TimeZone.getTimeZone("America/Los_Angeles");//// // TimeZone timeZone = TimeZone.getDefault();//////// trigger.setTimeZone(timeZone);//// trigger.setCronExpression("0 0 1 ? 4 *");//////// Date start = new Date(1056319200000L);//// Date end = new Date(1087682399000L);//////// trigger.setStartTime(start);//// trigger.setEndTime(end);//////// Date next = new Date(1056232800000L);//// while (next != null) {//// next = trigger.getFireTimeAfter(next);//// if (next != null) {//// Calendar cal = Calendar.getInstance();//// cal.setTimeZone(timeZone);//// cal.setTime(next);//// System.err.println(cal.get(Calendar.MONTH) + "/"//// + cal.get(Calendar.DATE) + "/" + cal.get(Calendar.YEAR)//// + " - " + cal.get(Calendar.HOUR_OF_DAY) + ":"//// + cal.get(Calendar.MINUTE));//// }//// }//////// System.err.println();//// System.err.println();//// System.err.println();//// System.err.println("Midnight test:");//////// trigger = new CronTrigger();//////// timeZone = TimeZone.getTimeZone("Asia/Jerusalem");//// // timeZone = TimeZone.getTimeZone("America/Los_Angeles");//// // TimeZone timeZone = TimeZone.getDefault();//////// trigger.setTimeZone(timeZone);//// trigger.setCronExpression("0 /15 * ? 4 *");//////// start = new Date(1056319200000L);//// end = new Date(1087682399000L);//////// trigger.setStartTime(start);//// trigger.setEndTime(end);//////// next = new Date(1056232800000L);//// while (next != null) {//// next = trigger.getFireTimeAfter(next);//// if (next != null) {//// Calendar cal = Calendar.getInstance();//// cal.setTimeZone(timeZone);//// cal.setTime(next);//// System.err.println(cal.get(Calendar.MONTH) + "/"//// + cal.get(Calendar.DATE) + "/" + cal.get(Calendar.YEAR)//// + " - " + cal.get(Calendar.HOUR_OF_DAY) + ":"//// + cal.get(Calendar.MINUTE));//// }//// } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -