1 17 18 package org.quartz.examples.example13; 19 20 import java.util.Date ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 import org.quartz.JobDetail; 26 import org.quartz.Scheduler; 27 import org.quartz.SchedulerFactory; 28 import org.quartz.SimpleTrigger; 29 import org.quartz.impl.StdSchedulerFactory; 30 31 72 public class ClusterExample { 73 74 private static Log _log = LogFactory.getLog(ClusterExample.class); 75 76 public void cleanUp(Scheduler inScheduler) throws Exception { 77 _log.warn("***** Deleting existing jobs/triggers *****"); 78 79 String [] groups = inScheduler.getTriggerGroupNames(); 81 for (int i = 0; i < groups.length; i++) { 82 String [] names = inScheduler.getTriggerNames(groups[i]); 83 for (int j = 0; j < names.length; j++) { 84 inScheduler.unscheduleJob(names[j], groups[i]); 85 } 86 } 87 88 groups = inScheduler.getJobGroupNames(); 90 for (int i = 0; i < groups.length; i++) { 91 String [] names = inScheduler.getJobNames(groups[i]); 92 for (int j = 0; j < names.length; j++) { 93 inScheduler.deleteJob(names[j], groups[i]); 94 } 95 } 96 } 97 98 public void run(boolean inClearJobs, boolean inScheduleJobs) 99 throws Exception { 100 101 SchedulerFactory sf = new StdSchedulerFactory(); 103 Scheduler sched = sf.getScheduler(); 104 105 if (inClearJobs) { 106 cleanUp(sched); 107 } 108 109 _log.info("------- Initialization Complete -----------"); 110 111 if (inScheduleJobs) { 112 113 _log.info("------- Scheduling Jobs ------------------"); 114 115 String schedId = sched.getSchedulerInstanceId(); 116 117 int count = 1; 118 119 JobDetail job = new JobDetail("job_" + count, schedId, 120 SimpleRecoveryJob.class); 121 job.setRequestsRecovery(true); 124 SimpleTrigger trigger = 125 new SimpleTrigger("triger_" + count, schedId, 20, 5000L); 126 trigger.setStartTime(new Date (System.currentTimeMillis() + 1000L)); 127 _log.info(job.getFullName() + 128 " will run at: " + trigger.getNextFireTime() + 129 " and repeat: " + trigger.getRepeatCount() + 130 " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); 131 sched.scheduleJob(job, trigger); 132 133 count++; 134 job = new JobDetail("job_" + count, schedId, 135 SimpleRecoveryJob.class); 136 job.setRequestsRecovery(true); 139 trigger = new SimpleTrigger("trig_" + count, schedId, 20, 5000L); 140 trigger.setStartTime(new Date (System.currentTimeMillis() + 2000L)); 141 _log.info(job.getFullName() + 142 " will run at: " + trigger.getNextFireTime() + 143 " and repeat: " + trigger.getRepeatCount() + 144 " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); 145 sched.scheduleJob(job, trigger); 146 147 count++; 148 job = new JobDetail("job_" + count, schedId, 149 SimpleRecoveryStatefulJob.class); 150 job.setRequestsRecovery(true); 153 trigger = new SimpleTrigger("trig_" + count, schedId, 20, 3000L); 154 trigger.setStartTime(new Date (System.currentTimeMillis() + 1000L)); 155 _log.info(job.getFullName() + 156 " will run at: " + trigger.getNextFireTime() + 157 " and repeat: " + trigger.getRepeatCount() + 158 " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); 159 sched.scheduleJob(job, trigger); 160 161 count++; 162 job = new JobDetail("job_" + count, schedId, SimpleRecoveryJob.class); 163 job.setRequestsRecovery(true); 166 trigger = new SimpleTrigger("trig_" + count, schedId, 20, 4000L); 167 trigger.setStartTime(new Date (System.currentTimeMillis() + 1000L)); 168 _log.info(job.getFullName() + " will run at: " 169 + trigger.getNextFireTime() + " & repeat: " 170 + trigger.getRepeatCount() + "/" 171 + trigger.getRepeatInterval()); 172 sched.scheduleJob(job, trigger); 173 174 count++; 175 job = new JobDetail("job_" + count, schedId, SimpleRecoveryJob.class); 176 job.setRequestsRecovery(true); 179 trigger = new SimpleTrigger("trig_" + count, schedId, 20, 4500L); 180 trigger.setStartTime(new Date (System.currentTimeMillis() + 1000L)); 181 _log.info(job.getFullName() + " will run at: " 182 + trigger.getNextFireTime() + " & repeat: " 183 + trigger.getRepeatCount() + "/" 184 + trigger.getRepeatInterval()); 185 sched.scheduleJob(job, trigger); 186 } 187 188 _log.info("------- Starting Scheduler ---------------"); 190 sched.start(); 191 _log.info("------- Started Scheduler ----------------"); 192 193 _log.info("------- Waiting for one hour... ----------"); 194 try { 195 Thread.sleep(3600L * 1000L); 196 } catch (Exception e) { 197 } 198 199 _log.info("------- Shutting Down --------------------"); 200 sched.shutdown(); 201 _log.info("------- Shutdown Complete ----------------"); 202 } 203 204 public static void main(String [] args) throws Exception { 205 boolean clearJobs = false; 206 boolean scheduleJobs = true; 207 208 for (int i = 0; i < args.length; i++) { 209 if (args[i].equalsIgnoreCase("clearJobs")) { 210 clearJobs = true; 211 } else if (args[i].equalsIgnoreCase("dontScheduleJobs")) { 212 scheduleJobs = false; 213 } 214 } 215 216 ClusterExample example = new ClusterExample(); 217 example.run(clearJobs, scheduleJobs); 218 } 219 } 220 221 | Popular Tags |