集成quartz
定时任务调度是我们常用的需求。如何开启一个任务呢。
1. 需要Scheduler,springBoot会帮我们自动注入
2. 需要一个JobDetail,定义任务的详细信息,如名称,组,描述等
3. 需要定义一个触发器Trigger,根据cron表达式确定任务执行出发时间
cron表达式
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式:
Seconds Minutes Hours DayofMonth Month DayofWeek Year
Seconds Minutes Hours DayofMonth Month DayofWeek
结构
corn从左到右(用空格隔开):秒 分 小时 月份中的日期 月份 星期中的日期 年份
字段含义列表
字段 | 允许值 | 允许的特殊字符 |
---|---|---|
秒(Seconds) | 0~59的整数 | , - * / 四个字符 |
分(Minutes) | 0~59的整数 | , - * / 四个字符 |
小时(Hours) | 0~23的整数 | , - * / 四个字符 |
日期(DayofMonth) | 1~31的整数(但是你需要考虑你月的天数) | ,- * ? / L W C 八个字符 |
月份(Month) | 1~12的整数或者 JAN-DEC | , - * / 四个字符 |
星期(DayofWeek) | 1~7的整数或者 SUN-SAT (1=SUN) | , - * ? / L C # 八个字符 |
年(可选,留空)(Year) | 1970~2099 | , - * / 四个字符 |
关于特殊字符
- *:表示匹配该域的任意值。假如在Minutes域使用*, 即表示每分钟都会触发事件。
- ?:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。因为DayofMonth和DayofWeek会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用,如果使用表示不管星期几都会触发,实际上并不是这样。
- -:表示范围。例如在Minutes域使用5-20,表示从5分到20分钟每分钟触发一次
- /:表示起始时间开始触发,然后每隔固定时间触发一次。例如在Minutes域使用5/20,则意味着5分钟触发一次,而25,45等分别触发一次.
- ,:表示列出枚举值。例如:在Minutes域使用5,20,则意味着在5和20分每分钟触发一次。
- L:表示最后,只能出现在DayofWeek和DayofMonth域。如果在DayofWeek域使用5L,意味着在最后的一个星期四触发。
- W:表示有效工作日(周一到周五),只能出现在DayofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 DayofMonth使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份 。
- LW:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。
- #:用于确定每个月第几个星期几,只能出现在DayofMonth域。例如在4#2,表示某月的第二个星期三。
常用例子
- 0 0 2 1 * ? * 表示在每月的1日的凌晨2点调整任务
- 0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业
- 0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作
- 0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
- 0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
- 0 0 12 ? * WED 表示每个星期三中午12点
- 0 0 12 * * ? 每天中午12点触发
- 0 15 10 ? * * 每天上午10:15触发
- 0 15 10 * * ? 每天上午10:15触发
- 0 15 10 * * ? * 每天上午10:15触发
- 0 15 10 * * ? 2005 2005年的每天上午10:15触发
- 0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发
- 0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发
- 0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
- 0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发
- 0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发
- 0 15 10 ? * MON-FRI 周一至周五的上午10:15触发
- 0 15 10 15 * ? 每月15日上午10:15触发
- 0 15 10 L * ? 每月最后一日的上午10:15触发
- 0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发
- 0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发
- 0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发
1.依赖和系统配置
pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
系统文件配置
# quartz任务配置
spring.quartz.job-store-type=jdbc
spring.quartz.properties.org.quartz.scheduler.instanceName=clusteredScheduler
spring.quartz.properties.org.quartz.scheduler.instanceId=AUTO
spring.quartz.properties.org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
spring.quartz.properties.org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
spring.quartz.properties.org.quartz.jobStore.tablePrefix=QRTZ_
spring.quartz.properties.org.quartz.jobStore.isClustered=true
spring.quartz.properties.org.quartz.jobStore.clusterCheckinInterval=10000
spring.quartz.properties.org.quartz.jobStore.useProperties=false
spring.quartz.properties.org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
spring.quartz.properties.org.quartz.threadPool.threadCount=10
spring.quartz.properties.org.quartz.threadPool.threadPriority=5
spring.quartz.properties.org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true
# 打开集群配置
spring.quartz.properties.org.quartz.jobStore.isClustered:true
# 设置集群检查间隔20s
spring.quartz.properties.org.quartz.jobStore.clusterCheckinInterval = 2000
2.实现Job接口,建立自己的任务
public class MyJob implements Job,Serializable {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("MyJob:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
JobDetail jobDetail = context.getJobDetail();
System.out.println("builderName:"+jobDetail.getJobBuilder().getClass().getName());
System.out.println("jobClassName:"+jobDetail.getJobClass().getName());
System.out.println("descib:"+jobDetail.getDescription());
System.out.println("keyName:"+jobDetail.getKey().getName());
System.out.println("--------------------------------------------------------");
}
3.测试
QuartzEntity entity = new QuartzEntity();
entity.setJobName("job1");
entity.setJobGroup("group1");
entity.setDescription("测试任务");
entity.setCronExpression("0/5 * * * * ?");
JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
.withIdentity(entity.getJobName(),entity.getJobGroup())
.withDescription(entity.getDescription())
.build();
CronScheduleBuilder cronSchedule = CronScheduleBuilder.cronSchedule(entity.getCronExpression());
Trigger trigger= TriggerBuilder.newTrigger().startNow()
.withIdentity("======trigger1======","test1")
.withSchedule(cronSchedule)
.build();
scheduler.scheduleJob(jobDetail,trigger);
MyJob:2018-08-28 23:18:45
builderName:org.quartz.JobBuilder
jobClassName:com.lg.iboot.quartz.job.MyJob
descib:测试任务
keyName:job1
4.Scheduler接口常用操作
- 暂停
/**
<ul><li>Pause the <code>{@link org.quartz.JobDetail}</code> with the given</li>
<li>key - by pausing all of its current <code>Trigger</code>s.</li>
<li>
<ul><li>@see #resumeJob(JobKey)
*/
void pauseJob(JobKey jobKey)
throws SchedulerException;
- 恢复
/** * Resume (un-pause) the <code>{@link org.quartz.JobDetail}</code> with * the given key. * * <p> * If any of the <code>Job</code>'s<code>Trigger</code> s missed one * or more fire-times, then the <code>Trigger</code>'s misfire * instruction will be applied. * </p> * * @see #pauseJob(JobKey) */ void resumeJob(JobKey jobKey) throws SchedulerException;
- 删除 : 需要依次调用
void pauseTrigger(TriggerKey triggerKey) throws SchedulerException; boolean unscheduleJob(TriggerKey triggerKey) throws SchedulerException; boolean deleteJob(JobKey jobKey) throws SchedulerException;
- 添加
Date scheduleJob(JobDetail jobDetail, Trigger trigger) throws SchedulerException;