1 17 18 package org.quartz.examples.example3; 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.CronTrigger; 25 import org.quartz.JobDetail; 26 import org.quartz.Scheduler; 27 import org.quartz.SchedulerFactory; 28 import org.quartz.SchedulerMetaData; 29 import org.quartz.impl.StdSchedulerFactory; 30 31 37 public class CronTriggerExample { 38 39 40 public void run() throws Exception { 41 Log log = LogFactory.getLog(CronTriggerExample.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 55 JobDetail job = new JobDetail("job1", "group1", SimpleJob.class); 57 CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", 58 "group1", "0/20 * * * * ?"); 59 sched.addJob(job, true); 60 Date ft = sched.scheduleJob(trigger); 61 log.info(job.getFullName() + " has been scheduled to run at: " + ft 62 + " and repeat based on expression: " 63 + trigger.getCronExpression()); 64 65 job = new JobDetail("job2", "group1", SimpleJob.class); 67 trigger = new CronTrigger("trigger2", "group1", "job2", "group1", 68 "15 0/2 * * * ?"); 69 sched.addJob(job, true); 70 ft = sched.scheduleJob(trigger); 71 log.info(job.getFullName() + " has been scheduled to run at: " + ft 72 + " and repeat based on expression: " 73 + trigger.getCronExpression()); 74 75 job = new JobDetail("job3", "group1", SimpleJob.class); 77 trigger = new CronTrigger("trigger3", "group1", "job3", "group1", 78 "0 0/2 8-17 * * ?"); 79 sched.addJob(job, true); 80 ft = sched.scheduleJob(trigger); 81 log.info(job.getFullName() + " has been scheduled to run at: " + ft 82 + " and repeat based on expression: " 83 + trigger.getCronExpression()); 84 85 job = new JobDetail("job4", "group1", SimpleJob.class); 87 trigger = new CronTrigger("trigger4", "group1", "job4", "group1", 88 "0 0/3 17-23 * * ?"); 89 sched.addJob(job, true); 90 ft = sched.scheduleJob(trigger); 91 log.info(job.getFullName() + " has been scheduled to run at: " + ft 92 + " and repeat based on expression: " 93 + trigger.getCronExpression()); 94 95 job = new JobDetail("job5", "group1", SimpleJob.class); 97 trigger = new CronTrigger("trigger5", "group1", "job5", "group1", 98 "0 0 10am 1,15 * ?"); 99 sched.addJob(job, true); 100 ft = sched.scheduleJob(trigger); 101 log.info(job.getFullName() + " has been scheduled to run at: " + ft 102 + " and repeat based on expression: " 103 + trigger.getCronExpression()); 104 105 job = new JobDetail("job6", "group1", SimpleJob.class); 108 trigger = new CronTrigger("trigger6", "group1", "job6", "group1", 109 "0,30 * * ? * MON-FRI"); 110 sched.addJob(job, true); 111 ft = sched.scheduleJob(trigger); 112 log.info(job.getFullName() + " has been scheduled to run at: " + ft 113 + " and repeat based on expression: " 114 + trigger.getCronExpression()); 115 116 job = new JobDetail("job7", "group1", SimpleJob.class); 119 trigger = new CronTrigger("trigger7", "group1", "job7", "group1", 120 "0,30 * * ? * SAT,SUN"); 121 sched.addJob(job, true); 122 ft = sched.scheduleJob(trigger); 123 log.info(job.getFullName() + " has been scheduled to run at: " + ft 124 + " and repeat based on expression: " 125 + trigger.getCronExpression()); 126 127 log.info("------- Starting Scheduler ----------------"); 128 129 sched.start(); 133 134 log.info("------- Started Scheduler -----------------"); 135 136 log.info("------- Waiting five minutes... ------------"); 137 try { 138 Thread.sleep(300L * 1000L); 140 } catch (Exception e) { 142 } 143 144 log.info("------- Shutting Down ---------------------"); 145 146 sched.shutdown(true); 147 148 log.info("------- Shutdown Complete -----------------"); 149 150 SchedulerMetaData metaData = sched.getMetaData(); 151 log.info("Executed " + metaData.numJobsExecuted() + " jobs."); 152 153 } 154 155 public static void main(String [] args) throws Exception { 156 157 CronTriggerExample example = new CronTriggerExample(); 158 example.run(); 159 } 160 161 } | Popular Tags |