189 8069 5689

浅谈SpringBoot集成Quartz动态定时任务

SpringBoot自带schedule

创新互联主营开州网站建设的网络公司,主营网站建设方案,重庆App定制开发,开州h5微信小程序搭建,开州网站营销推广欢迎开州等地区企业咨询

沿用的springboot少xml配置的优良传统,本身支持表达式等多种定时任务
注意在程序启动的时候加上@EnableScheduling

@Scheduled(cron="0/5 * * * * ?")
 public void job(){
  System.out.println("每五秒执行一次");
 }

为什么要使用Quartz

多任务情况下,quartz更容易管理,可以实现动态配置
执行时间表达式:

浅谈SpringBoot集成Quartz动态定时任务

表达式示例:

浅谈SpringBoot集成Quartz动态定时任务

集成Quartz

如何搭建springboot就不再细说,官网和网上的教程很多。

maven依赖


   org.quartz-scheduler
   quartz
   2.2.1
  

 测试Job类
SchedulerQuartzJob1和SchedulerQuartzJob2代码差不多就不一一列出

package com.yvan.quartz.job;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
 * 实现Job接口
 * @author yvan
 *
 */
public class SchedulerQuartzJob1 implements Job{
 private void before(){
  System.out.println("任务开始执行");
 }

 @Override
 public void execute(JobExecutionContext arg0) throws JobExecutionException {
  before();
  System.out.println("开始:"+System.currentTimeMillis());
  // TODO 业务
  System.out.println("结束:"+System.currentTimeMillis());
  after();
 }

 private void after(){
  System.out.println("任务开始执行");
 }

}

任务调用处理
将其拆分成两个部分

实例化Job,将任务触发器加入任务调度中

 private void startJob1(Scheduler scheduler) throws SchedulerException {
  // 通过JobBuilder构建JobDetail实例,JobDetail规定只能是实现Job接口的实例
  // JobDetail 是具体Job实例
  JobDetail jobDetail = JobBuilder.newJob(SchedulerQuartzJob1.class).withIdentity("job1", "group1").build();
  // 基于表达式构建触发器
  CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
  // CronTrigger表达式触发器 继承于Trigger
  // TriggerBuilder 用于构建触发器实例
  CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job1", "group1")
    .withSchedule(cronScheduleBuilder).build();
  scheduler.scheduleJob(jobDetail, cronTrigger);
 }

 private void startJob2(Scheduler scheduler) throws SchedulerException {
  JobDetail jobDetail = JobBuilder.newJob(SchedulerQuartzJob2.class).withIdentity("job2", "group2").build();
  CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/5 * * * ?");
  CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job2", "group2")
    .withSchedule(cronScheduleBuilder).build();
  scheduler.scheduleJob(jobDetail, cronTrigger);
 }

对外提供关于动态管理任务的方法

 /**
  * 开始执行所有任务
  * 
  * @throws SchedulerException
  */
 public void startJob() throws SchedulerException {
  startJob1(scheduler);
  startJob2(scheduler);
  scheduler.start();
 }

 /**
  * 获取Job信息
  * 
  * @param name
  * @param group
  * @return
  * @throws SchedulerException
  */
 public String getJobInfo(String name, String group) throws SchedulerException {
  TriggerKey triggerKey = new TriggerKey(name, group);
  CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
  return String.format("time:%s,state:%s", cronTrigger.getCronExpression(),
    scheduler.getTriggerState(triggerKey).name());
 }

 /**
  * 修改某个任务的执行时间
  * 
  * @param name
  * @param group
  * @param time
  * @return
  * @throws SchedulerException
  */
 public boolean modifyJob(String name, String group, String time) throws SchedulerException {
  Date date = null;
  TriggerKey triggerKey = new TriggerKey(name, group);
  CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
  String oldTime = cronTrigger.getCronExpression();
  if (!oldTime.equalsIgnoreCase(time)) {
   CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(time);
   CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(name, group)
     .withSchedule(cronScheduleBuilder).build();
   date = scheduler.rescheduleJob(triggerKey, trigger);
  }
  return date != null;
 }

 /**
  * 暂停所有任务
  * 
  * @throws SchedulerException
  */
 public void pauseAllJob() throws SchedulerException {
  scheduler.pauseAll();
 }

 /**
  * 暂停某个任务
  * 
  * @param name
  * @param group
  * @throws SchedulerException
  */
 public void pauseJob(String name, String group) throws SchedulerException {
  JobKey jobKey = new JobKey(name, group);
  JobDetail jobDetail = scheduler.getJobDetail(jobKey);
  if (jobDetail == null)
   return;
  scheduler.pauseJob(jobKey);
 }

 /**
  * 恢复所有任务
  * 
  * @throws SchedulerException
  */
 public void resumeAllJob() throws SchedulerException {
  scheduler.resumeAll();
 }

 /**
  * 恢复某个任务
  * 
  * @param name
  * @param group
  * @throws SchedulerException
  */
 public void resumeJob(String name, String group) throws SchedulerException {
  JobKey jobKey = new JobKey(name, group);
  JobDetail jobDetail = scheduler.getJobDetail(jobKey);
  if (jobDetail == null)
   return;
  scheduler.resumeJob(jobKey);
 }

 /**
  * 删除某个任务
  * 
  * @param name
  * @param group
  * @throws SchedulerException
  */
 public void deleteJob(String name, String group) throws SchedulerException {
  JobKey jobKey = new JobKey(name, group);
  JobDetail jobDetail = scheduler.getJobDetail(jobKey);
  if (jobDetail == null)
   return;
  scheduler.deleteJob(jobKey);
 }

完整代码

package com.yvan.quartz;

import java.util.Date;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.yvan.quartz.job.SchedulerQuartzJob1;
import com.yvan.quartz.job.SchedulerQuartzJob2;
/**
 * 任务调度处理
 * @author yvan
 *
 */
@Configuration
public class QuartzScheduler {
 // 任务调度
 @Autowired
 private Scheduler scheduler;

 /**
  * 开始执行所有任务
  * 
  * @throws SchedulerException
  */
 public void startJob() throws SchedulerException {
  startJob1(scheduler);
  startJob2(scheduler);
  scheduler.start();
 }

 /**
  * 获取Job信息
  * 
  * @param name
  * @param group
  * @return
  * @throws SchedulerException
  */
 public String getJobInfo(String name, String group) throws SchedulerException {
  TriggerKey triggerKey = new TriggerKey(name, group);
  CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
  return String.format("time:%s,state:%s", cronTrigger.getCronExpression(),
    scheduler.getTriggerState(triggerKey).name());
 }

 /**
  * 修改某个任务的执行时间
  * 
  * @param name
  * @param group
  * @param time
  * @return
  * @throws SchedulerException
  */
 public boolean modifyJob(String name, String group, String time) throws SchedulerException {
  Date date = null;
  TriggerKey triggerKey = new TriggerKey(name, group);
  CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
  String oldTime = cronTrigger.getCronExpression();
  if (!oldTime.equalsIgnoreCase(time)) {
   CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(time);
   CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(name, group)
     .withSchedule(cronScheduleBuilder).build();
   date = scheduler.rescheduleJob(triggerKey, trigger);
  }
  return date != null;
 }

 /**
  * 暂停所有任务
  * 
  * @throws SchedulerException
  */
 public void pauseAllJob() throws SchedulerException {
  scheduler.pauseAll();
 }

 /**
  * 暂停某个任务
  * 
  * @param name
  * @param group
  * @throws SchedulerException
  */
 public void pauseJob(String name, String group) throws SchedulerException {
  JobKey jobKey = new JobKey(name, group);
  JobDetail jobDetail = scheduler.getJobDetail(jobKey);
  if (jobDetail == null)
   return;
  scheduler.pauseJob(jobKey);
 }

 /**
  * 恢复所有任务
  * 
  * @throws SchedulerException
  */
 public void resumeAllJob() throws SchedulerException {
  scheduler.resumeAll();
 }

 /**
  * 恢复某个任务
  * 
  * @param name
  * @param group
  * @throws SchedulerException
  */
 public void resumeJob(String name, String group) throws SchedulerException {
  JobKey jobKey = new JobKey(name, group);
  JobDetail jobDetail = scheduler.getJobDetail(jobKey);
  if (jobDetail == null)
   return;
  scheduler.resumeJob(jobKey);
 }

 /**
  * 删除某个任务
  * 
  * @param name
  * @param group
  * @throws SchedulerException
  */
 public void deleteJob(String name, String group) throws SchedulerException {
  JobKey jobKey = new JobKey(name, group);
  JobDetail jobDetail = scheduler.getJobDetail(jobKey);
  if (jobDetail == null)
   return;
  scheduler.deleteJob(jobKey);
 }

