1 17 18 package org.quartz.examples.example4; 19 20 import java.util.Date ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.quartz.JobDetail; 25 import org.quartz.Scheduler; 26 import org.quartz.SchedulerFactory; 27 import org.quartz.SchedulerMetaData; 28 import org.quartz.SimpleTrigger; 29 import org.quartz.TriggerUtils; 30 import org.quartz.impl.StdSchedulerFactory; 31 32 38 public class JobStateExample { 39 40 public void run() throws Exception { 41 Log log = LogFactory.getLog(JobStateExample.class); 42 43 log.info("------- Initializing -------------------"); 44 45 SchedulerFactory sf = new StdSchedulerFactory(); 47 Scheduler sched = sf.getScheduler(); 48 49 log.info("------- Initialization Complete --------"); 50 51 log.info("------- Scheduling Jobs ----------------"); 52 53 long ts = TriggerUtils.getNextGivenSecondDate(null, 10).getTime(); 55 56 JobDetail job1 = new JobDetail("job1", "group1", ColorJob.class); 58 SimpleTrigger trigger1 = new SimpleTrigger("trigger1", "group1", "job1", "group1", 59 new Date (ts), null, 4, 10000); 60 job1.getJobDataMap().put(ColorJob.FAVORITE_COLOR, "Green"); 62 job1.getJobDataMap().put(ColorJob.EXECUTION_COUNT, 1); 63 64 Date scheduleTime1 = sched.scheduleJob(job1, trigger1); 66 log.info(job1.getFullName() + 67 " will run at: " + scheduleTime1 + 68 " and repeat: " + trigger1.getRepeatCount() + 69 " times, every " + trigger1.getRepeatInterval() / 1000 + " seconds"); 70 71 JobDetail job2 = new JobDetail("job2", "group1", ColorJob.class); 73 SimpleTrigger trigger2 = new SimpleTrigger("trigger2", "group1", "job2", "group1", 74 new Date (ts + 1000), null, 4, 10000); 75 job2.getJobDataMap().put(ColorJob.FAVORITE_COLOR, "Red"); 78 job2.getJobDataMap().put(ColorJob.EXECUTION_COUNT, 1); 79 80 Date scheduleTime2 = sched.scheduleJob(job2, trigger2); 82 log.info(job1.getFullName() + 83 " will run at: " + scheduleTime1 + 84 " and repeat: " + trigger1.getRepeatCount() + 85 " times, every " + trigger1.getRepeatInterval() / 1000 + " seconds"); 86 87 88 89 log.info("------- Starting Scheduler ----------------"); 90 91 sched.start(); 94 95 log.info("------- Started Scheduler -----------------"); 96 97 log.info("------- Waiting 60 seconds... -------------"); 98 try { 99 Thread.sleep(60L * 1000L); 101 } catch (Exception e) { 103 } 104 105 log.info("------- Shutting Down ---------------------"); 106 107 sched.shutdown(true); 108 109 log.info("------- Shutdown Complete -----------------"); 110 111 SchedulerMetaData metaData = sched.getMetaData(); 112 log.info("Executed " + metaData.numJobsExecuted() + " jobs."); 113 114 } 115 116 public static void main(String [] args) throws Exception { 117 118 JobStateExample example = new JobStateExample(); 119 example.run(); 120 } 121 122 } | Popular Tags |