zoukankan      html  css  js  c++  java
  • 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() + "
      " +
                      "  favorite color is " + favoriteColor + "
      " +
                      "  execution count (from job map) is " + count + "
      " +
                      "  execution count (from job member variable) is " + _counter);
      
      
              count++;
              data.put(EXECUTION_COUNT, count);
      
      
              _counter++;
          }
      
      }
      

    相关源码参考: https://github.com/albert-liu435/springquartz

  • 相关阅读:
    实验二 20159213
    读书笔记二 20159213
    读书随笔 20159213
    keycode对照表
    jquery ajax中各个事件执行顺序如下
    移动端web开发技巧
    zepto处理touch事件
    CSS控制文字只显示一行,超出部分显示省略号
    input按钮的事件处理大全
    jquery中ajax处理跨域的三大方式
  • 原文地址:https://www.cnblogs.com/haizhilangzi/p/12217135.html
Copyright © 2011-2022 走看看