DisallowConcurrentExecution注解


  • @DisallowConcurrentExecution

    @DisallowConcurrentExecution:禁止並發執行多個相同定義的JobDetail, 這個注解是加在Job類上的, 但意思並不是不能同時執行多個Job,而是不能並發執行同一個Job Definition(由JobDetail定義), 但是可以同時執行多個不同的JobDetail。
    @PersistJobDataAfterExecution:加在Job上,表示當正常執行完Job后, JobDataMap中的數據應該被改動,下次執行相同的job時,會接受到已經更新的數據。

    相關代碼:

    public class JobStateTriggerMain {
    
        public static void main(String[] args) throws Exception {
        
            //獲取調度實例
            SchedulerFactory sf = new StdSchedulerFactory();
            Scheduler sched = sf.getScheduler();
            // get a "nice round" time a few seconds in the future....
            Date startTime = nextGivenSecondDate(null, 10);
        
            // job1將會執行5次,並且時間間隔是10seconds
            JobDetail job1 = newJob(ColorJob.class).withIdentity("job1", "group1").build();
            SimpleTrigger trigger1 = newTrigger().withIdentity("trigger1", "group1").startAt(startTime)
                    .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(4)).build();
            job1.getJobDataMap().put(ColorJob.FAVORITE_COLOR, "Green");
            job1.getJobDataMap().put(ColorJob.EXECUTION_COUNT, 1);
        
            Date scheduleTime1 = sched.scheduleJob(job1, trigger1);
    
    
    
            JobDetail job2 = newJob(ColorJob.class).withIdentity("job2", "group1").build();
            SimpleTrigger trigger2 = newTrigger().withIdentity("trigger2", "group1").startAt(startTime)
                    .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(4)).build();
            job2.getJobDataMap().put(ColorJob.FAVORITE_COLOR, "Red");
            job2.getJobDataMap().put(ColorJob.EXECUTION_COUNT, 1);
        
            // schedule the job to run
            Date scheduleTime2 = sched.scheduleJob(job2, trigger2);
    
    
    
            sched.start();
        
            Thread.sleep(60L * 1000L);
    
    
            //所有job結束時,停止任務
            sched.shutdown(true);
        }
    
    }
    
    @PersistJobDataAfterExecution
    //表示 Quartz 將會在成功執行 execute() 方法后(沒有拋出異常)更新 JobDetail 的 JobDataMap,下一次執行相同的任務(JobDetail)將會得到更新后的值,而不是原始的值
    @DisallowConcurrentExecution //定義不能同時並發執行相同的JobDetail
    public class ColorJob implements Job {
    
        public static final String FAVORITE_COLOR = "favorite color";
        public static final String EXECUTION_COUNT = "count";
        
        private int _counter = 1;
        
        public ColorJob() {
        }
        
        public void execute(JobExecutionContext context)
                throws JobExecutionException {
        
            //獲取執行該任務的JobKey
                JobKey jobKey = context.getJobDetail().getKey();
    
            JobDataMap data = context.getJobDetail().getJobDataMap();
            String favoriteColor = data.getString(FAVORITE_COLOR);
            int count = data.getInt(EXECUTION_COUNT);
            System.out.println("ColorJob: " + jobKey + " executing at " + new Date() + "\n" +
                    "  favorite color is " + favoriteColor + "\n" +
                    "  execution count (from job map) is " + count + "\n" +
                    "  execution count (from job member variable) is " + _counter);
    
    
            count++;
            data.put(EXECUTION_COUNT, count);
    
    
            _counter++;
        }
    
    }
    

相關源碼參考: https://github.com/albert-liu435/springquartz


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM