quartz---的SimpleTrigger
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.SimpleTrigger; 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 System.out.println("Current Time is:"+sf.format(date)); 22 // 创建一个jobDatail实例,将该实力与helloJob绑定 23 JobDetail detail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob") 24 .build(); 25 26 // 创建一个Trigger实例,4秒后执行第一次,并且在每隔2秒重复执行 27 date.setTime(date.getTime()+4000L); 28 SimpleTrigger trigger = (SimpleTrigger)TriggerBuilder 29 .newTrigger() 30 .withIdentity("myTrigger", "group1") 31 .startAt(date) 32 .withSchedule(SimpleScheduleBuilder 33 .simpleSchedule() 34 .withIntervalInSeconds(2) 35 //无限执行:SimpleTrigger.REPEAT_INDEFINITELY) 36 .withRepeatCount(3)) 37 .build(); 38 // 创建Scheduler实例 39 SchedulerFactory factory = new StdSchedulerFactory(); 40 Scheduler scheduler = factory.getScheduler(); 41 scheduler.start(); 42 // 把jobDetail和Trigger与scheuler绑定在一起:触发启动 43 scheduler.scheduleJob(detail, trigger); 44 } 45 }
1 package com.imooc.demo.helloQuartz; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 import org.quartz.Job; 7 import org.quartz.JobExecutionContext; 8 import org.quartz.JobExecutionException; 9 10 public class HelloJob implements Job { 11 12 @Override 13 public void execute(JobExecutionContext context) throws JobExecutionException { 14 // 打印当前执行的时间,格式为2017-05-06 00:00:00 15 Date date = new Date(); 16 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 17 System.out.println("Current Exec Time Is:" + sf.format(date)); 18 System.out.println("Hello World"); 19 20 } 21 }
quartz:
SimpleTrigger://每隔几秒无限执行执行或执行几次
startAt(time);withRepeatValInSeconds(2秒)WithRepeatCount(3次)
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.SimpleTrigger; 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 System.out.println("Current Time is:"+sf.format(date)); 22 // 创建一个jobDatail实例,将该实力与helloJob绑定 23 JobDetail detail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob") 24 .build(); 25 26 //获取秒之后的时间 27 Date endDate=new Date(); 28 endDate.setTime(endDate.getTime()+6000); 29 // 创建一个Trigger实例,4秒后执行第一次,并且在每隔2秒重复执行 30 date.setTime(date.getTime()+4000L); 31 //endTime可以覆盖执行次数 32 SimpleTrigger trigger = (SimpleTrigger)TriggerBuilder 33 .newTrigger() 34 .withIdentity("myTrigger", "group1") 35 .startAt(date) 36 .endAt(endDate) 37 .withSchedule(SimpleScheduleBuilder 38 .simpleSchedule() 39 .withIntervalInSeconds(2) 40 //无限执行:SimpleTrigger.REPEAT_INDEFINITELY) 41 .withRepeatCount(3)) 42 .build(); 43 // 创建Scheduler实例 44 SchedulerFactory factory = new StdSchedulerFactory(); 45 Scheduler scheduler = factory.getScheduler(); 46 scheduler.start(); 47 // 把jobDetail和Trigger与scheuler绑定在一起:触发启动 48 scheduler.scheduleJob(detail, trigger); 49 } 50 }
endTime可以覆盖执行次数