package schedule; import java.text.ParseException; import java.util.Calendar; import java.util.Date; import org.quartz.CronExpression; import org.quartz.CronTrigger; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.impl.JobDetailImpl; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.triggers.CronTriggerImpl; import org.quartz.impl.triggers.SimpleTriggerImpl; public class TriggerTest { public static void main(String[] args) throws SchedulerException, ParseException, InterruptedException { testSimpleTrigger(); //testCronTrigger(); } private static void testSimpleTrigger() throws SchedulerException, InterruptedException{ JobDetailImpl jobDetail = new JobDetailImpl(); jobDetail.setGroup("myGroup"); jobDetail.setJobClass(MyJob.class); jobDetail.setName("jobDetail"); SimpleTriggerImpl trigger = new SimpleTriggerImpl(); trigger.setStartTime(new Date()); trigger.setName("myTrigger"); trigger.setRepeatInterval(1000); trigger.setRepeatCount(1); /*job在endTime时不再执行,但scheduler并未shutdown * Date c = new Date(); c.setTime(System.currentTimeMillis()+2000L); trigger.setEndTime(c);*/ Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); scheduler.shutdown(true); } }
执行时发现MyJob一次也没执行,scheduler就shutDown了。后来在网上看到一篇回答
http://stackoverflow.com/questions/15928980/quartz-scheduler-shuts-down-before-job-execution
答主提到了这些:I think you‘re misunderstanding the purpose of scheduler.shutdown(true);
It will wait for executing jobs to finish, but it will NOT wait for scheduled jobs to start and finish. Your job is not starting before you shutdown the scheduler.You could put a Thread.sleep(wait);
before you shut it down.
原来是shutDown(true)只是等待scheduler执行完,而不是等等job完成。试着按答主的方法加了,即
package schedule; import java.text.ParseException; import java.util.Calendar; import java.util.Date; import org.quartz.CronExpression; import org.quartz.CronTrigger; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.impl.JobDetailImpl; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.triggers.CronTriggerImpl; import org.quartz.impl.triggers.SimpleTriggerImpl; public class TriggerTest { public static void main(String[] args) throws SchedulerException, ParseException, InterruptedException { testSimpleTrigger(); //testCronTrigger(); } private static void testSimpleTrigger() throws SchedulerException, InterruptedException{ JobDetailImpl jobDetail = new JobDetailImpl(); jobDetail.setGroup("myGroup"); jobDetail.setJobClass(MyJob.class); jobDetail.setName("jobDetail"); SimpleTriggerImpl trigger = new SimpleTriggerImpl(); trigger.setStartTime(new Date()); trigger.setName("myTrigger"); trigger.setRepeatInterval(1000); trigger.setRepeatCount(1); /*job在endTime时不再执行,但scheduler并未shutdown * Date c = new Date(); c.setTime(System.currentTimeMillis()+2000L); trigger.setEndTime(c);*/ Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); Thread.sleep(2000);//不加这一句,会在schedule触发时即shutdown,而不是job执行完shutdown scheduler.shutdown(true); } }
给job留够执行完成的时间,则在job重复执行一次后,scheduler关闭。
时间: 2024-11-05 14:46:27