Quartz -第一篇-入门
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency>
创建一个实例实现Job接口,实现其中的execute方法,将你要做的任务放到此方法
package quartz.job; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import javax.xml.crypto.Data; import java.text.SimpleDateFormat; import java.util.Date; /** * ClassName: HelloJob<br/> * Description: <br/> * date: 2019/1/22 11:19 AM<br/> * * @author chengluchao * @since JDK 1.8 */ public class HelloJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Current Exec Time Is:" + simpleDateFormat.format(date)); System.out.println("Hello word!"); } }
编写调用类
package quartz.job; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SimpleScheduleBuilder; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; /** * ClassName: HelloScheduler<br/> * Description: <br/> * date: 2019/1/22 11:23 AM<br/> * * @author chengluchao * @since JDK 1.8 */ public class HelloScheduler { public static void main(String[] args) throws Exception { //创建一个JobDetail实例,将HelloJob.class绑定 JobDetail jobDetail = JobBuilder .newJob(HelloJob.class) .withIdentity("myJob", "group1") .build(); //创建一个Trigger实例,定义该job立即执行,并且每隔两秒钟重复执行一次,直到永远 Trigger trigger = TriggerBuilder .newTrigger() .withIdentity("myTrigger", "group1") .startNow()//立即执行 .withSchedule( SimpleScheduleBuilder .simpleSchedule() .withIntervalInSeconds(2)//两秒一次 .repeatForever())//直到永远 .build(); //创建Schduler实例 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); scheduler.start(); scheduler.scheduleJob(jobDetail, trigger); } }
14:56:17.296 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.myJob', class="quartz".job.HelloJob
14:56:17.296 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
14:56:17.296 [DefaultQuartzScheduler_Worker-10] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.myJob
Current Exec Time Is:2019-01-22 14:56:17
Hello word!
14:56:19.295 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.myJob', class="quartz".job.HelloJob
14:56:19.295 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
14:56:19.295 [DefaultQuartzScheduler_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.myJob
Current Exec Time Is:2019-01-22 14:56:19
Hello word!
14:56:21.296 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.myJob', class="quartz".job.HelloJob
14:56:21.296 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
14:56:21.296 [DefaultQuartzScheduler_Worker-2] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.myJob
Current Exec Time Is:2019-01-22 14:56:21
Hello word!
补充:
System.out.println("jobDetail's name : " + jobDetail.getKey().getName()); System.out.println("jobDetail's gruop : " + jobDetail.getKey().getGroup()); System.out.println("jobDetail's jobClass : " + jobDetail.getKey().getClass());
jobDataMap的使用:
public class HelloScheduler { public static void main(String[] args) throws Exception { //创建一个JobDetail实例,将HelloJob.class绑定 JobDetail jobDetail = JobBuilder .newJob(HelloJob.class) .withIdentity("myJob") .usingJobData("message","hello myJob1") .usingJobData("floatJobvalue",3.14f) .build(); //创建一个Trigger实例,定义该job立即执行,并且每隔两秒钟重复执行一次,直到永远 Trigger trigger = TriggerBuilder .newTrigger() .withIdentity("myTrigger", "group1") .usingJobData("message","hello myTrigger1") .usingJobData("doubleJobvalue",885.02) .startNow()//立即执行 .withSchedule( SimpleScheduleBuilder .simpleSchedule() .withIntervalInSeconds(2)//两秒一次 .repeatForever())//直到永远 .build(); //创建Schduler实例 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); scheduler.start(); scheduler.scheduleJob(jobDetail, trigger); } }
public class HelloJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Current Exec Time Is:" + simpleDateFormat.format(date)); System.out.println("Hello word!"); JobKey key = jobExecutionContext.getJobDetail().getKey(); System.out.println("JobDetailKey : " + key.getName() + "JobDetailValue : " + key.getGroup()); TriggerKey triggerKey1 = jobExecutionContext.getTrigger().getKey(); System.out.println("triggerKey1 : " + triggerKey1.getName() + "triggervalue1 : " + triggerKey1.getGroup()); JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap(); JobDataMap tdataMap = jobExecutionContext.getTrigger().getJobDataMap(); System.out.println(dataMap.get("message")); System.out.println(tdataMap.get("message")); JobDataMap dataMap2 = jobExecutionContext.getMergedJobDataMap(); //Trigger会覆盖JobDetail的信息 System.out.println(dataMap2.get("message")); System.out.println(dataMap2.get("message")); }
获取jobDataMap的值的方式2:
在HelloJob类中定义属性,属性名和map中的key一样,加上get/set方法
在HelloJob的类中就可以直接使用
Trigger的方法:
.startAt(date)//首次执行的时间
.endAt(endDate)//最后一次执行的时间
.startNow()//立即执行