quartz 总体架构
Quartz的使用
导包
1 2 3 4 5
| <!-- quartz --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException;
import java.util.Date;
public class MyJop implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.out.println("MyJob execute:" + new Date()); } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package quartz.quartz;
import org.quartz.*; import org.quartz.impl.StdSchedulerFactory;
public class TestJob { public static void main(String[] args) { JobDetail jobDetail = JobBuilder.newJob(MyJop.class) .withIdentity("jop1", "group1") .build();
Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("trigger1", "trigger1") .startNow() .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()) .build();
try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); } catch (SchedulerException e) { e.printStackTrace(); } } }
|
运行结果图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException;
/** * @author : 其然乐衣Letitbe * @date : 2022/11/27 */ public class MyJop implements Job {
/** * 如果在添加 .usingJobData("name", "trigger3") 的时候, * key 和 这里定义的属性名一样的话,就会给这里的属性赋值,下面就可以直接用了 */ private String name;
public void setName(String name) { this.name = name; }
@Override public void execute(JobExecutionContext context) throws JobExecutionException { // System.out.println("MyJob execute:" + new Date());
JobDataMap jobDetailMap = context.getJobDetail().getJobDataMap(); JobDataMap triggerMap = context.getTrigger().getJobDataMap(); // 获取 JobDetail 和 Trigger 合并,但如果存在相同的键值key,Trigger的会覆盖JobDetail的 JobDataMap mergeMap = context.getMergedJobDataMap();
System.out.println("jobDetailMap:" + jobDetailMap.getString("job")); System.out.println("triggerMap:" + triggerMap.getString("trigger")); System.out.println("——————————————————————————————————————————————————————————————————————————"); System.out.println("mergeMap:" + mergeMap.getString("job")); System.out.println("mergeMap:" + mergeMap.getString("trigger")); System.out.println("——————————————————————————————————————————————————————————————————————————"); System.out.println( "name : " + name ); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package quartz.quartz;
import org.quartz.*; import org.quartz.impl.StdSchedulerFactory;
/** * @author : 其然乐衣Letitbe * @date : 2022/11/27 */ public class TestJob { public static void main(String[] args) { JobDetail jobDetail = JobBuilder.newJob(MyJop.class) // name: 任务名称(在调度器里不能重复,唯一的) group : 组 .withIdentity("jop1", "group1") .usingJobData("job", "jobDetail1") .usingJobData("name", "jobDetail2") .build();
// 触发器 Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("trigger1", "trigger1") .usingJobData("trigger", "trigger") // 会覆盖上面JobDetail中的name的值 .usingJobData("name", "trigger2") .startNow() // 时间间隔 永久重复执行 .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()) .build();
// 调度器 try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.scheduleJob(jobDetail, trigger); // 启动 scheduler.start(); } catch (SchedulerException e) { e.printStackTrace(); } } }
|
结果
Job : 封装成JobDetail设置属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import org.quartz.*; import java.util.Date;
@DisallowConcurrentExecution public class MyJop implements Job {
private String name;
public void setName(String name) { this.name = name; }
@Override public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println( "jobDetail : " + System.identityHashCode(context.getJobDetail()) ); System.out.println( "job : " + System.identityHashCode(context.getJobInstance()) );
System.out.println("execute : " + new Date()); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } }
|
结果:
测试count++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import org.quartz.*;
import java.util.Date;
@DisallowConcurrentExecution
@PersistJobDataAfterExecution public class MyJop implements Job {
@Override public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap triggerMap = context.getTrigger().getJobDataMap(); JobDataMap jobDetailMap = context.getJobDetail().getJobDataMap(); triggerMap.put("count", triggerMap.getInt("count") + 1); jobDetailMap.put("count1", jobDetailMap.getInt("count1") + 1); System.out.println( "triggerMap count : " + triggerMap.getInt("count") ); System.out.println( "jobDetailMap count : " + jobDetailMap.getInt("count1") );
} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| import org.quartz.*; import org.quartz.impl.StdSchedulerFactory;
public class TestJob { public static void main(String[] args) { JobDetail jobDetail = JobBuilder.newJob(MyJop.class) .withIdentity("jop1", "group1") .usingJobData("job", "jobDetail1") .usingJobData("name", "jobDetail2") .usingJobData("count1", 0) .build();
int count = 0;
Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("trigger1", "trigger1") .usingJobData("trigger", "trigger") .usingJobData("name", "trigger2") .usingJobData("count", count) .startNow() .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()) .build();
try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); } catch (SchedulerException e) { e.printStackTrace(); } } }
|
结果图:
Springboot整合Quartz
Springboot整合Quartz
使用Springboot里面的监听器,让项目在启动后也启动了调度器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package quartz.bootquartz;
import org.quartz.*; import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.Date;
@DisallowConcurrentExecution
@PersistJobDataAfterExecution public class QuartzJob extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { try { Thread.sleep(2000); System.out.println(context.getScheduler().getSchedulerInstanceId());
System.out.println("taskname : " + context.getJobDetail().getKey().getName() ); System.out.println("执行时间 :" + new Date()); } catch (Exception e) { e.printStackTrace(); } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package quartz.bootquartz;
import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.impl.StdSchedulerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration public class SchedulerConfig {
@Bean public Scheduler scheduler() { Scheduler scheduler1 = null; try { scheduler1 = StdSchedulerFactory.getDefaultScheduler(); } catch (SchedulerException e) { e.printStackTrace(); } return scheduler1; }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package quartz.bootquartz.listener;
import org.quartz.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; import quartz.bootquartz.QuartzJob;
@Component public class StartApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
@Autowired private Scheduler scheduler;
@Override public void onApplicationEvent(ContextRefreshedEvent event) { TriggerKey triggerKey = TriggerKey.triggerKey("trigger1", "group1"); try { Trigger trigger = scheduler.getTrigger(triggerKey); if (trigger == null) { trigger = TriggerBuilder.newTrigger() .withIdentity(triggerKey) .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) .startNow() .build(); JobDetail jobDetail = JobBuilder.newJob(QuartzJob.class) .withIdentity("job1", "group1") .build();
scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); } } catch (SchedulerException e) { e.printStackTrace(); }
} }
|
结果: