Search in sources :

Example 11 with Calendar

use of org.quartz.Calendar in project weicoder by wdcode.

the class ExecutingJobsManager method scheduleJob.

/**
 * <p>
 * Schedule the given <code>{@link org.quartz.Trigger}</code> with the
 * <code>Job</code> identified by the <code>Trigger</code>'s settings.
 * </p>
 *
 * @throws SchedulerException
 *           if the indicated Job does not exist, or the Trigger cannot be
 *           added to the Scheduler, or there is an internal Scheduler
 *           error.
 */
public Date scheduleJob(Trigger trigger) throws SchedulerException {
    validateState();
    if (trigger == null) {
        throw new SchedulerException("Trigger cannot be null");
    }
    OperableTrigger trig = (OperableTrigger) trigger;
    trig.validate();
    Calendar cal = null;
    if (trigger.getCalendarName() != null) {
        cal = resources.getJobStore().retrieveCalendar(trigger.getCalendarName());
        if (cal == null) {
            throw new SchedulerException("Calendar not found: " + trigger.getCalendarName());
        }
    }
    Date ft = trig.computeFirstFireTime(cal);
    if (ft == null) {
        throw new SchedulerException("Based on configured schedule, the given trigger '" + trigger.getKey() + "' will never fire.");
    }
    resources.getJobStore().storeTrigger(trig, false);
    notifySchedulerThread(trigger.getNextFireTime().getTime());
    notifySchedulerListenersSchduled(trigger);
    return ft;
}
Also used : OperableTrigger(org.quartz.spi.OperableTrigger) SchedulerException(org.quartz.SchedulerException) Calendar(org.quartz.Calendar) Date(java.util.Date)

Example 12 with Calendar

use of org.quartz.Calendar in project weicoder by wdcode.

the class JobStoreSupport method doUpdateOfMisfiredTrigger.

private void doUpdateOfMisfiredTrigger(Connection conn, OperableTrigger trig, boolean forceState, String newStateIfNotComplete, boolean recovering) throws JobPersistenceException {
    Calendar cal = null;
    if (trig.getCalendarName() != null) {
        cal = retrieveCalendar(conn, trig.getCalendarName());
    }
    schedSignaler.notifyTriggerListenersMisfired(trig);
    trig.updateAfterMisfire(cal);
    if (trig.getNextFireTime() == null) {
        storeTrigger(conn, trig, null, true, STATE_COMPLETE, forceState, recovering);
        schedSignaler.notifySchedulerListenersFinalized(trig);
    } else {
        storeTrigger(conn, trig, null, true, newStateIfNotComplete, forceState, recovering);
    }
}
Also used : Calendar(org.quartz.Calendar)

Example 13 with Calendar

use of org.quartz.Calendar in project weicoder by wdcode.

the class StdJDBCDelegate method selectCalendar.

/**
 * <p>
 * Select a calendar.
 * </p>
 *
 * @param conn
 *          the DB Connection
 * @param calendarName
 *          the name of the calendar
 * @return the Calendar
 * @throws ClassNotFoundException
 *           if a class found during deserialization cannot be found be
 *           found
 * @throws IOException
 *           if there were problems deserializing the calendar
 */
public Calendar selectCalendar(Connection conn, String calendarName) throws ClassNotFoundException, IOException, SQLException {
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        String selCal = rtp(SELECT_CALENDAR);
        ps = conn.prepareStatement(selCal);
        ps.setString(1, calendarName);
        rs = ps.executeQuery();
        Calendar cal = null;
        if (rs.next()) {
            cal = (Calendar) getObjectFromBlob(rs, COL_CALENDAR);
        }
        if (null == cal) {
            logger.warn("Couldn't find calendar with name '" + calendarName + "'.");
        }
        return cal;
    } finally {
        closeResultSet(rs);
        closeStatement(ps);
    }
}
Also used : Calendar(org.quartz.Calendar) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 14 with Calendar

use of org.quartz.Calendar in project weicoder by wdcode.

the class TriggerWrapper method applyMisfire.

