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

posted on 2020-01-20 11:12  海之浪子  阅读(8252)  评论(0编辑  收藏  举报

导航