1 17 18 package org.quartz.examples.example7; 19 20 import java.util.Date ; 21 22 import org.quartz.JobDetail; 23 import org.quartz.Scheduler; 24 import org.quartz.SchedulerFactory; 25 import org.quartz.SchedulerMetaData; 26 import org.quartz.SimpleTrigger; 27 import org.quartz.TriggerUtils; 28 import org.quartz.impl.StdSchedulerFactory; 29 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.commons.logging.Log; 32 33 56 public class InterruptExample { 57 58 public void run() throws Exception { 59 final Log log = LogFactory.getLog(InterruptExample.class); 60 61 log.info("------- Initializing ----------------------"); 62 63 SchedulerFactory sf = new StdSchedulerFactory(); 65 Scheduler sched = sf.getScheduler(); 66 67 log.info("------- Initialization Complete -----------"); 68 69 log.info("------- Scheduling Jobs -------------------"); 70 71 long ts = TriggerUtils.getNextGivenSecondDate(null, 15).getTime(); 73 74 JobDetail job = new JobDetail("interruptableJob1", "group1", 75 DumbInterruptableJob.class); 76 SimpleTrigger trigger = 77 new SimpleTrigger("trigger1", "group1", 78 new Date (ts), 79 null, 80 SimpleTrigger.REPEAT_INDEFINITELY, 81 5000L); 82 Date ft = sched.scheduleJob(job, trigger); 83 log.info(job.getFullName() + " will run at: " + ft + " and repeat: " 84 + trigger.getRepeatCount() + " times, every " 85 + trigger.getRepeatInterval() / 1000 + " seconds"); 86 87 sched.start(); 90 log.info("------- Started Scheduler -----------------"); 91 92 93 log.info("------- Starting loop to interrupt job every 7 seconds ----------"); 94 for(int i=0; i < 50; i++) { 95 try { 96 Thread.sleep(7000L); 97 sched.interrupt(job.getName(), job.getGroup()); 99 } catch (Exception e) { 100 } 101 } 102 103 log.info("------- Shutting Down ---------------------"); 104 105 sched.shutdown(true); 106 107 log.info("------- Shutdown Complete -----------------"); 108 SchedulerMetaData metaData = sched.getMetaData(); 109 log.info("Executed " + metaData.numJobsExecuted() + " jobs."); 110 111 } 112 113 public static void main(String [] args) throws Exception { 114 115 InterruptExample example = new InterruptExample(); 116 example.run(); 117 } 118 119 } | Popular Tags |