 private void startJob1(Scheduler scheduler) throws SchedulerException {
  // 通过JobBuilder构建JobDetail实例,JobDetail规定只能是实现Job接口的实例
  // JobDetail 是具体Job实例
  JobDetail jobDetail = JobBuilder.newJob(SchedulerQuartzJob1.class).withIdentity("job1", "group1").build();
  // 基于表达式构建触发器
  CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
  // CronTrigger表达式触发器 继承于Trigger
  // TriggerBuilder 用于构建触发器实例
  CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job1", "group1")
    .withSchedule(cronScheduleBuilder).build();
  scheduler.scheduleJob(jobDetail, cronTrigger);
 }

 private void startJob2(Scheduler scheduler) throws SchedulerException {
  JobDetail jobDetail = JobBuilder.newJob(SchedulerQuartzJob2.class).withIdentity("job2", "group2").build();
  CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/5 * * * ?");
  CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job2", "group2")
    .withSchedule(cronScheduleBuilder).build();
  scheduler.scheduleJob(jobDetail, cronTrigger);
 }
}

Scheduler 注入
这里采用监听spring容器加载完毕后事件,启动任务调用
将Scheduler交给spring初始化管理

package com.yvan.quartz;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

@Configuration
public class ApplicationStartQuartzJobListener implements ApplicationListener{
 @Autowired
 private QuartzScheduler quartzScheduler;

 /**
  * 初始启动quartz
  */
 @Override
 public void onApplicationEvent(ContextRefreshedEvent event) {
  try {
   quartzScheduler.startJob();
   System.out.println("任务已经启动...");
  } catch (SchedulerException e) {
   e.printStackTrace();
  }
 }

 /**
  * 初始注入scheduler
  * @return
  * @throws SchedulerException
  */
 @Bean
 public Scheduler scheduler() throws SchedulerException{
  SchedulerFactory schedulerFactoryBean = new StdSchedulerFactory();
  return schedulerFactoryBean.getScheduler(); 
 }

}

封装API
这里封装的API主要是为了方便动态管理执行任务,当然依赖这些接口也可以做一个界面来更直观的操作管理任务,这个就不再赘述。

package com.yvan.controller.quartzApi;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yvan.quartz.QuartzScheduler;

/**
 * 这里并没有采用restful风格 只是简单封装了一下api
 * 
 * @author yvan
 *
 */
@RestController
@RequestMapping("/quartz")
public class QuartzApiController {
 @Autowired
 private QuartzScheduler quartzScheduler;

 @RequestMapping("/start")
 public void startQuartzJob() {
  try {
   quartzScheduler.startJob();
  } catch (SchedulerException e) {
   e.printStackTrace();
  }
 }

 @RequestMapping("/info")
 public String getQuartzJob(String name, String group) {
  String info = null;
  try {
   info = quartzScheduler.getJobInfo(name, group);
  } catch (SchedulerException e) {
   e.printStackTrace();
  }
  return info;
 }

 @RequestMapping("/modify")
 public boolean modifyQuartzJob(String name, String group, String time) {
  boolean flag = true;
  try {
   flag = quartzScheduler.modifyJob(name, group, time);
  } catch (SchedulerException e) {
   e.printStackTrace();
  }
  return flag;
 }

 @RequestMapping(value = "/pause")
 public void pauseQuartzJob(String name, String group) {
  try {
   quartzScheduler.pauseJob(name, group);
  } catch (SchedulerException e) {
   e.printStackTrace();
  }
 }

 @RequestMapping(value = "/pauseAll")
 public void pauseAllQuartzJob() {
  try {
   quartzScheduler.pauseAllJob();
  } catch (SchedulerException e) {
   e.printStackTrace();
  }
 }

 @RequestMapping(value = "/delete")
 public void deleteJob(String name, String group) {
  try {
   quartzScheduler.deleteJob(name, group);
  } catch (SchedulerException e) {
   e.printStackTrace();
  }
 }

}

运行结果

任务已经启动...
18-03-02 14:39:46,879  INFO TomcatEmbeddedServletContainer:201 - Tomcat started on port(s): 8080 (http)
-03-02 14:39:46,883  INFO Start:57 - Started Start in 3.18 seconds (JVM running for 3.47)
任务开始执行
开始:1519972790003
结束:1519972790003
任务开始执行
任务开始执行
开始:1519972795000
结束:1519972795000
任务开始执行

以上所述是小编给大家介绍的SpringBoot集成Quartz动态定时任务详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!


网站栏目:浅谈SpringBoot集成Quartz动态定时任务
URL分享:http://jkwzsj.com/article/iescge.html

其他资讯