一个定时任务调度框架,简单易用,功能强大可以使实现定时任务的。
在项目开发过程当中,某些定时任务,可能在运行一段时间之后,就不需要了,或者需要修改下定时任务的执行时间等等。
需要在代码当中进行修改然后重新打包发布,很麻烦。使用Quartz来实现的话不需要重新修改代码而达到要求。
<!--引入quartz定时框架-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
/*
Navicat MySQL Data Transfer
Source Server : mysql
Source Server Version : 50731
Source Host : localhost:3306
Source Database : school
Target Server Type : MYSQL
Target Server Version : 50731
File Encoding : 65001
Date: 2022-04-02 17:17:46
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for quartz
-- ----------------------------
DROP TABLE IF EXISTS `quartz`;
CREATE TABLE `quartz` (
`id` bigint(20) NOT NULL,
`job_name` varchar(255) DEFAULT NULL COMMENT '任务名称',
`job_class` varchar(255) DEFAULT NULL COMMENT '任务执行类',
`status` varchar(255) DEFAULT NULL COMMENT '任务状态',
`cron` varchar(255) DEFAULT NULL COMMENT '时间表达式 ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
另外的实体类、mapper、xml 自填
public class QuartzUtils {
/**
* 创建定时任务 定时任务创建之后默认启动状态
* @param scheduler 调度器
* @param quartzBean 定时任务信息类
* @throws Exception
*/
public static void createScheduleJob(Scheduler scheduler, QuartzBean quartzBean){
try {
//获取到定时任务的执行类 必须是类的绝对路径名称
//定时任务类需要是job类的具体实现 QuartzJobBean是job的抽象类。
Class<? extends Job> jobClass = (Class<? extends Job>) Class.forName(quartzBean.getJobClass());
// 构建定时任务信息
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(quartzBean.getJobName()).build();
// 设置定时任务执行方式
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartzBean.getCronExpression());
// 构建触发器trigger
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(quartzBean.getJobName()).withSchedule(scheduleBuilder).build();
scheduler.scheduleJob(jobDetail, trigger);
} catch (ClassNotFoundException e) {
System.out.println("定时任务类路径出错:请输入类的绝对路径");
} catch (SchedulerException e) {
System.out.println("创建定时任务出错:"+e.getMessage());
}
}
/**
* 根据任务名称暂停定时任务
* @param scheduler 调度器
* @param jobName 定时任务名称
* @throws SchedulerException
*/
public static void pauseScheduleJob(Scheduler scheduler, String jobName){
JobKey jobKey = JobKey.jobKey(jobName);
try {
scheduler.pauseJob(jobKey);
} catch (SchedulerException e) {
System.out.println("暂停定时任务出错:"+e.getMessage());
}
}
/**
* 根据任务名称恢复定时任务
* @param scheduler 调度器
* @param jobName 定时任务名称
* @throws SchedulerException
*/
public static void resumeScheduleJob(Scheduler scheduler, String jobName) {
JobKey jobKey = JobKey.jobKey(jobName);
try {
scheduler.resumeJob(jobKey);
} catch (SchedulerException e) {
System.out.println("启动定时任务出错:"+e.getMessage());
}
}
/**
* 根据任务名称立即运行一次定时任务
* @param scheduler 调度器
* @param jobName 定时任务名称
* @throws SchedulerException
*/
public static void runOnce(Scheduler scheduler, String jobName){
JobKey jobKey = JobKey.jobKey(jobName);
try {
scheduler.triggerJob(jobKey);
} catch (SchedulerException e) {
System.out.println("运行定时任务出错:"+e.getMessage());
}
}
/**
* 更新定时任务
* @param scheduler 调度器
* @param quartzBean 定时任务信息类
* @throws SchedulerException
*/
public static void updateScheduleJob(Scheduler scheduler, QuartzBean quartzBean) {
try {
//获取到对应任务的触发器
TriggerKey triggerKey = TriggerKey.triggerKey(quartzBean.getJobName());
//设置定时任务执行方式
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartzBean.getCronExpression());
//重新构建任务的触发器trigger
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
//重置对应的job
scheduler.rescheduleJob(triggerKey, trigger);
} catch (SchedulerException e) {
System.out.println("更新定时任务出错:"+e.getMessage());
}
}
/**
* 根据定时任务名称从调度器当中删除定时任务
* @param scheduler 调度器
* @param jobName 定时任务名称
* @throws SchedulerException
*/
public static void deleteScheduleJob(Scheduler scheduler, String jobName) {
JobKey jobKey = JobKey.jobKey(jobName);
try {
scheduler.deleteJob(jobKey);
} catch (SchedulerException e) {
System.out.println("删除定时任务出错:"+e.getMessage());
}
}
}
public class SimpleTask extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println("==avb==" + new Date() );
}
}
@Controller
@RequestMapping("/quartz/")
public class QuartzController {
//注入任务调度
@Autowired
private Scheduler scheduler;
@RequestMapping("/createJob")
@ResponseBody
public String createJob(QuartzBean quartzBean) {
try {
//进行测试所以写死
quartzBean.setJobClass("com.hjljy.blog.Quartz.MyTask1");
quartzBean.setJobName("test1");
quartzBean.setCronExpression("*/10 * * * * ?");
QuartzUtils.createScheduleJob(scheduler,quartzBean);
} catch (Exception e) {
return "创建失败";
}
return "创建成功";
}
@RequestMapping("/pauseJob")
@ResponseBody
public String pauseJob() {
try {
QuartzUtils.pauseScheduleJob (scheduler,"test1");
} catch (Exception e) {
return "暂停失败";
}
return "暂停成功";
}
@RequestMapping("/runOnce")
@ResponseBody
public String runOnce() {
try {
QuartzUtils.runOnce (scheduler,"test1");
} catch (Exception e) {
return "运行一次失败";
}
return "运行一次成功";
}
@RequestMapping("/resume")
@ResponseBody
public String resume() {
try {
QuartzUtils.resumeScheduleJob(scheduler,"test1");
} catch (Exception e) {
return "启动失败";
}
return "启动成功";
}
@RequestMapping("/update")
@ResponseBody
public String update(QuartzBean quartzBean) {
try {
//进行测试所以写死
quartzBean.setJobClass("com.hjljy.blog.Quartz.MyTask1");
quartzBean.setJobName("test1");
quartzBean.setCronExpression("10 * * * * ?");
QuartzUtils.updateScheduleJob(scheduler,quartzBean);
} catch (Exception e) {
return "启动失败";
}
return "启动成功";
}
}
可在 每个controller 执行QuartzUtils 之前调用进行sql 表的CRUD的检验
码云:https://gitee.com/lsnobug/springboot-quartz.git