1 17 18 21 package org.quartz; 22 23 import java.text.ParseException ; 24 import java.util.Calendar ; 25 import java.util.Date ; 26 import java.util.TimeZone ; 27 28 29 178 public class CronTrigger extends Trigger { 179 180 187 188 195 public static final int MISFIRE_INSTRUCTION_FIRE_ONCE_NOW = 1; 196 197 206 public static final int MISFIRE_INSTRUCTION_DO_NOTHING = 2; 207 208 215 216 private CronExpression cronEx = null; 217 private Date startTime = null; 218 private Date endTime = null; 219 private Date nextFireTime = null; 220 private Date previousFireTime = null; 221 private transient TimeZone timeZone = null; 222 223 230 231 241 public CronTrigger() { 242 super(); 243 setStartTime(new Date ()); 244 setTimeZone(TimeZone.getDefault()); 245 } 246 247 257 public CronTrigger(String name, String group) { 258 super(name, group); 259 setStartTime(new Date ()); 260 setTimeZone(TimeZone.getDefault()); 261 } 262 263 274 public CronTrigger(String name, String group, String cronExpression) 275 throws ParseException { 276 277 super(name, group); 278 279 setCronExpression(cronExpression); 280 281 setStartTime(new Date ()); 282 setTimeZone(TimeZone.getDefault()); 283 } 284 285 296 public CronTrigger(String name, String group, String jobName, 297 String jobGroup) { 298 super(name, group, jobName, jobGroup); 299 setStartTime(new Date ()); 300 setTimeZone(TimeZone.getDefault()); 301 } 302 303 315 public CronTrigger(String name, String group, String jobName, 316 String jobGroup, String cronExpression) throws ParseException { 317 this(name, group, jobName, jobGroup, null, null, cronExpression, 318 TimeZone.getDefault()); 319 } 320 321 328 public CronTrigger(String name, String group, String jobName, 329 String jobGroup, String cronExpression, TimeZone timeZone) 330 throws ParseException { 331 this(name, group, jobName, jobGroup, null, null, cronExpression, 332 timeZone); 333 } 334 335 353 public CronTrigger(String name, String group, String jobName, 354 String jobGroup, Date startTime, Date endTime, String cronExpression) 355 throws ParseException { 356 super(name, group, jobName, jobGroup); 357 358 setCronExpression(cronExpression); 359 360 if (startTime == null) { 361 startTime = new Date (); 362 } 363 setStartTime(startTime); 364 if (endTime != null) { 365 setEndTime(endTime); 366 } 367 setTimeZone(TimeZone.getDefault()); 368 369 } 370 371 409 public CronTrigger(String name, String group, String jobName, 410 String jobGroup, Date startTime, Date endTime, 411 String cronExpression, TimeZone timeZone) throws ParseException { 412 super(name, group, jobName, jobGroup); 413 414 setCronExpression(cronExpression); 415 416 if (startTime == null) { 417 startTime = new Date (); 418 } 419 setStartTime(startTime); 420 if (endTime != null) { 421 setEndTime(endTime); 422 } 423 if (timeZone == null) { 424 setTimeZone(TimeZone.getDefault()); 425 } else { 426 setTimeZone(timeZone); 427 } 428 } 429 430 437 438 public Object clone() { 439 CronTrigger copy = (CronTrigger) super.clone(); 440 copy.setCronExpression((CronExpression)cronEx.clone()); 441 return copy; 442 } 443 444 public void setCronExpression(String cronExpression) throws ParseException { 445 this.cronEx = new CronExpression(cronExpression); 446 this.cronEx.setTimeZone(getTimeZone()); 447 } 448 449 public String getCronExpression() { 450 return cronEx == null ? null : cronEx.getCronExpression(); 451 } 452 453 public void setCronExpression(CronExpression cronExpression) { 454 this.cronEx = cronExpression; 455 this.timeZone = cronExpression.getTimeZone(); 456 } 457 458 463 public Date getStartTime() { 464 return this.startTime; 465 } 466 467 public void setStartTime(Date startTime) { 468 if (startTime == null) { 469 throw new IllegalArgumentException ("Start time cannot be null"); 470 } 471 472 Date eTime = getEndTime(); 473 if (eTime != null && startTime != null && eTime.before(startTime)) { 474 throw new IllegalArgumentException ( 475 "End time cannot be before start time"); 476 } 477 478 Calendar cl = Calendar.getInstance(); 483 cl.setTime(startTime); 484 cl.set(Calendar.MILLISECOND, 0); 485 486 this.startTime = cl.getTime(); 487 } 488 489 497 public Date getEndTime() { 498 return this.endTime; 499 } 500 501 public void setEndTime(Date endTime) { 502 Date sTime = getStartTime(); 503 if (sTime != null && endTime != null && sTime.after(endTime)) { 504 throw new IllegalArgumentException ( 505 "End time cannot be before start time"); 506 } 507 508 this.endTime = endTime; 509 } 510 511 519 public Date getNextFireTime() { 520 return this.nextFireTime; 521 } 522 523 529 public Date getPreviousFireTime() { 530 return this.previousFireTime; 531 } 532 533 539 public void setNextFireTime(Date nextFireTime) { 540 this.nextFireTime = nextFireTime; 541 } 542 543 552 public void setPreviousFireTime(Date previousFireTime) { 553 this.previousFireTime = previousFireTime; 554 } 555 556 562 public TimeZone getTimeZone() { 563 564 if(cronEx != null) { 565 return cronEx.getTimeZone(); 566 } 567 568 if (timeZone == null) { 569 timeZone = TimeZone.getDefault(); 570 } 571 return timeZone; 572 } 573 574 580 public void setTimeZone(TimeZone timeZone) { 581 if(cronEx != null) { 582 cronEx.setTimeZone(timeZone); 583 } 584 this.timeZone = timeZone; 585 } 586 587 599 public Date getFireTimeAfter(Date afterTime) { 600 if (afterTime == null) { 601 afterTime = new Date (); 602 } 603 604 if (getStartTime().after(afterTime)) { 605 afterTime = new Date (getStartTime().getTime() - 1000l); 606 } 607 608 if (getEndTime() != null && (afterTime.compareTo(getEndTime()) >= 0)) { 609 return null; 610 } 611 612 Date pot = getTimeAfter(afterTime); 613 if (getEndTime() != null && pot != null && pot.after(getEndTime())) { 614 return null; 615 } 616 617 return pot; 618 } 619 620 631 public Date getFinalFireTime() { 632 Date resultTime; 633 if (getEndTime() != null) { 634 resultTime = getTimeBefore(new Date (getEndTime().getTime() + 1000l)); 635 } else { 636 resultTime = (cronEx == null) ? null : cronEx.getFinalFireTime(); 637 } 638 639 if ((resultTime != null) && (getStartTime() != null) && (resultTime.before(getStartTime()))) { 640 return null; 641 } 642 643 return resultTime; 644 } 645 646 652 public boolean mayFireAgain() { 653 return (getNextFireTime() != null); 654 } 655 656 protected boolean validateMisfireInstruction(int misfireInstruction) { 657 if (misfireInstruction < MISFIRE_INSTRUCTION_SMART_POLICY) { 658 return false; 659 } 660 661 if (misfireInstruction > MISFIRE_INSTRUCTION_DO_NOTHING) { 662 return false; 663 } 664 665 return true; 666 } 667 668 683 public void updateAfterMisfire(org.quartz.Calendar cal) { 684 int instr = getMisfireInstruction(); 685 686 if (instr == MISFIRE_INSTRUCTION_SMART_POLICY) { 687 instr = MISFIRE_INSTRUCTION_FIRE_ONCE_NOW; 688 } 689 690 if (instr == MISFIRE_INSTRUCTION_DO_NOTHING) { 691 Date newFireTime = getFireTimeAfter(new Date ()); 692 while (newFireTime != null && cal != null 693 && !cal.isTimeIncluded(newFireTime.getTime())) { 694 newFireTime = getFireTimeAfter(newFireTime); 695 } 696 setNextFireTime(newFireTime); 697 } else if (instr == MISFIRE_INSTRUCTION_FIRE_ONCE_NOW) { 698 setNextFireTime(new Date ()); 699 } 700 } 701 702 716 public boolean willFireOn(Calendar test) { 717 return willFireOn(test, false); 718 } 719 720 737 public boolean willFireOn(Calendar test, boolean dayOnly) { 738 739 test = (Calendar) test.clone(); 740 741 test.set(Calendar.MILLISECOND, 0); 743 if(dayOnly) { 744 test.set(Calendar.HOUR, 0); 745 test.set(Calendar.MINUTE, 0); 746 test.set(Calendar.SECOND, 0); 747 } 748 749 Date testTime = test.getTime(); 750 751 Date fta = getFireTimeAfter(new Date (test.getTime().getTime() - 1000)); 752 753 Calendar p = Calendar.getInstance(test.getTimeZone()); 754 p.setTime(fta); 755 756 int year = p.get(Calendar.YEAR); 757 int month = p.get(Calendar.MONTH); 758 int day = p.get(Calendar.DATE); 759 760 if(dayOnly) { 761 return (year == test.get(Calendar.YEAR) 762 && month == test.get(Calendar.MONTH) 763 && day == test.get(Calendar.DATE)); 764 } 765 766 while(fta.before(testTime)) { 767 fta = getFireTimeAfter(fta); 768 } 769 770 if(fta.equals(testTime)) { 771 return true; 772 } 773 774 return false; 775 } 776 777 798 public int executionComplete(JobExecutionContext context, 799 JobExecutionException result) { 800 if (result != null && result.refireImmediately()) { 801 return INSTRUCTION_RE_EXECUTE_JOB; 802 } 803 804 if (result != null && result.unscheduleFiringTrigger()) { 805 return INSTRUCTION_SET_TRIGGER_COMPLETE; 806 } 807 808 if (result != null && result.unscheduleAllTriggers()) { 809 return INSTRUCTION_SET_ALL_JOB_TRIGGERS_COMPLETE; 810 } 811 812 if (!mayFireAgain()) { 813 return INSTRUCTION_DELETE_TRIGGER; 814 } 815 816 return INSTRUCTION_NOOP; 817 } 818 819 829 public void triggered(org.quartz.Calendar calendar) { 830 previousFireTime = nextFireTime; 831 nextFireTime = getFireTimeAfter(nextFireTime); 832 833 while (nextFireTime != null && calendar != null 834 && !calendar.isTimeIncluded(nextFireTime.getTime())) { 835 nextFireTime = getFireTimeAfter(nextFireTime); 836 } 837 } 838 839 843 public void updateWithNewCalendar(org.quartz.Calendar calendar, long misfireThreshold) 844 { 845 nextFireTime = getFireTimeAfter(previousFireTime); 846 847 Date now = new Date (); 848 do { 849 while (nextFireTime != null && calendar != null 850 && !calendar.isTimeIncluded(nextFireTime.getTime())) { 851 nextFireTime = getFireTimeAfter(nextFireTime); 852 } 853 854 if(nextFireTime != null && nextFireTime.before(now)) { 855 long diff = now.getTime() - nextFireTime.getTime(); 856 if(diff >= misfireThreshold) { 857 nextFireTime = getFireTimeAfter(nextFireTime); 858 continue; 859 } 860 } 861 }while(false); 862 } 863 864 881 public Date computeFirstFireTime(org.quartz.Calendar calendar) { 882 nextFireTime = getFireTimeAfter(new Date (getStartTime().getTime() - 1000l)); 883 884 while (nextFireTime != null && calendar != null 885 && !calendar.isTimeIncluded(nextFireTime.getTime())) { 886 nextFireTime = getFireTimeAfter(nextFireTime); 887 } 888 889 return nextFireTime; 890 } 891 892 public String getExpressionSummary() { 893 return cronEx == null ? null : cronEx.getExpressionSummary(); 894 } 895 896 902 protected Date getTimeAfter(Date afterTime) { 903 return (cronEx == null) ? null : cronEx.getTimeAfter(afterTime); 904 } 905 906 910 protected Date getTimeBefore(Date endTime) { 911 return (cronEx == null) ? null : cronEx.getTimeBefore(endTime); 912 } 913 914 public static void main(String [] args) throws Exception { 917 918 String expr = "15 10 0/4 * * ?"; 919 if(args != null && args.length > 0 && args[0] != null) { 920 expr = args[0]; 921 } 922 923 CronTrigger ct = new CronTrigger("t", "g", "j", "g", new Date (), null, expr); 924 ct.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); 925 System.err.println(ct.getExpressionSummary()); 926 System.err.println("tz=" + ct.getTimeZone().getID()); 927 System.err.println(); 928 929 java.util.List times = TriggerUtils.computeFireTimes(ct, null, 25); 930 931 for (int i = 0; i < times.size(); i++) { 932 System.err.println("firetime = " + times.get(i)); 933 } 934 935 Calendar tt = Calendar.getInstance(); 936 tt.set(Calendar.DATE, 17); 937 tt.set(Calendar.MONTH, 5 - 1); 938 tt.set(Calendar.HOUR, 11); 939 tt.set(Calendar.MINUTE, 0); 940 tt.set(Calendar.SECOND, 7); 941 942 System.err.println("\nWill fire on: " + tt.getTime() + " -- " + ct.willFireOn(tt, false)); 943 944 945 1017 } 1018} 1019 1020 | Popular Tags |