目录
- 前言
- 一、添加pom.XML依赖
- 二、application.properties添加数据库信息
- 三、数据库操作
- 1.创建数据库test和表cron
- 2.创建cron表对应的实体类
- 3.创建Mapper
- 4.创建Service
- 二、Quartz相关
- 1.创建JobConfig类
- 2.创建Job类
- 总结
前言
在做定时任务时,有需求是把定时执行cron表达式存在数据库中,通过更改数据库中的值,来改变任务执行的频率。
一、添加pom.xml依赖
<!--定时器依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> <!--myBATis插件--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--数据库连接插件--> <dependency> <groupId>mysql</groupId> <artifactId>my编程sql-connector-Java</artifactId> <version>8.0.13</version> <scope>runtime</scope> </dependency>
二、application.properties添加数据库信息
spring.datasource.url:jdbc:mysql://localhost:3306/test?setUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false spring.datasource.username:root spring.datasource.password:root
三、数据库操作
1.创建数据库tepythonst和表cron
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------javascript------------------- -- Table structure for cron -- ---------------------------- DROP TABLE IF EXISTS `cron`; CREATE TABLE `cron` ( `id` int NOT NULL AUTO_INCREMENT, `cron` varchar(255) CHARACTER SET utf8 COLLATE pythonutf8_general_ci NOT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of cron -- ---------------------------- INSERT INTO `cron` VALUES (1, '0/5 * * * * ? *');
cron表:
2.创建cron表对应的实体类
public class CronModel { private String cron; public String getCron() { return cron; } public void setCron(String cron) { this.cron = cron; } }
3.创建Mapper
@Mapper public interface CronModelMapper { @Select("select cron from cron where id = 1") String getCron(); }
4.创建Service
@Ser开发者_JS学习vice public class CronModelService { @Autowired CronModelMapper cronModelMapper; public String getCron() { return cronModelMapper.getCron(); } }
二、Quartz相关
1.创建JobConfig类
/** * Quartz的相关配置,注册JobDetail和Trigger * 注意JobDetail和Trigger是org.quartz包下的,不是spring包下的,不要导入错误 */ @Configuration public class JobConfig { @Autowired CronModelService cronModelService; @Bean public JobDetail jobDetail() { JobDetail jobDetail = JobBuilder.newJob(Job.class) .withIdentity("jobDetail", "jobDetail") .storeDurably() .build(); return jobDetail; } @Bean public Trigger trigger() { Trigger trigger = TriggerBuilder.newTrigger() 编程客栈 .forJob(jobDetail()) .withIdentity("trigger", "trigger") .startNow() // 每天0点执行 .withSchedule(CronScheduleBuilder.cronSchedule(cronModelService.getCron())) .build(); return trigger; } }
2.创建Job类
@Component public class Job extends QuartzJobBean { Logger logger = LoggerFactory.getLogger(Job.class); @Autowired CronModelService cronModelService; @Autowired private Scheduler scheduler; @Override protected void executeInternal(JobExecutionContext jobExecutionContext) { logger.info("执行"); Trigger trigger = jobExecutionContext.getTrigger(); String cron = cronModelService.getCron(); try { CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(trigger.getKey()); String cronExpression = cronTrigger.getCronExpression(); if (!cron.equals(cronExpression)) { //创建表达式调度器 CronScheduleBuilder cronSchedule = CronScheduleBuilder.cronSchedule(cron); //重构 cronTrigger = cronTrigger.getTriggerBuilder() .withIdentity(trigger.getKey()) .withSchedule(cronSchedule) .build(); scheduler.rescheduleJob(trigger.getKey(), cronTrigger); } } catch (SchedulerException e) { e.printStackTrace(); } } }
总结
和之前引入quartz包(参考Quartz文档)不同之处在于,之前是要先定义好Job
和Trigger
,然后通过Scheduler
去绑定他们,最后去执行start来开启定时任务。
而spring-boot-starter-quartz
则省去了Job
和Trigger
的手动绑定(参考链接: SpringBoot官方文档),也就是做好2.1配置即可。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
精彩评论