1、Quartz基本用法
参见官方说明:[Quartz Job Scheduler]
三大核心元素:
- Job:定义要执行的任务
- triggers:任务触发策略
- scheduler:关联job和trigger
1.1 调度器scheduler
scheduler的初始化和启动
// 工厂方式创建调度器实例Scheduler Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); ... scheduler.scheduleJob(job, trigger); //将任务与触发器关联起来 // 启动,开始调度 scheduler.start();
1.2 任务Jobs
实现Job,其中包含一个.execute()
方法
import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.impl.StdSchedulerFactory; import static org.quartz.JobBuilder.*; import static org.quartz.TriggerBuilder.*; import static org.quartz.SimpleScheduleBuilder.*; public class MyJob implements org.quartz.Job { public MyJob() { }
// Quartz调度运行任务时,即执行execute()方法 public void execute(JobExecutionContext context) throws JobExecutionException { System.err.println("Hello World! MyJob is executing."); } }
1.3 触发器triggers
通过trigger触发任务执行(triggers定义了job在什么时候运行。)
// 定义job,并与MyJob绑定。 JobDetail job = newJob(MyJob.class) .withIdentity("job1", "group1") .build(); // Trigger 触发job立即执行,并每隔40s重复一次。 Trigger trigger = newTrigger() .withIdentity("trigger1", "group1") .startNow() .withSchedule(simpleSchedule() .withIntervalInSeconds(40) .repeatForever()) .build(); // 调度器用于将任务与触发器关联起来。(任务和触发器可以是多对多的关系,) scheduler.scheduleJob(job, trigger);
2、 再举例
2.1 调度的任务JOB
(与上述Job类一致)
package com.mypractice.quartz; import java.text.SimpleDateFormat; import java.time.format.DateTimeFormatterBuilder; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloJob implements Job{ public void execute(JobExecutionContext context) throws JobExecutionException { SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dsc = context.getJobDetail().getDescription(); String name = context.getJobDetail().getJobDataMap().getString("name"); System.out.println(time.format(new java.util.Date()) +" hello, " + name + ". " + dsc + " "); } }
2.2 可以添加trigger的监听(实现TriggerListener接口)
trigger的配置在上一部分已做描述,此处是添加了触发器的监听。
以下示例中,在触发执行完后做了打印输出。
package com.mypractice.quartz; import org.quartz.JobExecutionContext; import org.quartz.Trigger; import org.quartz.Trigger.CompletedExecutionInstruction; import org.quartz.TriggerListener; public class myJobTriggerListener implements TriggerListener { public String getName() { // TODO Auto-generated method stub return "user_defined_Trigger"; } public void triggerFired(Trigger trigger, JobExecutionContext context) { // TODO Auto-generated method stub } public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) { // TODO Auto-generated method stub return false; } public void triggerMisfired(Trigger trigger) { // TODO Auto-generated method stub } public void triggerComplete(Trigger trigger, JobExecutionContext context, CompletedExecutionInstruction triggerInstructionCode) { String name = context.getJobDetail().getJobDataMap().getString("name"); String dsc = context.getJobDetail().getDescription(); System.out.println("compelte " + name + " " +dsc); } }
2.3 整体测试代码
package com.mypractice.quartz; import org.quartz.CronScheduleBuilder; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.JobListener; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class TestQuartzMain { public static void main(String[] args) throws SchedulerException { //scheduler SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler sch = schedulerFactory.getScheduler(); //1. Job //JobDetail jobDetail = new JobDetail("jobDetail-s1", "jobDetailGroup-s1", HelloJob.class);
//(qurtz1.*版本之前是以上用法) JobDetail jobDetail = JobBuilder.newJob(HelloJob.class) .withIdentity("helloJob", "groupJob")//given name and group to identify the JobDetail. .usingJobData("name", "孙悟空")//Add the given key-value pair to the JobDetail's .withDescription("齐天大圣") .build(); //2. trigger Trigger trigger1 = TriggerBuilder.newTrigger() .withIdentity("cron trigger", "groupTrigger") .withSchedule( //每5秒执行一次 CronScheduleBuilder.cronSchedule("0/5 * * ? * *") ).build(); // (附加) 加入triggerlistener。 sch.getListenerManager().addTriggerListener(new myJobTriggerListener()); //3. 把job、trigger注册到任务调度中 sch.scheduleJob(jobDetail, trigger1); //4. 启动调度 sch.start(); } }
输出
2017-05-25 01:05:00 hello, 孙悟空. 齐天大圣 compelte 孙悟空 齐天大圣 2017-05-25 01:05:05 hello, 孙悟空. 齐天大圣 compelte 孙悟空 齐天大圣 2017-05-25 01:05:10 hello, 孙悟空. 齐天大圣
...