springboot整合Quartz实现动态配置定时任务
2018-08-05 20:42
1316 查看
大家可以关注一下公众号“Java架构师秘籍” 纯干货绿色天然无污染
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
server.port=7902
是否生成ddl语句
spring.jpa.generate-ddl=false
是否打印sql语句
spring.jpa.show-sql=true
自动生成ddl,由于指定了具体的ddl,此处设置为none
spring.jpa.hibernate.ddl-auto=none
使用H2数据库
spring.datasource.platform=h2
指定生成数据库的schema文件位置
spring.datasource.schema=classpath:schema.sql
指定插入数据库语句的脚本位置
spring.datasource.data=classpath:data.sql
配置日志打印信息
logging.level.root=INFO
logging.level.org.hibernate=INFO
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.hibernate.type.descriptor.sql.BasicExtractor=TRACE
logging.level.com.itmuch=DEBUG
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Config {[url=mailto:br/>@Id
@Id
private Long id;
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
@Configuration
@Component // 此注解必加
@EnableScheduling // 此注解必加
public class ScheduleTask {
private static final Logger LOGGER = LoggerFactory.getLogger(ScheduleTask.class);
public void sayHello(){
LOGGER.info("Hello world, i'm the king of the world!!!");
}
}
package com.chhliu.springboot.quartz.entity;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
@Configuration
public class QuartzConfigration {
/**
attention:
Details:配置定时任务
/
@Bean(name = "jobDetail")
public MethodInvokingJobDetailFactoryBean detailFactoryBean(ScheduleTask task) {// ScheduleTask为需要执行的任务
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
/
是否并发执行
例如每5s执行一次任务,但是当前任务还没有执行完,就已经过了5s了,
如果此处为true,则下一个任务会执行,如果此处为false,则下一个任务会等待上一个任务执行完后,再开始执行
*/
jobDetail.setConcurrent(false);
jobDetail.setName("srd-chhliu");// 设置任务的名字
jobDetail.setGroup("srd");// 设置任务的分组,这些属性都可以存储在数据库中,在多任务的时候使用
/*
为需要执行的实体类对应的对象
*/
jobDetail.setTargetObject(task);
/*
sayHello为需要执行的方法
通过这几个配置,告诉JobDetailFactoryBean我们需要执行定时执行ScheduleTask类中的sayHello方法
*/
jobDetail.setTargetMethod("sayHello");
return jobDetail;
}
/**
attention:
Details:配置定时任务的触发器,也就是什么时候触发执行定时任务
/
@Bean(name = "jobTrigger")
public CronTriggerFactoryBean cronJobTrigger(MethodInvokingJobDetailFactoryBean jobDetail) {
CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
tigger.setJobDetail(jobDetail.getObject());
tigger.setCronExpression("0 30 20 * ?");// 初始时的cron表达式
tigger.setName("srd-chhliu");// trigger的name
return tigger;
}
/**
attention:
Details:定义quartz调度工厂
*/
@Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactory(Trigger cronJobTrigger) {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
// 用于quartz集群,QuartzScheduler 启动时更新己存在的Job
bean.setOverwriteExistingJobs(true);
// 延时启动,应用启动1秒后
bean.setStartupDelay(1);
// 注册触发器
bean.setTriggers(cronJobTrigger);
return bean;
}
}
import javax.annotation.Resource;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.chhliu.springboot.quartz.repository.ConfigRepository;
@Configuration[url=mailto:br/>@EnableScheduling
@EnableScheduling
public class ScheduleRefreshDatabase {[url=mailto:br/>@Autowired
@Autowired
* ?'); # 每2分钟执行一次定时任务
2、schema.sql
drop table config if exists;
create table config(
id bigint generated by default as identity,
cron varchar(40),
primary key(id)
);
0 30 20 ?
0 0/2 * ?
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!!!
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!!!
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!!!
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!!!
虽然上面的解决方案没有使用Quartz推荐的方式完美,但基本上可以满足我们的需求,当然也可以采用触发事件的方式来实现,例如当前端修改定时任务的触发时间时,异步的向后台发送通知,后台收到通知后,然后再更新程序,也可以实现动态的定时任务刷新
欢迎大家加Q群:230419550 学习交流讨论架构师进阶知识
前言
在我们日常的开发中,很多时候,定时任务都不是写死的,而是写到数据库中,从而实现定时任务的动态配置,下面就通过一个简单的示例,来实现这个功能。一、新建一个springboot工程,并添加依赖
<dependency><groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency><!-- 为了方便测试,此处使用了内存数据库 --> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> <exclusions> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency><!-- 该依赖必加,里面有sping对schedule的支持 --> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency>
二、配置文件application.properties
服务器端口号server.port=7902
是否生成ddl语句
spring.jpa.generate-ddl=false
是否打印sql语句
spring.jpa.show-sql=true
自动生成ddl,由于指定了具体的ddl,此处设置为none
spring.jpa.hibernate.ddl-auto=none
使用H2数据库
spring.datasource.platform=h2
指定生成数据库的schema文件位置
spring.datasource.schema=classpath:schema.sql
指定插入数据库语句的脚本位置
spring.datasource.data=classpath:data.sql
配置日志打印信息
logging.level.root=INFO
logging.level.org.hibernate=INFO
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.hibernate.type.descriptor.sql.BasicExtractor=TRACE
logging.level.com.itmuch=DEBUG
三、Entity类
package com.chhliu.springboot.quartz.entity;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Config {[url=mailto:br/>@Id
@Id
private Long id;
@Column private String cron; /** * @return the id */ public Long getId() { return id; } ……此处省略getter和setter方法……
}
四、任务类
package com.chhliu.springboot.quartz.entity;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
@Configuration
@Component // 此注解必加
@EnableScheduling // 此注解必加
public class ScheduleTask {
private static final Logger LOGGER = LoggerFactory.getLogger(ScheduleTask.class);
public void sayHello(){
LOGGER.info("Hello world, i'm the king of the world!!!");
}
}
五、Quartz配置类
由于springboot追求零xml配置,所以下面会以配置Bean的方式来实现package com.chhliu.springboot.quartz.entity;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
@Configuration
public class QuartzConfigration {
/**
attention:
Details:配置定时任务
/
@Bean(name = "jobDetail")
public MethodInvokingJobDetailFactoryBean detailFactoryBean(ScheduleTask task) {// ScheduleTask为需要执行的任务
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
/
是否并发执行
例如每5s执行一次任务,但是当前任务还没有执行完,就已经过了5s了,
如果此处为true,则下一个任务会执行,如果此处为false,则下一个任务会等待上一个任务执行完后,再开始执行
*/
jobDetail.setConcurrent(false);
jobDetail.setName("srd-chhliu");// 设置任务的名字
jobDetail.setGroup("srd");// 设置任务的分组,这些属性都可以存储在数据库中,在多任务的时候使用
/*
为需要执行的实体类对应的对象
*/
jobDetail.setTargetObject(task);
/*
sayHello为需要执行的方法
通过这几个配置,告诉JobDetailFactoryBean我们需要执行定时执行ScheduleTask类中的sayHello方法
*/
jobDetail.setTargetMethod("sayHello");
return jobDetail;
}
/**
attention:
Details:配置定时任务的触发器,也就是什么时候触发执行定时任务
/
@Bean(name = "jobTrigger")
public CronTriggerFactoryBean cronJobTrigger(MethodInvokingJobDetailFactoryBean jobDetail) {
CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
tigger.setJobDetail(jobDetail.getObject());
tigger.setCronExpression("0 30 20 * ?");// 初始时的cron表达式
tigger.setName("srd-chhliu");// trigger的name
return tigger;
}
/**
attention:
Details:定义quartz调度工厂
*/
@Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactory(Trigger cronJobTrigger) {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
// 用于quartz集群,QuartzScheduler 启动时更新己存在的Job
bean.setOverwriteExistingJobs(true);
// 延时启动,应用启动1秒后
bean.setStartupDelay(1);
// 注册触发器
bean.setTriggers(cronJobTrigger);
return bean;
}
}
六、定时查库,并更新任务
package com.chhliu.springboot.quartz.entity;import javax.annotation.Resource;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.chhliu.springboot.quartz.repository.ConfigRepository;
@Configuration[url=mailto:br/>@EnableScheduling
@EnableScheduling
public class ScheduleRefreshDatabase {[url=mailto:br/>@Autowired
@Autowired
* ?'); # 每2分钟执行一次定时任务
2、schema.sql
drop table config if exists;
create table config(
id bigint generated by default as identity,
cron varchar(40),
primary key(id)
);
八、运行测试
测试结果如下:(Quartz默认的线程池大小为10)0 30 20 ?
0 0/2 * ?
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!!!
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!!!
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!!!
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推荐的方式完美,但基本上可以满足我们的需求,当然也可以采用触发事件的方式来实现,例如当前端修改定时任务的触发时间时,异步的向后台发送通知,后台收到通知后,然后再更新程序,也可以实现动态的定时任务刷新
欢迎大家加Q群:230419550 学习交流讨论架构师进阶知识
相关文章推荐
- springboot整合Quartz实现动态配置定时任务
- springboot整合Quartz实现动态配置定时任务
- springboot整合Quartz实现动态配置定时任务
- spring boot 整合 quartz 集群环境 实现 动态定时任务配置【原】
- springboot整合Quartz实现动态配置定时任务的方法
- Springboot整合Quartz实现动态配置定时任务
- springboot整合Quartz实现动态配置定时任务
- springboot整合Quartz实现动态配置定时任务
- springboot动态配置定时任务2种方式,整合Quartz多线程并发执行多个定时任务配置
- Spring-boot整合Quartz,3、动态配置定时任务
- spring boot 集成quartz 2.0 实现前端动态配置(获取spring上下文)的两种方式,启动数据库中已开启定时任务
- [置顶] spring整合quartz实现动态定时任务的前台网页配置与管理
- springboot动态配置定时任务2种方式,整合Quartz多线程并发执行多个定时任务配置
- Spring 3整合Quartz 2实现定时任务三:动态暂停 恢复 修改和删除任务
- Spring 3整合Quartz 2实现定时任务三:动态暂停 恢复 修改和删除任务
- Spring 3整合Quartz 2实现定时任务二:动态添加任务
- Spring 整合 Quartz 实现动态定时任务
- Spring动态对Quartz定时任务的管理,实现动态加载,停止的配置实例代码
- Spring 3整合Quartz 2实现定时任务二:动态添加任务
- Spring3整合Quartz2实现定时任务及动态任务调整(添加删除暂停恢复)--推荐