java - Quartz 作业注解@DisallowConcurrentExecution 实现
问题描述
我是石英的新手。我发现了@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 {
}
但是,我找不到真正负责不为同一作业并发执行的实现。这对我来说是新的,所以有人可以帮助我解释内部实现逻辑。
我试图查找用法但只在课堂上找到它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 决定在该实例上运行或不运行 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);
}
}
推荐阅读
- android - neshan 服务 API 密钥仅在调试模式下工作
- android - 将 gradle 插件版本升级到 7.0.0 会导致 HiltTestRunner 出现问题
- css - 将剪辑路径从右下角旋转到左下角
- mysql - If I know that the rows of a database table are inserted sequentially, can I use this to speed up SELECT queries?
- laravel-8 - 目标类 [App\Http\Controllers\Admin\Blog] 不存在
- python - 按下按钮时生成的 Kivy 三个按钮。按下新按钮之一应删除所有 3 个新按钮
- php - 使用php数学公式根据距离和重量计算运费
- git - ssh:无法解析主机名 github.com:名称解析临时失败致命:无法从远程存储库读取
- flutter - 颤振 - 计算 - 隔离消息中的非法参数:对象是一个闭包 - 顶级函数调用
- laravel - 错误 111 颤振 VS Laravel 8