quartz:Trigger:触发job时间和结束时间
1 package com.imooc.demo.helloQuartz; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 import org.quartz.JobBuilder; 7 import org.quartz.JobDetail; 8 import org.quartz.Scheduler; 9 import org.quartz.SchedulerException; 10 import org.quartz.SchedulerFactory; 11 import org.quartz.SimpleScheduleBuilder; 12 import org.quartz.Trigger; 13 import org.quartz.TriggerBuilder; 14 import org.quartz.impl.StdSchedulerFactory; 15 16 public class HelloScheduler { 17 18 public static void main(String[] args) throws SchedulerException { 19 Date date = new Date(); 20 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 21 //获取距离当前时间3秒后的时间 22 date.setTime(date.getTime()+3000); 23 //获取距离当前时间6秒后的时间 24 Date endTime=new Date(); 25 endTime.setTime(endTime.getTime()+6000); 26 // 创建一个jobDatail实例,将该实力与helloJob绑定 27 JobDetail detail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob") 28 .build(); 29 30 // 创建一个Trigger实例,定义该job立即执行,并且在每隔2秒重复执行 31 Trigger trigger = TriggerBuilder 32 .newTrigger() 33 .withIdentity("myTrigger", "group1") 34 .startAt(date) 35 .endAt(endTime) 36 .withSchedule( 37 SimpleScheduleBuilder.simpleSchedule() 38 .withIntervalInSeconds(2).repeatForever()) 39 .build(); 40 // 创建Scheduler实例 41 SchedulerFactory factory = new StdSchedulerFactory(); 42 Scheduler scheduler = factory.getScheduler(); 43 scheduler.start(); 44 45 System.out.println("Current Exec Time Is:" + sf.format(date) + "-----"); 46 // 把jobDetail和Trigger与scheuler绑定在一起:触发启动 47 scheduler.scheduleJob(detail, trigger); 48 } 49 }
startAt();
endAt();