亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Quartz作業注解@DisallowConcurrentExecution實現

Quartz作業注解@DisallowConcurrentExecution實現

森欄 2023-08-09 15:12:05
我是石英新手。我發現了@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
查看完整描述

1 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

我不參與石英項目。我在這里的所有評論都來自我出于好奇而對此事的調查,可能會遺漏一些信息。


首先要知道的是,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);

    }


}


查看完整回答
反對 回復 2023-08-09
  • 1 回答
  • 0 關注
  • 433 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號