public static final int | MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT |
Instructs the Scheduler that upon a mis-fire
situation, the SimpleTrigger wants to be
re-scheduled to 'now' (even if the associated Calendar
excludes 'now') with the repeat count left as-is. This does obey the
Trigger end-time however, so if 'now' is after the
end-time the Trigger will not fire again.
NOTE: Use of this instruction causes the trigger to 'forget'
the start-time and repeat-count that it was originally setup with (this
is only an issue if you for some reason wanted to be able to tell what
the original values were at some later time).
NOTE: This instruction could cause the Trigger
to go to the 'COMPLETE' state after firing 'now', if all the
repeat-fire-times where missed.
|
public static final int | MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT |
Instructs the Scheduler that upon a mis-fire
situation, the SimpleTrigger wants to be
re-scheduled to 'now' (even if the associated Calendar
excludes 'now') with the repeat count set to what it would be, if it had
not missed any firings. This does obey the Trigger end-time
however, so if 'now' is after the end-time the Trigger will
not fire again.
NOTE: Use of this instruction causes the trigger to 'forget'
the start-time and repeat-count that it was originally setup with (this
is only an issue if you for some reason wanted to be able to tell what
the original values were at some later time).
NOTE: This instruction could cause the Trigger
to go to the 'COMPLETE' state after firing 'now', if all the
repeat-fire-times where missed.
|
public static final int | MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT |
Instructs the Scheduler that upon a mis-fire
situation, the SimpleTrigger wants to be
re-scheduled to the next scheduled time after 'now' - taking into
account any associated Calendar , and with the
repeat count set to what it would be, if it had not missed any firings.
NOTE/WARNING: This instruction could cause the Trigger
to go directly to the 'COMPLETE' state if all fire-times where missed.
|
public static final int | MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT |
Instructs the Scheduler that upon a mis-fire
situation, the SimpleTrigger wants to be
re-scheduled to the next scheduled time after 'now' - taking into
account any associated Calendar , and with the
repeat count left unchanged.
NOTE: Use of this instruction causes the trigger to 'forget'
the repeat-count that it was originally setup with (this is only an
issue if you for some reason wanted to be able to tell what the original
values were at some later time).
NOTE/WARNING: This instruction could cause the Trigger
to go directly to the 'COMPLETE' state if all fire-times where missed.
|
Constructor: |
public SimpleTrigger() {
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Constructors.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
super();
}
|
public SimpleTrigger(String name,
String group) {
this(name, group, new Date(), null, 0, 0);
}
Create a SimpleTrigger that will occur immediately, and
not repeat.
|
public SimpleTrigger(String name,
String group,
Date startTime) {
this(name, group, startTime, null, 0, 0);
}
Create a SimpleTrigger that will occur at the given time,
and not repeat.
|
public SimpleTrigger(String name,
String group,
int repeatCount,
long repeatInterval) {
this(name, group, new Date(), null, repeatCount, repeatInterval);
}
Create a SimpleTrigger that will occur immediately, and
repeat at the the given interval the given number of times.
|
public SimpleTrigger(String name,
String group,
Date startTime,
Date endTime,
int repeatCount,
long repeatInterval) {
super(name, group);
setStartTime(startTime);
setEndTime(endTime);
setRepeatCount(repeatCount);
setRepeatInterval(repeatInterval);
}
Create a SimpleTrigger that will occur at the given time,
and repeat at the the given interval the given number of times, or until
the given end time.
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.
repeatCount -
The number of times for the Trigger to repeat
firing, use #REPEAT_INDEFINITELY for unlimitted times.
repeatInterval -
The number of milliseconds to pause between the repeat firing.
|
public SimpleTrigger(String name,
String group,
String jobName,
String jobGroup,
Date startTime,
Date endTime,
int repeatCount,
long repeatInterval) {
super(name, group, jobName, jobGroup);
setStartTime(startTime);
setEndTime(endTime);
setRepeatCount(repeatCount);
setRepeatInterval(repeatInterval);
}
Create a SimpleTrigger that will occur at the given time,
fire the identified Job and repeat at the the given
interval the given number of times, or until the given end time.
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.
repeatCount -
The number of times for the Trigger to repeat
firing, use #REPEAT_INDEFINITELY for unlimitted times.
repeatInterval -
The number of milliseconds to pause between the repeat firing.
|
Method from org.quartz.SimpleTrigger Detail: |
public Date computeFirstFireTime(Calendar calendar) {
nextFireTime = getStartTime();
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 computeNumTimesFiredBetween(Date start,
Date end) {
if(repeatInterval < 1) {
return 0;
}
long time = end.getTime() - start.getTime();
return (int) (time / repeatInterval);
}
|
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 Date getEndTime() {
return endTime;
}
Get the time at which the SimpleTrigger should quit
repeating - even if repeastCount isn't yet satisfied.
|
public Date getFinalFireTime() {
if (repeatCount == 0) {
return startTime;
}
if (repeatCount == REPEAT_INDEFINITELY) {
return (getEndTime() == null) ? null : getFireTimeBefore(getEndTime());
}
long lastTrigger = startTime.getTime() + (repeatCount * repeatInterval);
if ((getEndTime() == null) || (lastTrigger < getEndTime().getTime())) {
return new Date(lastTrigger);
} else {
return getFireTimeBefore(getEndTime());
}
}
Returns the final time at which the SimpleTrigger will
fire, if repeatCount is REPEAT_INDEFINITELY, null will be returned.
Note that the return time may be in the past.
|
public Date getFireTimeAfter(Date afterTime) {
if (complete) {
return null;
}
if ((timesTriggered > repeatCount)
&& (repeatCount != REPEAT_INDEFINITELY)) {
return null;
}
if (afterTime == null) {
afterTime = new Date();
}
if (repeatCount == 0 && afterTime.compareTo(getStartTime()) >= 0) {
return null;
}
long startMillis = getStartTime().getTime();
long afterMillis = afterTime.getTime();
long endMillis = (getEndTime() == null) ? Long.MAX_VALUE : getEndTime()
.getTime();
if (endMillis < = afterMillis) {
return null;
}
if (afterMillis < startMillis) {
return new Date(startMillis);
}
long numberOfTimesExecuted = ((afterMillis - startMillis) / repeatInterval) + 1;
if ((numberOfTimesExecuted > repeatCount) &&
(repeatCount != REPEAT_INDEFINITELY)) {
return null;
}
Date time = new Date(startMillis + (numberOfTimesExecuted * repeatInterval));
if (endMillis < = time.getTime()) {
return null;
}
return time;
}
Returns the next time at which the SimpleTrigger will
fire, after the given time. If the trigger will not fire after the given
time, null will be returned.
|
public Date getFireTimeBefore(Date end) {
if (end.getTime() < getStartTime().getTime()) {
return null;
}
int numFires = computeNumTimesFiredBetween(getStartTime(), end);
return new Date(getStartTime().getTime() + (numFires * repeatInterval));
}
Returns the last time at which the SimpleTrigger will
fire, before the given time. If the trigger will not fire before the
given time, null will be returned.
|
public Date getNextFireTime() {
return nextFireTime;
}
Returns the next time at which the SimpleTrigger 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 previousFireTime;
}
Returns the previous time at which the SimpleTrigger will
fire. If the trigger has not yet fired, null will be
returned.
|
public int getRepeatCount() {
return repeatCount;
}
Get the the number of times the SimpleTrigger should
repeat, after which it will be automatically deleted.
|
public long getRepeatInterval() {
return repeatInterval;
}
|
public Date getStartTime() {
return startTime;
}
|
public int getTimesTriggered() {
return timesTriggered;
}
|
public static void main(String[] args) throws Exception {
Date sdt = new Date();
Date edt = new Date(sdt.getTime() + 55000L);
SimpleTrigger st = new SimpleTrigger("t", "g", "j", "g", sdt, edt, 10,
10000L);
System.err.println();
st.computeFirstFireTime(null);
System.err.println("lastTime=" + st.getFinalFireTime());
java.util.List times = TriggerUtils.computeFireTimes(st, null, 50);
for (int i = 0; i < times.size(); i++) {
System.err.println("firetime = " + times.get(i));
}
}
|
public boolean mayFireAgain() {
return (getNextFireTime() != null);
}
|
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;
}
Set the next time at which the SimpleTrigger should 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 SimpleTrigger fired.
This method should not be invoked by client code.
|
public void setRepeatCount(int repeatCount) {
if (repeatCount < 0 && repeatCount != REPEAT_INDEFINITELY) {
throw new IllegalArgumentException(
"Repeat count must be >= 0, use the "
+ "constant REPEAT_INDEFINITELY for infinite.");
}
this.repeatCount = repeatCount;
}
Set the the number of time the SimpleTrigger should
repeat, after which it will be automatically deleted.
|
public void setRepeatInterval(long repeatInterval) {
if (repeatInterval < 0) {
throw new IllegalArgumentException(
"Repeat interval must be >= 0");
}
this.repeatInterval = repeatInterval;
}
|
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");
}
this.startTime = startTime;
}
|
public void setTimesTriggered(int timesTriggered) {
this.timesTriggered = timesTriggered;
}
|
public void triggered(Calendar calendar) {
timesTriggered++;
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 == Trigger.MISFIRE_INSTRUCTION_SMART_POLICY) {
if (getRepeatCount() == 0) {
instr = MISFIRE_INSTRUCTION_FIRE_NOW;
} else if (getRepeatCount() == REPEAT_INDEFINITELY) {
instr = MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT;
} else {
// if (getRepeatCount() > 0)
instr = MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT;
}
} else if (instr == MISFIRE_INSTRUCTION_FIRE_NOW && getRepeatCount() != 0) {
instr = MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT;
}
if (instr == MISFIRE_INSTRUCTION_FIRE_NOW) {
setNextFireTime(new Date());
} else if (instr == MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT) {
Date newFireTime = getFireTimeAfter(new Date());
while (newFireTime != null && cal != null
&& !cal.isTimeIncluded(newFireTime.getTime())) {
newFireTime = getFireTimeAfter(newFireTime);
}
setNextFireTime(newFireTime);
} else if (instr == MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT) {
Date newFireTime = getFireTimeAfter(new Date());
while (newFireTime != null && cal != null
&& !cal.isTimeIncluded(newFireTime.getTime())) {
newFireTime = getFireTimeAfter(newFireTime);
}
if (newFireTime != null) {
int timesMissed = computeNumTimesFiredBetween(nextFireTime,
newFireTime);
setTimesTriggered(getTimesTriggered() + timesMissed);
}
setNextFireTime(newFireTime);
} else if (instr == MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT) {
Date newFireTime = new Date();
if (repeatCount != 0 && repeatCount != REPEAT_INDEFINITELY) {
setRepeatCount(getRepeatCount() - getTimesTriggered());
setTimesTriggered(0);
}
if (getEndTime() != null && getEndTime().before(newFireTime)) {
setNextFireTime(null); // We are past the end time
} else {
setStartTime(newFireTime);
setNextFireTime(newFireTime);
}
} else if (instr == MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT) {
Date newFireTime = new Date();
int timesMissed = computeNumTimesFiredBetween(nextFireTime,
newFireTime);
if (repeatCount != 0 && repeatCount != REPEAT_INDEFINITELY) {
int remainingCount = getRepeatCount()
- (getTimesTriggered() + timesMissed);
if (remainingCount < = 0) {
remainingCount = 0;
}
setRepeatCount(remainingCount);
setTimesTriggered(0);
}
if (getEndTime() != null && getEndTime().before(newFireTime)) {
setNextFireTime(null); // We are past the end time
} else {
setStartTime(newFireTime);
setNextFireTime(newFireTime);
}
}
}
Updates the SimpleTrigger 's state based on the
MISFIRE_INSTRUCTION_XXX that was selected when the SimpleTrigger
was created.
If the misfire instruction is set to MISFIRE_INSTRUCTION_SMART_POLICY,
then the following scheme will be used:
- If the Repeat Count is
0 , then the instruction will
be interpreted as MISFIRE_INSTRUCTION_FIRE_NOW .
- If the Repeat Count is
REPEAT_INDEFINITELY , then
the instruction will be interpreted as MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT .
WARNING: using MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT
with a trigger that has a non-null end-time may cause the trigger to
never fire again if the end-time arrived during the misfire time span.
- If the Repeat Count is
> 0 , then the instruction
will be interpreted as MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT .
|
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);
}
|
public void validate() throws SchedulerException {
super.validate();
if (repeatCount != 0 && repeatInterval < 1) {
throw new SchedulerException("Repeat Interval cannot be zero.",
SchedulerException.ERR_CLIENT_ERROR);
}
}
|
protected boolean validateMisfireInstruction(int misfireInstruction) {
if (misfireInstruction < MISFIRE_INSTRUCTION_SMART_POLICY) {
return false;
}
if (misfireInstruction > MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT) {
return false;
}
return true;
}
|