Search in sources :

Example 16 with JobPersistenceException

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

the class JobStoreSupport method resetTriggerFromErrorState.

void resetTriggerFromErrorState(Connection conn, final TriggerKey triggerKey) throws JobPersistenceException {
    try {
        String newState = STATE_WAITING;
        if (getDelegate().isTriggerGroupPaused(conn, triggerKey.getGroup())) {
            newState = STATE_PAUSED;
        }
        getDelegate().updateTriggerStateFromOtherState(conn, triggerKey, newState, STATE_ERROR);
        getLog().info("Trigger " + triggerKey + " reset from ERROR state to: " + newState);
    } catch (SQLException e) {
        throw new JobPersistenceException("Couldn't reset from error state of trigger (" + triggerKey + "): " + e.getMessage(), e);
    }
}
Also used : SQLException(java.sql.SQLException) JobPersistenceException(org.quartz.JobPersistenceException)

Example 17 with JobPersistenceException

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

the class JobStoreSupport method resumeTriggerGroup.

/**
 * <p>
 * Resume (un-pause) all of the <code>{@link org.quartz.Trigger}s</code> matching the given groupMatcher.
 * </p>
 * <p>
 * If any <code>Trigger</code> missed one or more fire-times, then the <code>Trigger</code>'s misfire instruction will
 * be applied.
 * </p>
 *
 * @see #pauseTriggers(org.quartz.impl.matchers.GroupMatcher)
 */
public Set<String> resumeTriggerGroup(Connection conn, GroupMatcher<TriggerKey> matcher) throws JobPersistenceException {
    try {
        getDelegate().deletePausedTriggerGroup(conn, matcher);
        HashSet<String> groups = new HashSet<String>();
        Set<TriggerKey> keys = getDelegate().selectTriggersInGroup(conn, matcher);
        for (TriggerKey key : keys) {
            resumeTrigger(conn, key);
            groups.add(key.getGroup());
        }
        return groups;
    // FUTURE_TODO: find an efficient way to resume triggers (better than the
    // above)... logic below is broken because of
    // findTriggersToBeBlocked()
    /*
			 * int res = getDelegate().updateTriggerGroupStateFromOtherState(conn, groupName, STATE_WAITING, PAUSED); if(res > 0) {
			 * long misfireTime = System.currentTimeMillis(); if(getMisfireThreshold() > 0) misfireTime -= getMisfireThreshold();
			 * Key[] misfires = getDelegate().selectMisfiredTriggersInGroupInState(conn, groupName, STATE_WAITING, misfireTime);
			 * List blockedTriggers = findTriggersToBeBlocked(conn, groupName); Iterator itr = blockedTriggers.iterator();
			 * while(itr.hasNext()) { Key key = (Key)itr.next(); getDelegate().updateTriggerState(conn, key.getName(),
			 * key.getGroup(), BLOCKED); } for(int i=0; i < misfires.length; i++) { String newState = STATE_WAITING;
			 * if(blockedTriggers.contains(misfires[i])) newState = BLOCKED; updateMisfiredTrigger(conn, misfires[i].getName(),
			 * misfires[i].getGroup(), newState, true); } }
			 */
    } catch (SQLException e) {
        throw new JobPersistenceException("Couldn't pause trigger group '" + matcher + "': " + e.getMessage(), e);
    }
}
Also used : TriggerKey(org.quartz.TriggerKey) SQLException(java.sql.SQLException) JobPersistenceException(org.quartz.JobPersistenceException) HashSet(java.util.HashSet)

Example 18 with JobPersistenceException

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

the class JobStoreSupport method triggerFired.

protected TriggerFiredBundle triggerFired(Connection conn, OperableTrigger trigger) throws JobPersistenceException {
    JobDetail job;
    Calendar cal = null;
    // Make sure trigger wasn't deleted, paused, or completed...
    try {
        // if trigger was deleted, state will be STATE_DELETED
        String state = getDelegate().selectTriggerState(conn, trigger.getKey());
        if (!state.equals(STATE_ACQUIRED)) {
            return null;
        }
    } catch (SQLException e) {
        throw new JobPersistenceException("Couldn't select trigger state: " + e.getMessage(), e);
    }
    try {
        job = retrieveJob(conn, trigger.getJobKey());
        if (job == null) {
            return null;
        }
    } catch (JobPersistenceException jpe) {
        try {
            getLog().error("Error retrieving job, setting trigger state to ERROR.", jpe);
            getDelegate().updateTriggerState(conn, trigger.getKey(), STATE_ERROR);
        } catch (SQLException sqle) {
            getLog().error("Unable to set trigger state to ERROR.", sqle);
        }
        throw jpe;
    }
    if (trigger.getCalendarName() != null) {
        cal = retrieveCalendar(conn, trigger.getCalendarName());
        if (cal == null) {
            return null;
        }
    }
    try {
        getDelegate().updateFiredTrigger(conn, trigger, STATE_EXECUTING, job);
    } catch (SQLException e) {
        throw new JobPersistenceException("Couldn't insert fired trigger: " + e.getMessage(), e);
    }
    Date prevFireTime = trigger.getPreviousFireTime();
    // call triggered - to update the trigger's next-fire-time state...
    trigger.triggered(cal);
    String state = STATE_WAITING;
    boolean force = true;
    if (job.isConcurrentExectionDisallowed()) {
        state = STATE_BLOCKED;
        force = false;
        try {
            getDelegate().updateTriggerStatesForJobFromOtherState(conn, job.getKey(), STATE_BLOCKED, STATE_WAITING);
            getDelegate().updateTriggerStatesForJobFromOtherState(conn, job.getKey(), STATE_BLOCKED, STATE_ACQUIRED);
            getDelegate().updateTriggerStatesForJobFromOtherState(conn, job.getKey(), STATE_PAUSED_BLOCKED, STATE_PAUSED);
        } catch (SQLException e) {
            throw new JobPersistenceException("Couldn't update states of blocked triggers: " + e.getMessage(), e);
        }
    }
    if (trigger.getNextFireTime() == null) {
        state = STATE_COMPLETE;
        force = true;
    }
    storeTrigger(conn, trigger, job, true, state, force, false);
    job.getJobDataMap().clearDirtyFlag();
    return new TriggerFiredBundle(job, trigger, cal, trigger.getKey().getGroup().equals(Scheduler.DEFAULT_RECOVERY_GROUP), new Date(), trigger.getPreviousFireTime(), prevFireTime, trigger.getNextFireTime());
}
Also used : JobDetail(org.quartz.JobDetail) SQLException(java.sql.SQLException) JobPersistenceException(org.quartz.JobPersistenceException) Calendar(org.quartz.Calendar) TriggerFiredBundle(org.quartz.spi.TriggerFiredBundle) Date(java.util.Date)

