Search in sources :

Example 1 with JobPersistenceException

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

the class StdJDBCDelegate method selectTrigger.

/**
 * <p>
 * Select a trigger.
 * </p>
 *
 * @param conn
 *          the DB Connection
 * @return the <code>{@link org.quartz.Trigger}</code> object
 * @throws JobPersistenceException
 */
public OperableTrigger selectTrigger(Connection conn, TriggerKey triggerKey) throws SQLException, ClassNotFoundException, IOException, JobPersistenceException {
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        OperableTrigger trigger = null;
        ps = conn.prepareStatement(rtp(SELECT_TRIGGER));
        ps.setString(1, triggerKey.getName());
        ps.setString(2, triggerKey.getGroup());
        rs = ps.executeQuery();
        if (rs.next()) {
            String jobName = rs.getString(COL_JOB_NAME);
            String jobGroup = rs.getString(COL_JOB_GROUP);
            String description = rs.getString(COL_DESCRIPTION);
            long nextFireTime = rs.getLong(COL_NEXT_FIRE_TIME);
            long prevFireTime = rs.getLong(COL_PREV_FIRE_TIME);
            String triggerType = rs.getString(COL_TRIGGER_TYPE);
            long startTime = rs.getLong(COL_START_TIME);
            long endTime = rs.getLong(COL_END_TIME);
            String calendarName = rs.getString(COL_CALENDAR_NAME);
            int misFireInstr = rs.getInt(COL_MISFIRE_INSTRUCTION);
            int priority = rs.getInt(COL_PRIORITY);
            Map<?, ?> map = null;
            if (canUseProperties()) {
                map = getMapFromProperties(rs);
            } else {
                map = (Map<?, ?>) getObjectFromBlob(rs, COL_JOB_DATAMAP);
            }
            Date nft = null;
            if (nextFireTime > 0) {
                nft = new Date(nextFireTime);
            }
            Date pft = null;
            if (prevFireTime > 0) {
                pft = new Date(prevFireTime);
            }
            Date startTimeD = new Date(startTime);
            Date endTimeD = null;
            if (endTime > 0) {
                endTimeD = new Date(endTime);
            }
            if (triggerType.equals(TTYPE_BLOB)) {
                rs.close();
                rs = null;
                ps.close();
                ps = null;
                ps = conn.prepareStatement(rtp(SELECT_BLOB_TRIGGER));
                ps.setString(1, triggerKey.getName());
                ps.setString(2, triggerKey.getGroup());
                rs = ps.executeQuery();
                if (rs.next()) {
                    trigger = (OperableTrigger) getObjectFromBlob(rs, COL_BLOB);
                }
            } else {
                TriggerPersistenceDelegate tDel = findTriggerPersistenceDelegate(triggerType);
                if (tDel == null)
                    throw new JobPersistenceException("No TriggerPersistenceDelegate for trigger discriminator type: " + triggerType);
                TriggerPropertyBundle triggerProps = null;
                try {
                    triggerProps = tDel.loadExtendedTriggerProperties(conn, triggerKey);
                } catch (IllegalStateException isex) {
                    if (isTriggerStillPresent(ps)) {
                        throw isex;
                    } else {
                        // QTZ-386 Trigger has been deleted
                        return null;
                    }
                }
                TriggerBuilder<?> tb = newTrigger().withDescription(description).withPriority(priority).startAt(startTimeD).endAt(endTimeD).withIdentity(triggerKey).modifiedByCalendar(calendarName).withSchedule(triggerProps.getScheduleBuilder()).forJob(jobKey(jobName, jobGroup));
                if (null != map) {
                    tb.usingJobData(new JobDataMap(map));
                }
                trigger = (OperableTrigger) tb.build();
                trigger.setMisfireInstruction(misFireInstr);
                trigger.setNextFireTime(nft);
                trigger.setPreviousFireTime(pft);
                setTriggerStateProperties(trigger, triggerProps);
            }
        }
        return trigger;
    } finally {
        closeResultSet(rs);
        closeStatement(ps);
    }
}
Also used : OperableTrigger(org.quartz.spi.OperableTrigger) JobDataMap(org.quartz.JobDataMap) JobPersistenceException(org.quartz.JobPersistenceException) PreparedStatement(java.sql.PreparedStatement) Date(java.util.Date) TriggerPropertyBundle(org.quartz.impl.jdbcjobstore.TriggerPersistenceDelegate.TriggerPropertyBundle) ResultSet(java.sql.ResultSet)

Example 2 with JobPersistenceException

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

the class JobStoreSupport method replaceTrigger.

