1 64 65 package com.jcorporate.expresso.services.crontab; 66 67 import org.apache.log4j.Logger; 68 69 import java.util.Date ; 70 import java.util.Iterator ; 71 import java.util.LinkedList ; 72 import java.util.List ; 73 import java.util.SortedSet ; 74 import java.util.TreeSet ; 75 76 82 public class Crontab { 83 84 88 protected CronDaemon waiter; 89 90 94 protected SortedSet queue; 95 96 99 private static final Logger log = Logger.getLogger(Crontab.class); 100 101 102 108 public Crontab(boolean isDaemon, String threadName) { 109 queue = (SortedSet ) new TreeSet (); 110 waiter = new CronDaemon(this, isDaemon, threadName); 111 } 112 113 116 public Crontab() { 117 this(true, "Crontab"); 118 } 119 120 128 public synchronized CrontabEntry addCrontabEntry(Date date, 129 CrontabListenerI listener) 130 throws CronException { 131 CrontabEntry entry = new CrontabEntry(date, listener); 132 addCrontabEntry(entry); 133 134 return entry; 135 } 136 137 147 public synchronized CrontabEntry addCrontabEntry(int delay, 148 boolean isRepetitive, 149 CrontabListenerI listener) 150 throws CronException { 151 CrontabEntry entry = new CrontabEntry(delay, isRepetitive, listener); 152 addCrontabEntry(entry); 153 154 return entry; 155 } 156 157 172 public synchronized CrontabEntry addCrontabEntry(int minute, int hour, 173 int dayOfMonth, int month, 174 int dayOfWeek, int year, 175 CrontabListenerI listener) 176 throws CronException { 177 CrontabEntry entry = new CrontabEntry(minute, hour, dayOfMonth, month, 178 dayOfWeek, year, listener); 179 addCrontabEntry(entry); 180 181 return entry; 182 } 183 184 200 public synchronized CrontabEntry addCrontabEntry(int minute, int hour, 201 int dayOfMonth, int month, 202 int dayOfWeek, int year, String label, 203 CrontabListenerI listener) 204 throws CronException { 205 CrontabEntry entry = new CrontabEntry(minute, hour, dayOfMonth, month, 206 dayOfWeek, year, label, listener); 207 addCrontabEntry(entry); 208 209 return entry; 210 } 211 212 229 public synchronized CrontabEntry addCrontabEntry(int minute, int hour, 230 int dayOfMonth, int month, 231 int dayOfWeek, int year, String label, 232 CrontabListenerI listener, String jobNumber) 233 throws CronException { 234 CrontabEntry entry = new CrontabEntry(minute, hour, dayOfMonth, month, 235 dayOfWeek, year, label, listener); 236 entry.setJobNumber(jobNumber); 237 addCrontabEntry(entry); 238 239 return entry; 240 } 241 242 243 248 public synchronized void addCrontabEntry(CrontabEntry entry) 249 throws CronException { 250 if (log.isDebugEnabled()) { 251 log.debug("Adding Crontab Entry: " + entry.toString()); 252 } 253 254 for (Iterator i = queue.iterator(); i.hasNext();) { 260 CrontabEntry test = (CrontabEntry) i.next(); 261 if (test.equals(entry)) { 262 if (log.isInfoEnabled()) { 263 log.info("Crontab entry: " + entry + 264 " already exists in crontab. Skipping"); 265 } 266 return; 267 } 268 } 269 270 queue.add(entry); 271 waiter.update(((CrontabEntry) queue.first()).alarmTime); 272 } 273 274 280 public synchronized boolean removeCrontabEntry(CrontabEntry entry) { 281 282 283 for (Iterator i = queue.iterator(); i.hasNext();) { 289 CrontabEntry test = (CrontabEntry) i.next(); 290 if (test.equals(entry)) { 291 if (log.isInfoEnabled()) { 292 log.info("Crontab entry: " + entry + 293 " already exists in crontab. Skipping"); 294 } 295 queue.remove(test); 296 if (queue.size() > 0) { 297 waiter.update(((CrontabEntry) queue.first()).alarmTime); 298 } 299 return true; 300 } 301 } 302 303 return false; 304 } 305 306 309 public synchronized void removeAllCrontabEntries() { 310 if (waiter != null) { 311 waiter.stop(); 312 } 313 314 waiter = null; 315 queue.clear(); 316 } 317 318 324 public synchronized boolean containsCrontabEntry(CrontabEntry oneEntry) { 325 return queue.contains(oneEntry); 326 } 327 328 333 public synchronized List getAllEntries() { 334 final LinkedList result = new LinkedList (); 335 Iterator iterator = queue.iterator(); 336 337 while (iterator.hasNext()) { 338 result.add(iterator.next()); 339 } 340 341 return result; 342 } 343 344 347 protected synchronized void notifyListeners() { 348 349 if (queue.isEmpty()) { 351 log.debug("Execution queue is empty, exiting notifyListeners()"); 352 return; 353 } 355 356 CrontabEntry entry = (CrontabEntry) queue.first(); 358 queue.remove(entry); 359 360 try { 361 if (log.isDebugEnabled()) { 362 log.debug("Handling crontab entry: " + entry.toString()); 363 } 364 365 entry.listener.handleCrontabEntry(entry); 366 } catch (Throwable e) { 367 log.error("Error handling crontab entry", e); 368 } 369 if (entry.isRepetitive) { 371 if (log.isDebugEnabled()) { 372 log.debug("Re-adding repetitive crontab back to queue: " + entry.toString()); 373 } 374 entry.updateEntryTime(); 375 queue.add(entry); 376 } else { 377 if (log.isDebugEnabled()) { 378 log.debug("Finished non-repetitive cron: " + entry.toString()); 379 } 380 } 381 382 if (!queue.isEmpty()) { 384 long alarmTime = ((CrontabEntry) queue.first()).alarmTime; 385 386 if (alarmTime - System.currentTimeMillis() < 1000) { 387 notifyListeners(); 388 } else { 389 waiter.restart(alarmTime); 390 } 391 } 392 393 if (log.isDebugEnabled()) { 394 log.debug("Execution queue is empty, exiting notifyListeners()"); 395 396 } 397 } 398 399 402 public void finalize() { 403 if (waiter != null) { 404 waiter.stop(); 405 } 406 } 407 } | Popular Tags |