Quartz Job注解@DisallowConcurrentExecution实现

Quartz Job Annotation @DisallowConcurrentExecution Implementation

我是石英的新手。我发现了石英库提供的 @DisallowConcurrentExecution 注释,文档说:

'An annotation that marks a {@link Job} class as one that must not have multiple instances executed concurrently (where instance is based-upon a {@link JobDetail} definition - or in other words based upon a {@link JobKey}).'

DisallowConcurrentExecution.java写成:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface DisallowConcurrentExecution {

}

但是,我找不到真正处理同一个作业没有并发执行的实现。这对我来说是新的,所以有人可以帮我解释一下内部实现逻辑。

我试图查找用法,但只在 class MethodInvokingJobDetailFactoryBean.java

中找到

免责声明:我没有参与石英项目。我在这里的所有评论都来自我出于好奇而对此进行的调查,可能缺少一些信息。

首先要知道的是,JobDetailImpl 将检查注释是否存在,并使此信息在方法中可用。

/**
 * @return whether the associated Job class carries the {@link DisallowConcurrentExecution} annotation.
 */
public boolean isConcurrentExectionDisallowed() {

    return ClassUtils.isAnnotationPresent(jobClass, DisallowConcurrentExecution.class);
}

然后你可以看到这个方法在系统的不同部分被检查了。

例如,JobStoreSupport 会在此处检查它,如果存在注释,则会检查块状态:

    if (job.isConcurrentExectionDisallowed() && !recovering) { 
        state = checkBlockedState(conn, job.getKey(), state);
    }

这里是实际验证发生的地方,让 Quartz 决定 运行 或不 运行 该实例上的作业。

org.quartz.impl.jdbcjobstore.JobStoreSupport#checkBlockedState

    /**
     * Determines if a Trigger for the given job should be blocked.  
     * State can only transition to STATE_PAUSED_BLOCKED/BLOCKED from 
     * PAUSED/STATE_WAITING respectively.
     * 
     * @return STATE_PAUSED_BLOCKED, BLOCKED, or the currentState. 
     */
    protected String checkBlockedState(
            Connection conn, JobKey jobKey, String currentState)
        throws JobPersistenceException {

    // State can only transition to BLOCKED from PAUSED or WAITING.
    if ((!currentState.equals(STATE_WAITING)) &&
        (!currentState.equals(STATE_PAUSED))) {
        return currentState;
    }

    try {
        List<FiredTriggerRecord> lst = getDelegate().selectFiredTriggerRecordsByJob(conn,
                jobKey.getName(), jobKey.getGroup());

        if (lst.size() > 0) {
            FiredTriggerRecord rec = lst.get(0);
            if (rec.isJobDisallowsConcurrentExecution()) { // OLD_TODO: worry about failed/recovering/volatile job  states?
                return (STATE_PAUSED.equals(currentState)) ? STATE_PAUSED_BLOCKED : STATE_BLOCKED;
            }
        }

        return currentState;
    } catch (SQLException e) {
        throw new JobPersistenceException(
            "Couldn't determine if trigger should be in a blocked state '"
                + jobKey + "': "
                + e.getMessage(), e);
    }

}