springboot整合Quartz实现动态配置定时任务

前言

在我们日常的开发中,很多时候,定时任务都不是写死的,而是写到数据库中,从而实现定时任务的动态配置,下面就通过一个简单的示例,来实现这个功能。

一、新建一个springboot工程,并添加依赖

 

[html]  view plain  copy
 
  1. <dependency>  
  2.             <groupId>org.springframework.boot</groupId>  
  3.             <artifactId>spring-boot-starter-data-jpa</artifactId>  
  4.         </dependency>  
  5.   
  6.         <dependency><!-- 为了方便测试,此处使用了内存数据库 -->  
  7.             <groupId>com.h2database</groupId>  
  8.             <artifactId>h2</artifactId>  
  9.             <scope>runtime</scope>  
  10.         </dependency>  
  11.         <dependency>  
  12.             <groupId>org.springframework.boot</groupId>  
  13.             <artifactId>spring-boot-starter-test</artifactId>  
  14.             <scope>test</scope>  
  15.         </dependency>  
  16.           
  17.         <dependency>  
  18.             <groupId>org.quartz-scheduler</groupId>  
  19.             <artifactId>quartz</artifactId>  
  20.             <version>2.2.1</version>  
  21.             <exclusions>  
  22.                 <exclusion>  
  23.                     <artifactId>slf4j-api</artifactId>  
  24.                     <groupId>org.slf4j</groupId>  
  25.                 </exclusion>  
  26.             </exclusions>  
  27.         </dependency>  
  28.         <dependency><!-- 该依赖必加,里面有sping对schedule的支持 -->  
  29.                        <groupId>org.springframework</groupId>  
  30.                        <artifactId>spring-context-support</artifactId>  
  31.         </dependency>  

二、配置文件application.properties

 

 

[java]  view plain  copy
 
  1. # 服务器端口号    
  2. server.port=7902  
  3. # 是否生成ddl语句    
  4. spring.jpa.generate-ddl=false    
  5. # 是否打印sql语句    
  6. spring.jpa.show-sql=true    
  7. # 自动生成ddl,由于指定了具体的ddl,此处设置为none    
  8. spring.jpa.hibernate.ddl-auto=none    
  9. # 使用H2数据库    
  10. spring.datasource.platform=h2    
  11. # 指定生成数据库的schema文件位置    
  12. spring.datasource.schema=classpath:schema.sql    
  13. # 指定插入数据库语句的脚本位置    
  14. spring.datasource.data=classpath:data.sql    
  15. # 配置日志打印信息    
  16. logging.level.root=INFO    
  17. logging.level.org.hibernate=INFO    
  18. logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE    
  19. logging.level.org.hibernate.type.descriptor.sql.BasicExtractor=TRACE    
  20. logging.level.com.itmuch=DEBUG   

三、Entity类

 

 

[java]  view plain  copy
 
  1. package com.chhliu.springboot.quartz.entity;  
  2.   
  3. import javax.persistence.Column;  
  4. import javax.persistence.Entity;  
  5. import javax.persistence.GeneratedValue;  
  6. import javax.persistence.GenerationType;  
  7. import javax.persistence.Id;  
  8.   
  9. @Entity  
  10. public class Config {  
  11.     @Id  
  12.       @GeneratedValue(strategy = GenerationType.AUTO)  
  13.       private Long id;  
  14.   
  15.       @Column  
  16.       private String cron;  
  17.   
  18.     /** 
  19.      * @return the id 
  20.      */  
  21.     public Long getId() {  
  22.         return id;  
  23.     }  
  24.         ……此处省略getter和setter方法……  
  25. }  

四、任务类

 

 

[java]  view plain  copy
 
  1. package com.chhliu.springboot.quartz.entity;  
  2.   
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.context.annotation.Configuration;  
  6. import org.springframework.scheduling.annotation.EnableScheduling;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. @Configuration  
  10. @Component // 此注解必加  
  11. @EnableScheduling // 此注解必加  
  12. public class ScheduleTask {  
  13.     private static final Logger LOGGER =  LoggerFactory.getLogger(ScheduleTask.class);  
  14.     public void sayHello(){  
  15.         LOGGER.info("Hello world, i'm the king of the world!!!");  
  16.     }  
  17. }  

五、Quartz配置类

 

由于springboot追求零xml配置,所以下面会以配置Bean的方式来实现

 

[java]  view plain  copy
 
  1. package com.chhliu.springboot.quartz.entity;  
  2.   
  3. import org.quartz.Trigger;  
  4. import org.springframework.context.annotation.Bean;  
  5. import org.springframework.context.annotation.Configuration;  
  6. import org.springframework.scheduling.quartz.CronTriggerFactoryBean;  
  7. import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;  
  8. import org.springframework.scheduling.quartz.SchedulerFactoryBean;  
  9.   
  10. @Configuration  
  11. public class QuartzConfigration {  
  12.     /** 
  13.      * attention: 
  14.      * Details:配置定时任务 
  15.      */  
  16.     @Bean(name = "jobDetail")  
  17.     public MethodInvokingJobDetailFactoryBean detailFactoryBean(ScheduleTask task) {// ScheduleTask为需要执行的任务  
  18.         MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();  
  19.         /* 
  20.          *  是否并发执行 
  21.          *  例如每5s执行一次任务,但是当前任务还没有执行完,就已经过了5s了, 
  22.          *  如果此处为true,则下一个任务会执行,如果此处为false,则下一个任务会等待上一个任务执行完后,再开始执行 
  23.          */  
  24.         jobDetail.setConcurrent(false);  
  25.           
  26.         jobDetail.setName("srd-chhliu");// 设置任务的名字  
  27.         jobDetail.setGroup("srd");// 设置任务的分组,这些属性都可以存储在数据库中,在多任务的时候使用  
  28.           
  29.         /* 
  30.          * 为需要执行的实体类对应的对象 
  31.          */  
  32.         jobDetail.setTargetObject(task);  
  33.           
  34.         /* 
  35.          * sayHello为需要执行的方法 
  36.          * 通过这几个配置,告诉JobDetailFactoryBean我们需要执行定时执行ScheduleTask类中的sayHello方法 
  37.          */  
  38.         jobDetail.setTargetMethod("sayHello");  
  39.         return jobDetail;  
  40.     }  
  41.       
  42.     /** 
  43.      * attention: 
  44.      * Details:配置定时任务的触发器,也就是什么时候触发执行定时任务 
  45.      */  
  46.     @Bean(name = "jobTrigger")  
  47.     public CronTriggerFactoryBean cronJobTrigger(MethodInvokingJobDetailFactoryBean jobDetail) {  
  48.         CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();  
  49.         tigger.setJobDetail(jobDetail.getObject());  
  50.         tigger.setCronExpression("0 30 20 * * ?");// 初始时的cron表达式  
  51.         tigger.setName("srd-chhliu");// trigger的name  
  52.         return tigger;  
  53.   
  54.     }  
  55.   
  56.     /** 
  57.      * attention: 
  58.      * Details:定义quartz调度工厂 
  59.      */  
  60.     @Bean(name = "scheduler")  
  61.     public SchedulerFactoryBean schedulerFactory(Trigger cronJobTrigger) {  
  62.         SchedulerFactoryBean bean = new SchedulerFactoryBean();  
  63.         // 用于quartz集群,QuartzScheduler 启动时更新己存在的Job  
  64.         bean.setOverwriteExistingJobs(true);  
  65.         // 延时启动,应用启动1秒后  
  66.         bean.setStartupDelay(1);  
  67.         // 注册触发器  
  68.         bean.setTriggers(cronJobTrigger);  
  69.         return bean;  
  70.     }  
  71. }  

六、定时查库,并更新任务

 

 

[java]  view plain  copy
 
  1. package com.chhliu.springboot.quartz.entity;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.quartz.CronScheduleBuilder;  
  6. import org.quartz.CronTrigger;  
  7. import org.quartz.JobDetail;  
  8. import org.quartz.Scheduler;  
  9. import org.quartz.SchedulerException;  
  10. import org.springframework.beans.factory.annotation.Autowired;  
  11. import org.springframework.context.annotation.Configuration;  
  12. import org.springframework.scheduling.annotation.EnableScheduling;  
  13. import org.springframework.scheduling.annotation.Scheduled;  
  14. import org.springframework.stereotype.Component;  
  15.   
  16. import com.chhliu.springboot.quartz.repository.ConfigRepository;  
  17.   
  18. @Configuration  
  19. @EnableScheduling  
  20. @Component  
  21. public class ScheduleRefreshDatabase {  
  22.     @Autowired  
  23.     private ConfigRepository repository;  
  24.   
  25.     @Resource(name = "jobDetail")  
  26.     private JobDetail jobDetail;  
  27.   
  28.     @Resource(name = "jobTrigger")  
  29.     private CronTrigger cronTrigger;  
  30.   
  31.     @Resource(name = "scheduler")  
  32.     private Scheduler scheduler;  
  33.   
  34.     @Scheduled(fixedRate = 5000) // 每隔5s查库,并根据查询结果决定是否重新设置定时任务  
  35.     public void scheduleUpdateCronTrigger() throws SchedulerException {  
  36.         CronTrigger trigger = (CronTrigger) scheduler.getTrigger(cronTrigger.getKey());  
  37.         String currentCron = trigger.getCronExpression();// 当前Trigger使用的  
  38.         String searchCron = repository.findOne(1L).getCron();// 从数据库查询出来的  
  39.         System.out.println(currentCron);  
  40.         System.out.println(searchCron);  
  41.         if (currentCron.equals(searchCron)) {  
  42.             // 如果当前使用的cron表达式和从数据库中查询出来的cron表达式一致,则不刷新任务  
  43.         } else {  
  44.             // 表达式调度构建器  
  45.             CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(searchCron);  
  46.             // 按新的cronExpression表达式重新构建trigger  
  47.             trigger = (CronTrigger) scheduler.getTrigger(cronTrigger.getKey());  
  48.             trigger = trigger.getTriggerBuilder().withIdentity(cronTrigger.getKey())  
  49.                     .withSchedule(scheduleBuilder).build();  
  50.             // 按新的trigger重新设置job执行  
  51.             scheduler.rescheduleJob(cronTrigger.getKey(), trigger);  
  52.             currentCron = searchCron;  
  53.         }  
  54.     }  
  55. }  

六、相关脚本

 

1、data.sql

 

[sql]  view plain  copy
 
  1. insert into config(id,cron) values(1,'0 0/2 * * * ?'); # 每2分钟执行一次定时任务  

2、schema.sql

 

 

[sql]  view plain  copy
 
  1. drop table config if exists;  
  2. create table config(  
  3.     id bigint generated by default as identity,  
  4.     cron varchar(40),  
  5.     primary key(id)  
  6. );  

六、运行测试

 

测试结果如下:(Quartz默认的线程池大小为10)

 

[java]  view plain  copy
 
  1. 0 30 20 * * ?  
  2. 0 0/2 * * * ?  
  3. 2017-03-08 18:02:00.025  INFO 5328 --- [eduler_Worker-1] c.c.s.quartz.entity.ScheduleTask         : Hello world, i'm the king of the world!!!  
  4. 2017-03-08 18:04:00.003  INFO 5328 --- [eduler_Worker-2] c.c.s.quartz.entity.ScheduleTask         : Hello world, i'm the king of the world!!!  
  5. 2017-03-08 18:06:00.002  INFO 5328 --- [eduler_Worker-3] c.c.s.quartz.entity.ScheduleTask         : Hello world, i'm the king of the world!!!  
  6. 2017-03-08 18:08:00.002  INFO 5328 --- [eduler_Worker-4] c.c.s.quartz.entity.ScheduleTask         : Hello world, i'm the king of the world!!!  

从上面的日志打印时间来看,我们实现了动态配置,最初的时候,任务是每天20:30执行,后面通过动态刷新变成了每隔2分钟执行一次。

 

虽然上面的解决方案没有使用Quartz推荐的方式完美,但基本上可以满足我们的需求,当然也可以采用触发事件的方式来实现,例如当前端修改定时任务的触发时间时,异步的向后台发送通知,后台收到通知后,然后再更新程序,也可以实现动态的定时任务刷新

猜你喜欢

转载自201504092844.iteye.com/blog/2398946