欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot Quartz動(dòng)態(tài)修改cron表達(dá)式的方法

 更新時(shí)間:2018年09月29日 10:21:21   作者:追風(fēng)的獨(dú)角鯨  
這篇文章主要介紹了springboot Quartz動(dòng)態(tài)修改cron表達(dá)式的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

1、概述: 在開(kāi)發(fā)中有的時(shí)候需要去手動(dòng)禁止和啟用定時(shí)任務(wù),修改定時(shí)任務(wù)的cron表達(dá)式然后再讓其動(dòng)態(tài)生效,之前有過(guò)SSM的類(lèi)似的業(yè)務(wù)的開(kāi)發(fā)但是忘記寫(xiě)下來(lái)了。。。只好重新溫習(xí)了一次,加上最近比較流行springBoot所以升級(jí)了一下用springBoot來(lái)完成.

2、關(guān)聯(lián)技術(shù) SpringBoot、Quartz、H2、thymeleaf (好像就這么多)

3、具體流程      

1)首先去手動(dòng)創(chuàng)建一個(gè)調(diào)度器工廠對(duì)象-SchedulerFactoryBean;其實(shí)應(yīng)該不用手動(dòng)創(chuàng)建的但是為了顧及到業(yè)務(wù)的復(fù)雜性所以還是創(chuàng)建一個(gè)好用。

 @Bean
  public SchedulerFactoryBean schedulerFactory(){
    SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
    /*用于Quartz集群,啟動(dòng)時(shí)更新已存在的Job*/
    factoryBean.setOverwriteExistingJobs(true);
    /*定時(shí)任務(wù)開(kāi)始啟動(dòng)后延遲5秒開(kāi)始*/
    factoryBean.setStartupDelay(5);
    return factoryBean;
  }

2)獲取到

//得到調(diào)度器
Scheduler scheduler = schedulerFactoryBean.getScheduler();

3)判斷是否有觸發(fā)器-trigger存在其中,因?yàn)橛锌赡苷f(shuō)上次的觸發(fā)器 并沒(méi)有刪除

//獲得觸發(fā)器
TriggerKey triggerKey = TriggerKey.triggerKey(config.getName(), config.getGroup());
CronTrigger trigger = (CronTrigger)scheduler.getTrigger(triggerKey);

4)創(chuàng)建一個(gè)任務(wù)類(lèi)需要繼承Job,實(shí)現(xiàn)方法execute。需要在其中執(zhí)行定時(shí)任務(wù)如下:

//注釋作用,當(dāng)上一個(gè)任務(wù)未結(jié)束時(shí)下一個(gè)任務(wù)需進(jìn)行等待
@DisallowConcurrentExecution
public class QuartzJobFactory implements Job {
  public static final  String SCHEDULEJOBKEY="scheduleJob";
  //execute會(huì)根據(jù)cron的規(guī)則進(jìn)行執(zhí)行
  @Override
  public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {   
        Config config = (Config) jobExecutionContext.getMergedJobDataMap().get(SCHEDULEJOBKEY);
        TaskUtils.invokMethod(config);
    }
}

5)將執(zhí)行實(shí)例添加到任務(wù)當(dāng)中去,我在例子是將執(zhí)行任務(wù)的信息封裝到了對(duì)象config當(dāng)中然后在任務(wù)QuartzJobFactoryz中進(jìn)行解讀的

public static void invokMethod(Config config){
    Object obj=null;
    Class clazz=null;
    //通過(guò)Spring上下文去找 也有可能找不到
   try {
      obj= SpringUtils.getBean(config.getClassPath().split("\\.")[config.getClassPath().split("\\.").length - 1]);
   if (obj == null){
        clazz = Class.forName(config.getClassPath());
        obj = clazz.newInstance();
      }else{
       clazz =obj.getClass();
      }
    }catch (Exception e){
 throw new RuntimeException("ERROR:TaskUtils is Bean Create please check the classpath is`t right or not");
    }
 Method method=null;
    //獲得方法名
    try {
      method = clazz.getDeclaredMethod(config.getMethodName());
   } catch (NoSuchMethodException e) {   
   throw new RuntimeException("ERROR:TaskUtils is Bean the method Create please check the methodName is`t right or not");  
   }   
   //方法執(zhí)行

    try {
      method.invoke(obj);
    } catch (Exception e) {
   throw new RuntimeException("ERROR:TaskUtils is Bean the method execute please check the methodName is`t right or not");
    }
  }

6)創(chuàng)建觸發(fā)器并且綁定cron表達(dá)式

7)在調(diào)度器中將觸發(fā)器和任務(wù)進(jìn)行組合 詳情見(jiàn):com.study.www.service.QuartzTableservice.addJob

  //將cron表達(dá)式進(jìn)行轉(zhuǎn)換    
  CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(config.getCron());   
  //創(chuàng)建觸發(fā)器并將cron表達(dá)式對(duì)象給塞入
  trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(cronScheduleBuilder).build();
  //在調(diào)度器中將觸發(fā)器和任務(wù)進(jìn)行組合
  scheduler.scheduleJob(jobDetail,trigger);

github:點(diǎn)擊打開(kāi)鏈接

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論