Constructor: |
public CronTrigger() {
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Constructors.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
super();
setStartTime(new Date());
setTimeZone(TimeZone.getDefault());
}
Create a CronTrigger with no settings.
The start-time will also be set to the current time, and the time zone
will be set the the system's default time zone.
|
public CronTrigger(String name,
String group) {
super(name, group);
setStartTime(new Date());
setTimeZone(TimeZone.getDefault());
}
Create a CronTrigger with the given name and group.
The start-time will also be set to the current time, and the time zone
will be set the the system's default time zone.
|
public CronTrigger(String name,
String group,
String cronExpression) throws ParseException {
super(name, group);
setCronExpression(cronExpression);
setStartTime(new Date());
setTimeZone(TimeZone.getDefault());
}
Create a CronTrigger with the given name, group and
expression.
The start-time will also be set to the current time, and the time zone
will be set the the system's default time zone.
|
public CronTrigger(String name,
String group,
String jobName,
String jobGroup) {
super(name, group, jobName, jobGroup);
setStartTime(new Date());
setTimeZone(TimeZone.getDefault());
}
Create a CronTrigger with the given name and group, and
associated with the identified org.quartz.JobDetail .
The start-time will also be set to the current time, and the time zone
will be set the the system's default time zone.
|
public CronTrigger(String name,
String group,
String jobName,
String jobGroup,
String cronExpression) throws ParseException {
this(name, group, jobName, jobGroup, null, null, cronExpression,
TimeZone.getDefault());
}
Create a CronTrigger with the given name and group,
associated with the identified org.quartz.JobDetail ,
and with the given "cron" expression.
The start-time will also be set to the current time, and the time zone
will be set the the system's default time zone.
|
public CronTrigger(String name,
String group,
String jobName,
String jobGroup,
String cronExpression,
TimeZone timeZone) throws ParseException {
this(name, group, jobName, jobGroup, null, null, cronExpression,
timeZone);
}
Create a CronTrigger with the given name and group,
associated with the identified org.quartz.JobDetail ,
and with the given "cron" expression resolved with respect to the TimeZone .
|
public CronTrigger(String name,
String group,
String jobName,
String jobGroup,
Date startTime,
Date endTime,
String cronExpression) throws ParseException {
super(name, group, jobName, jobGroup);
setCronExpression(cronExpression);
if (startTime == null) {
startTime = new Date();
}
setStartTime(startTime);
if (endTime != null) {
setEndTime(endTime);
}
setTimeZone(TimeZone.getDefault());
}
Create a CronTrigger that will occur at the given time,
until the given end time.
If null, the start-time will also be set to the current time, the time
zone will be set the the system's default.
Parameters:
startTime -
A Date set to the time for the Trigger
to fire.
endTime -
A Date set to the time for the Trigger
to quit repeat firing.
|
public CronTrigger(String name,
String group,
String jobName,
String jobGroup,
Date startTime,
Date endTime,
String cronExpression,
TimeZone timeZone) throws ParseException {
super(name, group, jobName, jobGroup);
setCronExpression(cronExpression);
if (startTime == null) {
startTime = new Date();
}
setStartTime(startTime);
if (endTime != null) {
setEndTime(endTime);
}
if (timeZone == null) {
setTimeZone(TimeZone.getDefault());
} else {
setTimeZone(timeZone);
}
}
Create a CronTrigger with fire time dictated by the
cronExpression resolved with respect to the specified
timeZone occuring from the startTime until
the given endTime .
If null, the start-time will also be set to the current time. If null,
the time zone will be set to the system's default.
Parameters:
name -
of the Trigger
group -
of the Trigger
jobName -
name of the org.quartz.JobDetail
executed on firetime
jobGroup -
group of the org.quartz.JobDetail
executed on firetime
startTime -
A Date set to the earliest time for the Trigger
to start firing.
endTime -
A Date set to the time for the Trigger
to quit repeat firing.
cronExpression -
A cron expression dictating the firing sequence of the Trigger
timeZone -
Specifies for which time zone the cronExpression
should be interprted, i.e. the expression 0 0 10 * * ?, is
resolved to 10:00 am in this time zone.
Throws:
ParseException -
if the cronExpression is invalid.
|
Method from org.quartz.CronTrigger Detail: |
public Object clone() {
CronTrigger copy = (CronTrigger) super.clone();
copy.setCronExpression((CronExpression)cronEx.clone());
return copy;
}
|
public Date computeFirstFireTime(Calendar calendar) {
nextFireTime = getFireTimeAfter(new Date(getStartTime().getTime() - 1000l));
while (nextFireTime != null && calendar != null
&& !calendar.isTimeIncluded(nextFireTime.getTime())) {
nextFireTime = getFireTimeAfter(nextFireTime);
}
return nextFireTime;
}
Called by the scheduler at the time a Trigger is first
added to the scheduler, in order to have the Trigger
compute its first fire time, based on any associated calendar.
After this method has been called, getNextFireTime()
should return a valid answer.
|
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;
}
Called after the Scheduler has executed the
org.quartz.JobDetail associated with the Trigger
in order to get the final instruction code from the trigger.
|
public String getCronExpression() {
return cronEx == null ? null : cronEx.getCronExpression();
}
|
public Date getEndTime() {
return this.endTime;
}
Get the time at which the CronTrigger should quit
repeating - even if repeastCount isn't yet satisfied.
|
public String getExpressionSummary() {
return cronEx == null ? null : cronEx.getExpressionSummary();
}
|
public Date getFinalFireTime() {
Date resultTime;
if (getEndTime() != null) {
resultTime = getTimeBefore(new Date(getEndTime().getTime() + 1000l));
} else {
resultTime = (cronEx == null) ? null : cronEx.getFinalFireTime();
}
if ((resultTime != null) && (getStartTime() != null) && (resultTime.before(getStartTime()))) {
return null;
}
return resultTime;
}
NOT YET IMPLEMENTED: Returns the final time at which the
CronTrigger will fire.
Note that the return time *may* be in the past. and the date returned is
not validated against org.quartz.calendar
|
public Date getFireTimeAfter(Date afterTime) {
if (afterTime == null) {
afterTime = new Date();
}
if (getStartTime().after(afterTime)) {
afterTime = new Date(getStartTime().getTime() - 1000l);
}
if (getEndTime() != null && (afterTime.compareTo(getEndTime()) >= 0)) {
return null;
}
Date pot = getTimeAfter(afterTime);
if (getEndTime() != null && pot != null && pot.after(getEndTime())) {
return null;
}
return pot;
}
Returns the next time at which the CronTrigger will fire,
after the given time. If the trigger will not fire after the given time,
null will be returned.
Note that the date returned is NOT validated against the related
org.quartz.Calendar (if any)
|
public Date getNextFireTime() {
return this.nextFireTime;
}
Returns the next time at which the CronTrigger will fire.
If the trigger will not fire again, null will be
returned. The value returned is not guaranteed to be valid until after
the Trigger has been added to the scheduler.
|
public Date getPreviousFireTime() {
return this.previousFireTime;
}
Returns the previous time at which the CronTrigger will
fire. If the trigger has not yet fired, null will be
returned.
|
public Date getStartTime() {
return this.startTime;
}
|
protected Date getTimeAfter(Date afterTime) {
return (cronEx == null) ? null : cronEx.getTimeAfter(afterTime);
}
|
protected Date getTimeBefore(Date endTime) {
return (cronEx == null) ? null : cronEx.getTimeBefore(endTime);
}
NOT YET IMPLEMENTED: Returns the time before the given time
that this CronTrigger will fire. |
public TimeZone getTimeZone() {
if(cronEx != null) {
return cronEx.getTimeZone();
}
if (timeZone == null) {
timeZone = TimeZone.getDefault();
}
return timeZone;
}
|
public static void main(String[] args) 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));
//// }
//// }
}
|
public boolean mayFireAgain() {
return (getNextFireTime() != null);
}
|
public void setCronExpression(String cronExpression) throws ParseException {
this.cronEx = new CronExpression(cronExpression);
this.cronEx.setTimeZone(getTimeZone());
}
|
public void setCronExpression(CronExpression cronExpression) {
this.cronEx = cronExpression;
this.timeZone = cronExpression.getTimeZone();
}
|
public void setEndTime(Date endTime) {
Date sTime = getStartTime();
if (sTime != null && endTime != null && sTime.after(endTime)) {
throw new IllegalArgumentException(
"End time cannot be before start time");
}
this.endTime = endTime;
}
|
public void setNextFireTime(Date nextFireTime) {
this.nextFireTime = nextFireTime;
}
Sets the next time at which the CronTrigger will fire.
This method should not be invoked by client code.
|
public void setPreviousFireTime(Date previousFireTime) {
this.previousFireTime = previousFireTime;
}
Set the previous time at which the CronTrigger fired.
This method should not be invoked by client code.
|
public void setStartTime(Date startTime) {
if (startTime == null) {
throw new IllegalArgumentException("Start time cannot be null");
}
Date eTime = getEndTime();
if (eTime != null && startTime != null && eTime.before(startTime)) {
throw new IllegalArgumentException(
"End time cannot be before start time");
}
// round off millisecond...
// Note timeZone is not needed here as parameter for
// Calendar.getInstance(),
// since time zone is implicit when using a Date in the setTime method.
Calendar cl = Calendar.getInstance();
cl.setTime(startTime);
cl.set(Calendar.MILLISECOND, 0);
this.startTime = cl.getTime();
}
|
public void setTimeZone(TimeZone timeZone) {
if(cronEx != null) {
cronEx.setTimeZone(timeZone);
}
this.timeZone = timeZone;
}
|
public void triggered(Calendar calendar) {
previousFireTime = nextFireTime;
nextFireTime = getFireTimeAfter(nextFireTime);
while (nextFireTime != null && calendar != null
&& !calendar.isTimeIncluded(nextFireTime.getTime())) {
nextFireTime = getFireTimeAfter(nextFireTime);
}
}
Called when the Scheduler has decided to 'fire'
the trigger (execute the associated Job ), in order to
give the Trigger a chance to update itself for its next
triggering (if any).
|
public void updateAfterMisfire(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());
}
}
Updates the CronTrigger 's state based on the
MISFIRE_INSTRUCTION_XXX that was selected when the CronTrigger
was created.
If the misfire instruction is set to MISFIRE_INSTRUCTION_SMART_POLICY,
then the following scheme will be used:
- The instruction will be interpreted as
MISFIRE_INSTRUCTION_FIRE_ONCE_NOW
|
public void updateWithNewCalendar(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);
}
|
protected boolean validateMisfireInstruction(int misfireInstruction) {
if (misfireInstruction < MISFIRE_INSTRUCTION_SMART_POLICY) {
return false;
}
if (misfireInstruction > MISFIRE_INSTRUCTION_DO_NOTHING) {
return false;
}
return true;
}
|
public boolean willFireOn(Calendar test) {
return willFireOn(test, false);
}
Determines whether the date and (optionally) time of the given Calendar
instance falls on a scheduled fire-time of this trigger.
Equivalent to calling willFireOn(cal, false) .
|
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;
}
Determines whether the date and (optionally) time of the given Calendar
instance falls on a scheduled fire-time of this trigger.
Note that the value returned is NOT validated against the related
org.quartz.Calendar (if any)
|