protected boolean applyMisfire(TriggerWrapper tw) {
    long misfireTime = System.currentTimeMillis();
    if (getMisfireThreshold() > 0) {
        misfireTime -= getMisfireThreshold();
    }
    Date tnft = tw.trigger.getNextFireTime();
    if (tnft == null || tnft.getTime() > misfireTime || tw.trigger.getMisfireInstruction() == Trigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY) {
        return false;
    }
    Calendar cal = null;
    if (tw.trigger.getCalendarName() != null) {
        cal = retrieveCalendar(tw.trigger.getCalendarName());
    }
    signaler.notifyTriggerListenersMisfired((OperableTrigger) tw.trigger.clone());
    tw.trigger.updateAfterMisfire(cal);
    if (tw.trigger.getNextFireTime() == null) {
        tw.state = TriggerWrapper.STATE_COMPLETE;
        signaler.notifySchedulerListenersFinalized(tw.trigger);
        synchronized (lock) {
            timeTriggers.remove(tw);
        }
    } else if (tnft.equals(tw.trigger.getNextFireTime())) {
        return false;
    }
    return true;
}
Also used : Calendar(org.quartz.Calendar) Date(java.util.Date)

Example 15 with Calendar

use of org.quartz.Calendar in project weicoder by wdcode.

the class SimpleTriggerImpl method updateAfterMisfire.

/**
 * <p>
 * Updates the <code>SimpleTrigger</code>'s state based on the
 * MISFIRE_INSTRUCTION_XXX that was selected when the <code>SimpleTrigger</code>
 * was created.
 * </p>
 *
 * <p>
 * If the misfire instruction is set to MISFIRE_INSTRUCTION_SMART_POLICY,
 * then the following scheme will be used: <br>
 * <ul>
 * <li>If the Repeat Count is <code>0</code>, then the instruction will
 * be interpreted as <code>MISFIRE_INSTRUCTION_FIRE_NOW</code>.</li>
 * <li>If the Repeat Count is <code>REPEAT_INDEFINITELY</code>, then
 * the instruction will be interpreted as <code>MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT</code>.
 * <b>WARNING:</b> 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.
 * </li>
 * <li>If the Repeat Count is <code>&gt; 0</code>, then the instruction
 * will be interpreted as <code>MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT</code>.
 * </li>
 * </ul>
 * </p>
 */
@Override
public void updateAfterMisfire(Calendar cal) {
    int instr = getMisfireInstruction();
    if (instr == Trigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY)
        return;
    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);
            if (newFireTime == null)
                break;
            // avoid infinite loop
            java.util.Calendar c = java.util.Calendar.getInstance();
            c.setTime(newFireTime);
            if (c.get(java.util.Calendar.YEAR) > YEAR_TO_GIVEUP_SCHEDULING_AT) {
                newFireTime = null;
            }
        }
        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)
                break;
            // avoid infinite loop
            java.util.Calendar c = java.util.Calendar.getInstance();
            c.setTime(newFireTime);
            if (c.get(java.util.Calendar.YEAR) > YEAR_TO_GIVEUP_SCHEDULING_AT) {
                newFireTime = null;
            }
        }
        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)) {
            // We are past the end time
            setNextFireTime(null);
        } 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)) {
            // We are past the end time
            setNextFireTime(null);
        } else {
            setStartTime(newFireTime);
            setNextFireTime(newFireTime);
        }
    }
}
Also used : Calendar(org.quartz.Calendar) Date(java.util.Date)

Aggregations

Calendar (org.quartz.Calendar)15 Date (java.util.Date)10 JobDetail (org.quartz.JobDetail)5 SchedulerException (org.quartz.SchedulerException)5 OperableTrigger (org.quartz.spi.OperableTrigger)5 Trigger (org.quartz.Trigger)4 Scheduler (org.quartz.Scheduler)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 Test (org.junit.Test)2 ComplexJobTrigger (org.pentaho.platform.api.scheduler2.ComplexJobTrigger)2 SimpleJobTrigger (org.pentaho.platform.api.scheduler2.SimpleJobTrigger)2 CronTrigger (org.quartz.CronTrigger)2 SimpleTrigger (org.quartz.SimpleTrigger)2 HolidayCalendar (org.quartz.impl.calendar.HolidayCalendar)2 TriggerFiredBundle (org.quartz.spi.TriggerFiredBundle)2 Serializable (java.io.Serializable)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1