springboot Quartz动态修改cron表达式 springboot Quartz动态修改cron表达式的方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

springboot Quartz动态修改cron表达式 springboot Quartz动态修改cron表达式的方法

追风的独角鲸   2021-03-30 我要评论

1、概述: 在开发中有的时候需要去手动禁止和启用定时任务,修改定时任务的cron表达式然后再让其动态生效,之前有过SSM的类似的业务的开发但是忘记写下来了。。。只好重新温习了一次,加上最近比较流行springBoot所以升级了一下用springBoot来完成.

2、关联技术 SpringBoot、Quartz、H2、thymeleaf (好像就这么多)

3、具体流程      

1)首先去手动创建一个调度器工厂对象-SchedulerFactoryBean;其实应该不用手动创建的但是为了顾及到业务的复杂性所以还是创建一个好用。

 @Bean
  public SchedulerFactoryBean schedulerFactory(){
    SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
    /*用于Quartz集群,启动时更新已存在的Job*/
    factoryBean.setOverwriteExistingJobs(true);
    /*定时任务开始启动后延迟5秒开始*/
    factoryBean.setStartupDelay(5);
    return factoryBean;
  }

2)获取到

//得到调度器
Scheduler scheduler = schedulerFactoryBean.getScheduler();

3)判断是否有触发器-trigger存在其中,因为有可能说上次的触发器 并没有删除

//获得触发器
TriggerKey triggerKey = TriggerKey.triggerKey(config.getName(), config.getGroup());
CronTrigger trigger = (CronTrigger)scheduler.getTrigger(triggerKey);

4)创建一个任务类需要继承Job,实现方法execute。需要在其中执行定时任务如下:

//注释作用,当上一个任务未结束时下一个任务需进行等待
@DisallowConcurrentExecution
public class QuartzJobFactory implements Job {
  public static final  String SCHEDULEJOBKEY="scheduleJob";
  //execute会根据cron的规则进行执行
  @Override
  public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {   
        Config config = (Config) jobExecutionContext.getMergedJobDataMap().get(SCHEDULEJOBKEY);
        TaskUtils.invokMethod(config);
    }
}

5)将执行实例添加到任务当中去,我在例子是将执行任务的信息封装到了对象config当中然后在任务QuartzJobFactoryz中进行解读的

public static void invokMethod(Config config){
    Object obj=null;
    Class clazz=null;
    //通过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");  
   }   
   //方法执行

    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)创建触发器并且绑定cron表达式

7)在调度器中将触发器和任务进行组合 详情见:com.study.www.service.QuartzTableservice.addJob

  //将cron表达式进行转换    
  CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(config.getCron());   
  //创建触发器并将cron表达式对象给塞入
  trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(cronScheduleBuilder).build();
  //在调度器中将触发器和任务进行组合
  scheduler.scheduleJob(jobDetail,trigger);

github:点击打开链接

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们