Example 19 with JobPersistenceException

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

the class JobStoreSupport method executeInNonManagedTXLock.

/**
 * Execute the given callback having optionally acquired the given lock. This uses the non-managed transaction
 * connection.
 *
 * @param lockName The name of the lock to acquire, for example "TRIGGER_ACCESS". If null, then no lock is acquired, but
 *                 the lockCallback is still executed in a non-managed transaction.
 */
protected <T> T executeInNonManagedTXLock(String lockName, TransactionCallback<T> txCallback, final TransactionValidator<T> txValidator) throws JobPersistenceException {
    boolean transOwner = false;
    Connection conn = null;
    try {
        if (lockName != null) {
            // until after acquiring the lock since it isn't needed.
            if (getLockHandler().requiresConnection()) {
                conn = getNonManagedTXConnection();
            }
            transOwner = getLockHandler().obtainLock(conn, lockName);
        }
        if (conn == null) {
            conn = getNonManagedTXConnection();
        }
        final T result = txCallback.execute(conn);
        try {
            commitConnection(conn);
        } catch (JobPersistenceException e) {
            rollbackConnection(conn);
            if (txValidator == null || !retryExecuteInNonManagedTXLock(lockName, new TransactionCallback<Boolean>() {

                @Override
                public Boolean execute(Connection conn) throws JobPersistenceException {
                    return txValidator.validate(conn, result);
                }
            })) {
                throw e;
            }
        }
        Long sigTime = clearAndGetSignalSchedulingChangeOnTxCompletion();
        if (sigTime != null && sigTime >= 0) {
            signalSchedulingChangeImmediately(sigTime);
        }
        return result;
    } catch (JobPersistenceException e) {
        rollbackConnection(conn);
        throw e;
    } catch (RuntimeException e) {
        rollbackConnection(conn);
        throw new JobPersistenceException("Unexpected runtime exception: " + e.getMessage(), e);
    } finally {
        try {
            releaseLock(lockName, transOwner);
        } finally {
            cleanupConnection(conn);
        }
    }
}
Also used : JobPersistenceException(org.quartz.JobPersistenceException) Connection(java.sql.Connection)

Example 20 with JobPersistenceException

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

the class JobStoreSupport method doRecoverMisfires.

// ---------------------------------------------------------------------------
// Management methods
// ---------------------------------------------------------------------------
protected RecoverMisfiredJobsResult doRecoverMisfires() throws JobPersistenceException {
    boolean transOwner = false;
    Connection conn = getNonManagedTXConnection();
    try {
        RecoverMisfiredJobsResult result = RecoverMisfiredJobsResult.NO_OP;
        // Before we make the potentially expensive call to acquire the
        // trigger lock, peek ahead to see if it is likely we would find
        // misfired triggers requiring recovery.
        int misfireCount = (getDoubleCheckLockMisfireHandler()) ? getDelegate().countMisfiredTriggersInState(conn, STATE_WAITING, getMisfireTime()) : Integer.MAX_VALUE;
        if (misfireCount == 0) {
            getLog().debug("Found 0 triggers that missed their scheduled fire-time.");
        } else {
            transOwner = getLockHandler().obtainLock(conn, LOCK_TRIGGER_ACCESS);
            result = recoverMisfiredJobs(conn, false);
        }
        commitConnection(conn);
        return result;
    } catch (JobPersistenceException e) {
        rollbackConnection(conn);
        throw e;
    } catch (SQLException e) {
        rollbackConnection(conn);
        throw new JobPersistenceException("Database error recovering from misfires.", e);
    } catch (RuntimeException e) {
        rollbackConnection(conn);
        throw new JobPersistenceException("Unexpected runtime exception: " + e.getMessage(), e);
    } finally {
        try {
            releaseLock(LOCK_TRIGGER_ACCESS, transOwner);
        } finally {
            cleanupConnection(conn);
        }
    }
}
Also used : JobPersistenceException(org.quartz.JobPersistenceException) SQLException(java.sql.SQLException) 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