protected boolean replaceTrigger(Connection conn, TriggerKey key, OperableTrigger newTrigger) throws JobPersistenceException {
    try {
        // this must be called before we delete the trigger, obviously
        JobDetail job = getDelegate().selectJobForTrigger(conn, getClassLoadHelper(), key);
        if (job == null) {
            return false;
        }
        if (!newTrigger.getJobKey().equals(job.getKey())) {
            throw new JobPersistenceException("New trigger is not related to the same job as the old trigger.");
        }
        boolean removedTrigger = deleteTriggerAndChildren(conn, key);
        storeTrigger(conn, newTrigger, job, false, STATE_WAITING, false, false);
        return removedTrigger;
    } catch (ClassNotFoundException e) {
        throw new JobPersistenceException("Couldn't remove trigger: " + e.getMessage(), e);
    } catch (SQLException e) {
        throw new JobPersistenceException("Couldn't remove trigger: " + e.getMessage(), e);
    }
}
Also used : JobDetail(org.quartz.JobDetail) JobPersistenceException(org.quartz.JobPersistenceException) SQLException(java.sql.SQLException)

Example 3 with JobPersistenceException

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

the class JobStoreSupport method findFailedInstances.

/**
 * Get a list of all scheduler instances in the cluster that may have failed. This includes this scheduler if it is
 * checking in for the first time.
 */
protected List<SchedulerStateRecord> findFailedInstances(Connection conn) throws JobPersistenceException {
    try {
        List<SchedulerStateRecord> failedInstances = new LinkedList<SchedulerStateRecord>();
        boolean foundThisScheduler = false;
        long timeNow = System.currentTimeMillis();
        List<SchedulerStateRecord> states = getDelegate().selectSchedulerStateRecords(conn, null);
        for (SchedulerStateRecord rec : states) {
            // find own record...
            if (rec.getSchedulerInstanceId().equals(getInstanceId())) {
                foundThisScheduler = true;
                if (firstCheckIn) {
                    failedInstances.add(rec);
                }
            } else {
                // find failed instances...
                if (calcFailedIfAfter(rec) < timeNow) {
                    failedInstances.add(rec);
                }
            }
        }
        // The first time through, also check for orphaned fired triggers.
        if (firstCheckIn) {
            failedInstances.addAll(findOrphanedFailedInstances(conn, states));
        }
        // Someone must have done recovery for us.
        if ((!foundThisScheduler) && (!firstCheckIn)) {
            // FUTURE_TODO: revisit when handle self-failed-out impl'ed (see FUTURE_TODO in clusterCheckIn() below)
            getLog().warn("This scheduler instance (" + getInstanceId() + ") is still " + "active but was recovered by another instance in the cluster.  " + "This may cause inconsistent behavior.");
        }
        return failedInstances;
    } catch (Exception e) {
        lastCheckin = System.currentTimeMillis();
        throw new JobPersistenceException("Failure identifying failed instances when checking-in: " + e.getMessage(), e);
    }
}
Also used : JobPersistenceException(org.quartz.JobPersistenceException) LinkedList(java.util.LinkedList) JobPersistenceException(org.quartz.JobPersistenceException) SQLException(java.sql.SQLException) SchedulerException(org.quartz.SchedulerException) SchedulerConfigException(org.quartz.SchedulerConfigException) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException) IOException(java.io.IOException)

Example 4 with JobPersistenceException

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

the class JobStoreSupport method triggeredJobComplete.

