猿问

Quartz作业注解@DisallowConcurrentExecution实现

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


'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


森栏
浏览 288回答 1
1回答

泛舟湖上清波郎朗

我不参与石英项目。我在这里的所有评论都来自我出于好奇而对此事的调查,可能会遗漏一些信息。首先要知道的是,JobDetailImpl 将检查注释是否存在并在方法中提供此信息。/**&nbsp;* @return whether the associated Job class carries the {@link DisallowConcurrentExecution} annotation.&nbsp;*/public boolean isConcurrentExectionDisallowed() {&nbsp; &nbsp; return ClassUtils.isAnnotationPresent(jobClass, DisallowConcurrentExecution.class);}然后你可以看到这个方法在系统的不同部分被检查。例如,JobStoreSupport 在此处检查它,如果存在注释,则它会检查块状态:&nbsp; &nbsp; if (job.isConcurrentExectionDisallowed() && !recovering) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; state = checkBlockedState(conn, job.getKey(), state);&nbsp; &nbsp; }这里是实际验证发生的地方,并让 Quartz 决定在该实例上运行或不运行作业。org.quartz.impl.jdbcjobstore.JobStoreSupport#checkBlockedState&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Determines if a Trigger for the given job should be blocked.&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;* State can only transition to STATE_PAUSED_BLOCKED/BLOCKED from&nbsp;&nbsp; &nbsp; &nbsp;* PAUSED/STATE_WAITING respectively.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @return STATE_PAUSED_BLOCKED, BLOCKED, or the currentState.&nbsp;&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected String checkBlockedState(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Connection conn, JobKey jobKey, String currentState)&nbsp; &nbsp; &nbsp; &nbsp; throws JobPersistenceException {&nbsp; &nbsp; // State can only transition to BLOCKED from PAUSED or WAITING.&nbsp; &nbsp; if ((!currentState.equals(STATE_WAITING)) &&&nbsp; &nbsp; &nbsp; &nbsp; (!currentState.equals(STATE_PAUSED))) {&nbsp; &nbsp; &nbsp; &nbsp; return currentState;&nbsp; &nbsp; }&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; List<FiredTriggerRecord> lst = getDelegate().selectFiredTriggerRecordsByJob(conn,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jobKey.getName(), jobKey.getGroup());&nbsp; &nbsp; &nbsp; &nbsp; if (lst.size() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FiredTriggerRecord rec = lst.get(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (rec.isJobDisallowsConcurrentExecution()) { // OLD_TODO: worry about failed/recovering/volatile job&nbsp; states?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (STATE_PAUSED.equals(currentState)) ? STATE_PAUSED_BLOCKED : STATE_BLOCKED;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return currentState;&nbsp; &nbsp; } catch (SQLException e) {&nbsp; &nbsp; &nbsp; &nbsp; throw new JobPersistenceException(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Couldn't determine if trigger should be in a blocked state '"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + jobKey + "': "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + e.getMessage(), e);&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答