一、简介
Quartz是一个功能丰富的开源作业调度库,几乎可以集成到任何Java应用程序中——从最小的独立应用程序到最大的电子商务系统。Quartz可以用来创建简单或复杂的时间表,以执行数十个、数百个甚至数万个工作;这些作业的任务被定义为标准Java组件,可以执行您通过编程让它们执行的几乎任何事情。Quartz调度器包括许多企业级特性,比如对JTA事务和集群的支持。
二、应用场景
- 定时任务(通过cron表达式)
- 订单管理(状态管理)
- 系统维护(数据库备份...)
- 提醒服务
- ...
三、简单应用
1. 基本配置
- 引入jar包
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency>
- resources目录下添加quartz.properties文件
org.quartz.scheduler.instanceName = MyScheduler org.quartz.threadPool.threadCount = 3 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
2. 简单应用
- java代码
public static void main(String[] args) { try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.start(); scheduler.shutdown(); }catch (Exception e){ e.printStackTrace(); } SpringApplication.run(DockerImagesApplication.class, args); }
- 执行结果
2020-12-26 19:54:22.882 INFO 4904 --- [ main] org.quartz.impl.StdSchedulerFactory : Using default implementation for ThreadExecutor 2020-12-26 19:54:22.892 INFO 4904 --- [ main] org.quartz.core.SchedulerSignalerImpl : Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl 2020-12-26 19:54:22.893 INFO 4904 --- [ main] org.quartz.core.QuartzScheduler : Quartz Scheduler v.2.2.1 created. 2020-12-26 19:54:22.894 INFO 4904 --- [ main] org.quartz.simpl.RAMJobStore : RAMJobStore initialized. 2020-12-26 19:54:22.895 INFO 4904 --- [ main] org.quartz.core.QuartzScheduler : Scheduler meta-data: Quartz Scheduler (v2.2.1) 'MyScheduler' with instanceId 'NON_CLUSTERED' Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally. NOT STARTED. Currently in standby mode. Number of jobs executed: 0 Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 3 threads. Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. 2020-12-26 19:54:22.895 INFO 4904 --- [ main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler 'MyScheduler' initialized from default resource file in Quartz package: 'quartz.properties' 2020-12-26 19:54:22.895 INFO 4904 --- [ main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler version: 2.2.1 2020-12-26 19:54:22.896 INFO 4904 --- [ main] org.quartz.core.QuartzScheduler : Scheduler MyScheduler_$_NON_CLUSTERED started. 2020-12-26 19:54:22.896 INFO 4904 --- [ main] org.quartz.core.QuartzScheduler : Scheduler MyScheduler_$_NON_CLUSTERED shutting down. 2020-12-26 19:54:22.896 INFO 4904 --- [ main] org.quartz.core.QuartzScheduler : Scheduler MyScheduler_$_NON_CLUSTERED paused. 2020-12-26 19:54:22.896 INFO 4904 --- [ main] org.quartz.core.QuartzScheduler : Scheduler MyScheduler_$_NON_CLUSTERED shutdown complete.
3. 立即执行job
- 简单job实现
import lombok.extern.slf4j.Slf4j; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; @Slf4j public class HelloJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("hello world!!!"); } }
- job调用
public static void main(String[] args) { try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("test1", "repeat").build(); jobDetail.getJobDataMap().put("name","ljl"); Trigger build = newTrigger().withIdentity("test1", "repeat").startNow().build(); build.getJobDataMap().put("name","lsr"); scheduler.scheduleJob(jobDetail,build); scheduler.start(); }catch (Exception e){ e.printStackTrace(); } SpringApplication.run(DockerImagesApplication.class, args); }
- 执行结果
23:33:31.110 [main] INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor 23:33:31.123 [main] INFO org.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl 23:33:31.124 [main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.2.1 created. 23:33:31.125 [main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized. 23:33:31.126 [main] INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.2.1) 'QuartzScheduler' with instanceId 'NON_CLUSTERED' Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally. NOT STARTED. Currently in standby mode. Number of jobs executed: 0 Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 3 threads. Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. 23:33:31.126 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'QuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties' 23:33:31.126 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.2.1 23:33:31.131 [main] INFO org.quartz.core.QuartzScheduler - Scheduler QuartzScheduler_$_NON_CLUSTERED started. 23:33:31.132 [QuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers 23:33:31.134 [QuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'repeat.test1', class=com.example.docker_images.job.HelloJob 23:33:31.139 [QuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers 23:33:31.139 [QuartzScheduler_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job repeat.test1 ljl lsr hello world!!!
4. 未来执行一次
- job简单实现
import lombok.extern.slf4j.Slf4j; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; @Slf4j public class HelloJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { //通过jobDetail获取数据 String name = context.getJobDetail().getJobDataMap().getString("name"); //通过trigger获取传参 String nameByTrigger = context.getTrigger().getJobDataMap().getString("name"); System.out.println(name); System.out.println(nameByTrigger); System.out.println("hello world!!!"); } }
- job调用
public static void main(String[] args) { try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); //利用jobDetail传参 JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("test1", "repeat").build(); jobDetail.getJobDataMap().put("name","ljl"); //利用trigger传参 Trigger build = newTrigger().withIdentity("test1", "repeat").startAt(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2020-12-26 23:26:00")).build(); build.getJobDataMap().put("name","lsr"); scheduler.scheduleJob(jobDetail,build); scheduler.start(); }catch (Exception e){ e.printStackTrace(); } SpringApplication.run(DockerImagesApplication.class, args); }
- 执行结果
2020-12-26 23:25:06.309 INFO 20832 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed 2020-12-26 23:25:06.320 INFO 20832 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s) 2020-12-26 23:25:06.339 INFO 20832 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references 2020-12-26 23:25:06.442 INFO 20832 --- [ main] c.e.d.DockerImagesApplication : Started DockerImagesApplication in 5.418 seconds (JVM running for 6.339) ljl lsr hello world!!!
5. 重复执行
- 简单job实现
import lombok.extern.slf4j.Slf4j; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; @Slf4j public class HelloJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("hello world!!!"); } }
- job重复调用
public static void main(String[] args) { try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("test1", "repeat").build(); CronTrigger build = newTrigger().withIdentity("test1", "repeat").withSchedule(cronSchedule("*/5 * * * * ?")).build(); scheduler.scheduleJob(jobDetail,build); scheduler.start(); }catch (Exception e){ e.printStackTrace(); } SpringApplication.run(DockerImagesApplication.class, args); }
- 执行结果
2020-12-26 23:06:43.122 INFO 21156 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references 2020-12-26 23:06:43.209 INFO 21156 --- [ main] c.e.d.DockerImagesApplication : Started DockerImagesApplication in 4.835 seconds (JVM running for 5.715) hello world!!! hello world!!! hello world!!! hello world!!! hello world!!!