gpt4 book ai didi

java - Quartz Job注解@DisallowConcurrentExecution实现

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:30:20 36 4
gpt4 key购买 nike

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

'将 {@link Job} 类标记为不能同时执行多个实例的注释(其中实例基于 {@link JobDetail} 定义 - 或者换句话说基于{@link JobKey}).'

DisallowConcurrentExecution.java 写成:

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

}

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

我试图查找用法,但只在类 MethodInvokingJobDetailFactoryBean.java 中找到它

最佳答案

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

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

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

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

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

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

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

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

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);
}

}

关于java - Quartz Job注解@DisallowConcurrentExecution实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58009728/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com