protected void triggeredJobComplete(Connection conn, OperableTrigger trigger, JobDetail jobDetail, CompletedExecutionInstruction triggerInstCode) throws JobPersistenceException {
    try {
        if (triggerInstCode == CompletedExecutionInstruction.DELETE_TRIGGER) {
            if (trigger.getNextFireTime() == null) {
                // double check for possible reschedule within job
                // execution, which would cancel the need to delete...
                TriggerStatus stat = getDelegate().selectTriggerStatus(conn, trigger.getKey());
                if (stat != null && stat.getNextFireTime() == null) {
                    removeTrigger(conn, trigger.getKey());
                }
            } else {
                removeTrigger(conn, trigger.getKey());
                signalSchedulingChangeOnTxCompletion(0L);
            }
        } else if (triggerInstCode == CompletedExecutionInstruction.SET_TRIGGER_COMPLETE) {
            getDelegate().updateTriggerState(conn, trigger.getKey(), STATE_COMPLETE);
            signalSchedulingChangeOnTxCompletion(0L);
        } else if (triggerInstCode == CompletedExecutionInstruction.SET_TRIGGER_ERROR) {
            getLog().info("Trigger " + trigger.getKey() + " set to ERROR state.");
            getDelegate().updateTriggerState(conn, trigger.getKey(), STATE_ERROR);
            signalSchedulingChangeOnTxCompletion(0L);
        } else if (triggerInstCode == CompletedExecutionInstruction.SET_ALL_JOB_TRIGGERS_COMPLETE) {
            getDelegate().updateTriggerStatesForJob(conn, trigger.getJobKey(), STATE_COMPLETE);
            signalSchedulingChangeOnTxCompletion(0L);
        } else if (triggerInstCode == CompletedExecutionInstruction.SET_ALL_JOB_TRIGGERS_ERROR) {
            getLog().info("All triggers of Job " + trigger.getKey() + " set to ERROR state.");
            getDelegate().updateTriggerStatesForJob(conn, trigger.getJobKey(), STATE_ERROR);
            signalSchedulingChangeOnTxCompletion(0L);
        }
        if (jobDetail.isConcurrentExectionDisallowed()) {
            getDelegate().updateTriggerStatesForJobFromOtherState(conn, jobDetail.getKey(), STATE_WAITING, STATE_BLOCKED);
            getDelegate().updateTriggerStatesForJobFromOtherState(conn, jobDetail.getKey(), STATE_PAUSED, STATE_PAUSED_BLOCKED);
            signalSchedulingChangeOnTxCompletion(0L);
        }
        if (jobDetail.isPersistJobDataAfterExecution()) {
            try {
                if (jobDetail.getJobDataMap().isDirty()) {
                    getDelegate().updateJobData(conn, jobDetail);
                }
            } catch (IOException e) {
                throw new JobPersistenceException("Couldn't serialize job data: " + e.getMessage(), e);
            } catch (SQLException e) {
                throw new JobPersistenceException("Couldn't update job data: " + e.getMessage(), e);
            }
        }
    } catch (SQLException e) {
        throw new JobPersistenceException("Couldn't update trigger state(s): " + e.getMessage(), e);
    }
    try {
        getDelegate().deleteFiredTrigger(conn, trigger.getFireInstanceId());
    } catch (SQLException e) {
        throw new JobPersistenceException("Couldn't delete fired trigger: " + e.getMessage(), e);
    }
}
Also used : JobPersistenceException(org.quartz.JobPersistenceException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 5 with JobPersistenceException

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

the class JobStoreSupport method doCheckin.

protected boolean doCheckin() throws JobPersistenceException {
    boolean transOwner = false;
    boolean transStateOwner = false;
    boolean recovered = false;
    Connection conn = getNonManagedTXConnection();
    try {
        // Other than the first time, always checkin first to make sure there is
        // work to be done before we acquire the lock (since that is expensive,
        // and is almost never necessary). This must be done in a separate
        // transaction to prevent a deadlock under recovery conditions.
        List<SchedulerStateRecord> failedRecords = null;
        if (!firstCheckIn) {
            failedRecords = clusterCheckIn(conn);
            commitConnection(conn);
        }
        if (firstCheckIn || (failedRecords.size() > 0)) {
            getLockHandler().obtainLock(conn, LOCK_STATE_ACCESS);
            transStateOwner = true;
            // Now that we own the lock, make sure we still have work to do.
            // The first time through, we also need to make sure we update/create our state record
            failedRecords = (firstCheckIn) ? clusterCheckIn(conn) : findFailedInstances(conn);
            if (failedRecords.size() > 0) {
                getLockHandler().obtainLock(conn, LOCK_TRIGGER_ACCESS);
                // getLockHandler().obtainLock(conn, LOCK_JOB_ACCESS);
                transOwner = true;
                clusterRecover(conn, failedRecords);
                recovered = true;
            }
        }
        commitConnection(conn);
    } catch (JobPersistenceException e) {
        rollbackConnection(conn);
        throw e;
    } finally {
        try {
            releaseLock(LOCK_TRIGGER_ACCESS, transOwner);
        } finally {
            try {
                releaseLock(LOCK_STATE_ACCESS, transStateOwner);
            } finally {
                cleanupConnection(conn);
            }
        }
    }
    firstCheckIn = false;
    return recovered;
}
Also used : JobPersistenceException(org.quartz.JobPersistenceException) Connection(java.sql.Connection)

Aggregations

JobPersistenceException (org.quartz.JobPersistenceException)23 SQLException (java.sql.SQLException)17 IOException (java.io.IOException)6 Date (java.util.Date)6 ObjectAlreadyExistsException (org.quartz.ObjectAlreadyExistsException)6 SchedulerException (org.quartz.SchedulerException)6 OperableTrigger (org.quartz.spi.OperableTrigger)6 JobDetail (org.quartz.JobDetail)5 Connection (java.sql.Connection)4 HashSet (java.util.HashSet)4 SchedulerConfigException (org.quartz.SchedulerConfigException)4 TriggerKey (org.quartz.TriggerKey)4 JobDataMap (org.quartz.JobDataMap)3 ArrayList (java.util.ArrayList)2 JobKey (org.quartz.JobKey)2 TriggerFiredBundle (org.quartz.spi.TriggerFiredBundle)2 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 NotSerializableException (java.io.NotSerializableException)1 PreparedStatement (java.sql.PreparedStatement)1