前言
quartz是Java编写的一款开源的任务调度开发框架,在项目开发中很多场景都可以用到,比如订单超期自动收货。
所谓程序源于生活,生活中也有很多场景可以用quartz来模拟,比如工作日早上七点起床,再比如,每天八杯水,
隔一小时一杯,在这里我就根据这两个例子来给大家初步介绍quartz。
粗糙类图+流程介绍
1.简要画一下quartz大致结构,画的不好,还望见谅:)
2.使用quartz开发,首先得知道自己的需要去完成的任务,quartz提供一个Job接口,只要实现它的execute()方法即可,然后创建调度工厂拿到调度实例,
设置好触发器,将任务和触发器加入到调度中即可,具体实现请参考后续代码。
编码实现
开发环境
- gradle 4.1
- quartz 2.2.1
- jdk1.7
新建gradle项目
group 'com.haq.quartz' version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.7 repositories { mavenCentral() } dependencies { compile group: 'org.quartz-scheduler', name: 'quartz', version: '2.2.1' compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21' compile group: 'ch.qos.logback', name: 'logback-core', version: '1.1.7' compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.7' testCompile group: 'junit', name: 'junit', version: '4.12' }
创建quartz配置文件quartz.properties
1 #============================================================================ 2 # Scheduler configration 3 #============================================================================ 4 org.quartz.scheduler.instanceName = MyScheduler 5 org.quartz.scheduler.instanceId = AUTO 6 7 org.quartz.scheduler.skipUpdateCheck: true 8 9 #============================================================================ 10 # jobstore configration 11 #============================================================================ 12 org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore 13 #============================================================== 14 # threadPool configuration 15 #============================================================== 16 #org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool 17 org.quartz.threadPool.threadCount = 10 18 #org.quartz.threadPool.threadPriority = 5 19 #org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
新建任务类
分别创建MorningJob和DrinkJob
1 package com.haq.quartz.job; 2 3 import org.quartz.Job; 4 import org.quartz.JobExecutionContext; 5 import org.quartz.JobExecutionException; 6 import org.slf4j.Logger; 7 import org.slf4j.LoggerFactory; 8 9 import java.util.Date; 10 11 public class DrinkJob implements Job { 12 13 private static final Logger log = LoggerFactory.getLogger(DrinkJob.class); 14 15 @Override 16 public void execute(JobExecutionContext context) throws JobExecutionException { 17 log.info(new Date() + " hey! it is time to drink some water!"); 18 } 19 20 }
1 package com.haq.quartz.job; 2 3 import org.quartz.Job; 4 import org.quartz.JobExecutionContext; 5 import org.quartz.JobExecutionException; 6 import org.quartz.JobKey; 7 import org.slf4j.Logger; 8 import org.slf4j.LoggerFactory; 9 10 import java.util.Date; 11 12 public class MorningJob implements Job { 13 14 private static final Logger log = LoggerFactory.getLogger(MorningJob.class); 15 16 @Override 17 public void execute(JobExecutionContext context) throws JobExecutionException { 18 log.info(new Date() + " Morning , it is time to get up!"); 19 } 20 }
创建调度类 TestSchedule
1 package com.haq.quartz.schedule; 2 3 import com.haq.quartz.job.DrinkJob; 4 import com.haq.quartz.job.MorningJob; 5 import org.quartz.*; 6 import org.quartz.impl.StdSchedulerFactory; 7 8 9 public class TestSchedule { 10 public static void main(String[] args) throws SchedulerException, InterruptedException { 11 SchedulerFactory sf = new StdSchedulerFactory(); 12 13 Scheduler scheduler = sf.getScheduler(); 14 JobDetail drinkjob = JobBuilder.newJob(DrinkJob.class) 15 .withIdentity("drinkjob", "group1") 16 .build(); 17 Trigger drinktrgger = TriggerBuilder.newTrigger() 18 .withIdentity("drinktrgger", "group1") 19 .startNow() 20 .withSchedule(SimpleScheduleBuilder.simpleSchedule() 21 // .withIntervalInHours(1) 为方便测试,改为5秒执行一次 22 .withIntervalInSeconds(5) 23 .withRepeatCount(8)) 24 .build(); 25 26 27 28 JobDetail morningjob = JobBuilder.newJob(MorningJob.class) 29 .withIdentity("morningjob", "group1") 30 .build(); 31 Trigger morningtrgger = TriggerBuilder.newTrigger() 32 .withIdentity("morningtrgger", "group1") 33 .startNow() 34 .withSchedule(DailyTimeIntervalScheduleBuilder.dailyTimeIntervalSchedule() 35 // .onMondayThroughFriday() 为方便测试将时间调整为当前时间 36 .onSaturdayAndSunday() 37 .startingDailyAt(new TimeOfDay(12,35,0)) 38 .withRepeatCount(5)) 39 .build(); 40 41 42 scheduler.scheduleJob(drinkjob, drinktrgger); 43 scheduler.scheduleJob(morningjob, morningtrgger); 44 45 scheduler.start(); 46 47 Thread.sleep(90L * 1000L); 48 49 scheduler.shutdown(); 50 } 51 }
运行结果如下:
源码下载:
https://github.com/xiaodongio/quartz_learning/tree/day1