Quartz常规操作
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11899532.html
Project Directory
Maven Dependency
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>org.fool.quartz</groupId> 8 <artifactId>helloquartz</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <dependencies> 12 <dependency> 13 <groupId>org.quartz-scheduler</groupId> 14 <artifactId>quartz</artifactId> 15 <version>2.3.2</version> 16 </dependency> 17 18 <dependency> 19 <groupId>org.quartz-scheduler</groupId> 20 <artifactId>quartz-jobs</artifactId> 21 <version>2.3.2</version> 22 </dependency> 23 24 <dependency> 25 <groupId>org.projectlombok</groupId> 26 <artifactId>lombok</artifactId> 27 <version>1.18.10</version> 28 </dependency> 29 30 <dependency> 31 <groupId>org.slf4j</groupId> 32 <artifactId>slf4j-log4j12</artifactId> 33 <version>1.7.29</version> 34 </dependency> 35 </dependencies> 36 37 <build> 38 <plugins> 39 <plugin> 40 <groupId>org.apache.maven.plugins</groupId> 41 <artifactId>maven-compiler-plugin</artifactId> 42 <version>3.8.1</version> 43 <configuration> 44 <source>1.8</source> 45 <target>1.8</target> 46 </configuration> 47 </plugin> 48 </plugins> 49 </build> 50 </project>
log4j.properties
1 log4j.rootLogger=info,stdout 2 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 3 log4j.appender.stdout.Target=System.out 4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n
无状态Job
HelloJob.java
1 package org.fool.quartz.job; 2 3 import lombok.extern.slf4j.Slf4j; 4 import org.quartz.Job; 5 import org.quartz.JobExecutionContext; 6 import org.quartz.JobExecutionException; 7 8 import java.util.Date; 9 10 @Slf4j 11 public class HelloJob implements Job { 12 @Override 13 public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { 14 log.info("job name: {}", jobExecutionContext.getJobDetail().getKey().getName()); 15 log.info("job group: {}", jobExecutionContext.getJobDetail().getKey().getGroup()); 16 log.info("job class detail name: {}", jobExecutionContext.getJobInstance().getClass().getName()); 17 log.info("job class simple name: {}", jobExecutionContext.getJobInstance().getClass().getSimpleName()); 18 19 log.info("trigger name: {}", jobExecutionContext.getTrigger().getKey().getName()); 20 log.info("trigger group: {}", jobExecutionContext.getTrigger().getKey().getGroup()); 21 22 String jobDetailMessage = jobExecutionContext.getJobDetail().getJobDataMap().getString("message"); 23 String triggerMessage = jobExecutionContext.getTrigger().getJobDataMap().getString("message"); 24 25 log.info("current job detail message: {}", jobDetailMessage); 26 log.info("current trigger message: {}", triggerMessage); 27 28 log.info("current datetime: {}", new Date()); 29 log.info("current job fire time: {}", jobExecutionContext.getFireTime()); 30 log.info("next job fire time: {}", jobExecutionContext.getNextFireTime()); 31 } 32 }
QuartzJobTest.java
1 package org.fool.quartz.job; 2 3 import lombok.extern.slf4j.Slf4j; 4 import org.quartz.JobBuilder; 5 import org.quartz.JobDetail; 6 import org.quartz.Scheduler; 7 import org.quartz.SimpleScheduleBuilder; 8 import org.quartz.Trigger; 9 import org.quartz.TriggerBuilder; 10 import org.quartz.impl.StdSchedulerFactory; 11 12 @Slf4j 13 public class QuartzJobTest { 14 public static void main(String[] args) throws Exception { 15 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); 16 17 JobDetail jobDetail = JobBuilder.newJob(HelloJob.class) 18 .withIdentity("job1", "job group1") 19 .usingJobData("message", "HelloMyJob") 20 .build(); 21 22 Trigger trigger = TriggerBuilder.newTrigger() 23 .withIdentity("trigger1", "trigger group1") 24 .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(5)) 25 .usingJobData("message", "HelloMyTrigger") 26 .startNow() 27 .build(); 28 29 scheduler.scheduleJob(jobDetail, trigger); 30 scheduler.start(); 31 } 32 }
有状态Job
HelloPersistJob.java
1 package org.fool.quartz.job; 2 3 import lombok.extern.slf4j.Slf4j; 4 import org.quartz.Job; 5 import org.quartz.JobExecutionContext; 6 import org.quartz.JobExecutionException; 7 import org.quartz.PersistJobDataAfterExecution; 8 9 @PersistJobDataAfterExecution 10 @Slf4j 11 public class HelloPersistJob implements Job { 12 13 private String message; 14 private Integer count; 15 16 public void setMessage(String message) { 17 this.message = message; 18 } 19 20 public void setCount(Integer count) { 21 this.count = count; 22 } 23 24 @Override 25 public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { 26 // if the key name is the same between trigger and jobDetail, the trigger name will override the jobDetail name 27 // if you want to fetch both trigger name and jobDetail name, please define different jobData key name 28 log.info("current name: {}", message); 29 30 count++; 31 jobExecutionContext.getJobDetail().getJobDataMap().put("count", count); 32 33 log.info("job execution count: {}", count); 34 } 35 }
Note: 有状态job需要在job类头上加上@PersistJobDataAfterExecution注解
QuartzPersistJobTest.java
1 package org.fool.quartz.job; 2 3 import lombok.extern.slf4j.Slf4j; 4 import org.quartz.JobBuilder; 5 import org.quartz.JobDetail; 6 import org.quartz.Scheduler; 7 import org.quartz.SimpleScheduleBuilder; 8 import org.quartz.Trigger; 9 import org.quartz.TriggerBuilder; 10 import org.quartz.impl.StdSchedulerFactory; 11 12 @Slf4j 13 public class QuartzPersistJobTest { 14 public static void main(String[] args) throws Exception { 15 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); 16 17 JobDetail jobDetail = JobBuilder.newJob(HelloPersistJob.class) 18 .withIdentity("job1", "job group1") 19 .usingJobData("message", "HelloMyJob") 20 .usingJobData("count", 0) 21 .build(); 22 23 Trigger trigger = TriggerBuilder.newTrigger() 24 .withIdentity("trigger1", "trigger group1") 25 .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(5)) 26 .usingJobData("message", "HelloMyTrigger") 27 .startNow() 28 .build(); 29 30 scheduler.scheduleJob(jobDetail, trigger); 31 scheduler.start(); 32 } 33 }
SimpleSchedule
SimpleJob.java
1 package org.fool.quartz.job; 2 3 import lombok.extern.slf4j.Slf4j; 4 import org.quartz.Job; 5 import org.quartz.JobExecutionContext; 6 import org.quartz.JobExecutionException; 7 8 import java.util.Date; 9 10 @Slf4j 11 public class SimpleJob implements Job { 12 @Override 13 public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { 14 log.info("current datetime: {}", new Date()); 15 } 16 }
QuartzSimpleJobTest.java
1 package org.fool.quartz.job; 2 3 import org.quartz.JobBuilder; 4 import org.quartz.JobDetail; 5 import org.quartz.Scheduler; 6 import org.quartz.SimpleScheduleBuilder; 7 import org.quartz.Trigger; 8 import org.quartz.TriggerBuilder; 9 import org.quartz.impl.StdSchedulerFactory; 10 11 import java.util.Date; 12 13 public class QuartzSimpleJobTest { 14 public static void main(String[] args) throws Exception { 15 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); 16 17 JobDetail jobDetail = JobBuilder.newJob(SimpleJob.class) 18 .withIdentity("job1", "job group1") 19 .build(); 20 21 Trigger trigger = TriggerBuilder.newTrigger() 22 .withIdentity("trigger1", "trigger group1") 23 .withSchedule(SimpleScheduleBuilder.repeatSecondlyForTotalCount(3, 8)) 24 .startAt(new Date(System.currentTimeMillis() + 5000)) 25 .endAt(new Date(System.currentTimeMillis() + 10000)) 26 .build(); 27 28 scheduler.scheduleJob(jobDetail, trigger); 29 scheduler.start(); 30 } 31 }
CronSchedule
CronJob.java
1 package org.fool.quartz.job; 2 3 import lombok.extern.slf4j.Slf4j; 4 import org.quartz.Job; 5 import org.quartz.JobExecutionContext; 6 import org.quartz.JobExecutionException; 7 8 import java.util.Date; 9 10 @Slf4j 11 public class CronJob implements Job { 12 @Override 13 public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { 14 log.info("current datetime: {}", new Date()); 15 } 16 }
QuartzCronJobTest.java
1 package org.fool.quartz.job; 2 3 import org.quartz.CronScheduleBuilder; 4 import org.quartz.JobBuilder; 5 import org.quartz.JobDetail; 6 import org.quartz.Scheduler; 7 import org.quartz.Trigger; 8 import org.quartz.TriggerBuilder; 9 import org.quartz.impl.StdSchedulerFactory; 10 11 public class QuartzCronJobTest { 12 public static void main(String[] args) throws Exception { 13 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); 14 15 JobDetail jobDetail = JobBuilder.newJob(CronJob.class) 16 .withIdentity("job1", "job group1") 17 .build(); 18 19 Trigger trigger = TriggerBuilder.newTrigger() 20 .withIdentity("trigger1", "trigger group1") 21 .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) 22 .startNow() 23 .build(); 24 25 scheduler.scheduleJob(jobDetail, trigger); 26 scheduler.start(); 27 } 28 }
Reference
http://www.quartz-scheduler.org/
强者自救 圣者渡人