1 2 18 19 22 package org.quartz; 23 24 import java.util.Date ; 25 import java.util.LinkedList ; 26 27 import org.quartz.utils.Key; 28 29 30 61 public abstract class Trigger implements java.io.Serializable , Cloneable , 62 Comparable { 63 64 private static final long serialVersionUID = -3904243490805975570L; 65 66 73 74 80 public static final int INSTRUCTION_NOOP = 0; 81 82 91 public static final int INSTRUCTION_RE_EXECUTE_JOB = 1; 92 93 99 public static final int INSTRUCTION_SET_TRIGGER_COMPLETE = 2; 100 101 107 public static final int INSTRUCTION_DELETE_TRIGGER = 3; 108 109 116 public static final int INSTRUCTION_SET_ALL_JOB_TRIGGERS_COMPLETE = 4; 117 118 125 public static final int INSTRUCTION_SET_TRIGGER_ERROR = 5; 126 127 133 public static final int INSTRUCTION_SET_ALL_JOB_TRIGGERS_ERROR = 6; 134 135 148 public static final int MISFIRE_INSTRUCTION_SMART_POLICY = 0; 149 150 155 public static final int STATE_NORMAL = 0; 156 157 162 public static final int STATE_PAUSED = 1; 163 164 174 public static final int STATE_COMPLETE = 2; 175 176 193 public static final int STATE_ERROR = 3; 194 195 196 209 public static final int STATE_BLOCKED = 4; 210 211 216 public static final int STATE_NONE = -1; 217 218 221 public static final int DEFAULT_PRIORITY = 5; 222 223 230 231 private String name; 232 233 private String group = Scheduler.DEFAULT_GROUP; 234 235 private String jobName; 236 237 private String jobGroup = Scheduler.DEFAULT_GROUP; 238 239 private String description; 240 241 private JobDataMap jobDataMap; 242 243 private boolean volatility = false; 244 245 private String calendarName = null; 246 247 private String fireInstanceId = null; 248 249 private int misfireInstruction = MISFIRE_INSTRUCTION_SMART_POLICY; 250 251 private LinkedList triggerListeners = new LinkedList (); 252 253 private int priority = DEFAULT_PRIORITY; 254 255 private transient Key key = null; 256 257 264 265 266 267 279 public Trigger() { 280 } 282 283 299 public Trigger(String name, String group) { 300 setName(name); 301 setGroup(group); 302 } 303 304 314 public Trigger(String name, String group, String jobName, String jobGroup) { 315 setName(name); 316 setGroup(group); 317 setJobName(jobName); 318 setJobGroup(jobGroup); 319 } 320 321 328 329 334 public String getName() { 335 return name; 336 } 337 338 346 public void setName(String name) { 347 if (name == null || name.trim().length() == 0) { 348 throw new IllegalArgumentException ( 349 "Trigger name cannot be null or empty."); 350 } 351 352 this.name = name; 353 } 354 355 360 public String getGroup() { 361 return group; 362 } 363 364 374 public void setGroup(String group) { 375 if (group != null && group.trim().length() == 0) { 376 throw new IllegalArgumentException ( 377 "Group name cannot be an empty string."); 378 } 379 380 if(group == null) { 381 group = Scheduler.DEFAULT_GROUP; 382 } 383 384 this.group = group; 385 } 386 387 392 public String getJobName() { 393 return jobName; 394 } 395 396 404 public void setJobName(String jobName) { 405 if (jobName == null || jobName.trim().length() == 0) { 406 throw new IllegalArgumentException ( 407 "Job name cannot be null or empty."); 408 } 409 410 this.jobName = jobName; 411 } 412 413 419 public String getJobGroup() { 420 return jobGroup; 421 } 422 423 434 public void setJobGroup(String jobGroup) { 435 if (jobGroup != null && jobGroup.trim().length() == 0) { 436 throw new IllegalArgumentException ( 437 "Group name cannot be null or empty."); 438 } 439 440 if(jobGroup == null) { 441 jobGroup = Scheduler.DEFAULT_GROUP; 442 } 443 444 this.jobGroup = jobGroup; 445 } 446 447 453 public String getFullName() { 454 return group + "." + name; 455 } 456 457 public Key getKey() { 458 if(key == null) { 459 key = new Key(getName(), getGroup()); 460 } 461 462 return key; 463 } 464 465 471 public String getFullJobName() { 472 return jobGroup + "." + jobName; 473 } 474 475 483 public String getDescription() { 484 return description; 485 } 486 487 494 public void setDescription(String description) { 495 this.description = description; 496 } 497 498 505 public void setVolatility(boolean volatility) { 506 this.volatility = volatility; 507 } 508 509 518 public void setCalendarName(String calendarName) { 519 this.calendarName = calendarName; 520 } 521 522 530 public String getCalendarName() { 531 return calendarName; 532 } 533 534 545 public JobDataMap getJobDataMap() { 546 if (jobDataMap == null) { 547 jobDataMap = new JobDataMap(); 548 } 549 return jobDataMap; 550 } 551 552 553 559 public void setJobDataMap(JobDataMap jobDataMap) { 560 this.jobDataMap = jobDataMap; 561 } 562 563 577 public boolean isVolatile() { 578 return volatility; 579 } 580 581 593 public int getPriority() { 594 return priority; 595 } 596 597 598 610 public void setPriority(int priority) { 611 this.priority = priority; 612 } 613 614 620 public void addTriggerListener(String name) { 621 if (triggerListeners.contains(name)) { 622 throw new IllegalArgumentException ( 623 "Trigger listener '" + name + "' is already registered for trigger: " + getFullName()); 624 } 625 626 triggerListeners.add(name); 627 } 628 629 637 public boolean removeTriggerListener(String name) { 638 return triggerListeners.remove(name); 639 } 640 641 648 public String [] getTriggerListenerNames() { 649 return (String [])triggerListeners.toArray(new String [triggerListeners.size()]); 650 } 651 652 655 public void clearAllTriggerListeners() { 656 triggerListeners.clear(); 657 } 658 659 673 public abstract void triggered(Calendar calendar); 674 675 696 public abstract Date computeFirstFireTime(Calendar calendar); 697 698 723 public abstract int executionComplete(JobExecutionContext context, 724 JobExecutionException result); 725 726 737 public abstract boolean mayFireAgain(); 738 739 744 public abstract Date getStartTime(); 745 746 public abstract void setStartTime(Date startTime); 747 748 public abstract void setEndTime(Date endTime); 749 750 758 public abstract Date getEndTime(); 759 760 768 public abstract Date getNextFireTime(); 769 770 775 public abstract Date getPreviousFireTime(); 776 777 784 public abstract Date getFireTimeAfter(Date afterTime); 785 786 796 public abstract Date getFinalFireTime(); 797 798 816 public void setMisfireInstruction(int misfireInstruction) { 817 if (!validateMisfireInstruction(misfireInstruction)) { 818 throw new IllegalArgumentException ( 819 "The misfire instruction code is invalid for this type of trigger."); 820 } 821 this.misfireInstruction = misfireInstruction; 822 } 823 824 protected abstract boolean validateMisfireInstruction(int misfireInstruction); 825 826 844 public int getMisfireInstruction() { 845 return misfireInstruction; 846 } 847 848 863 public abstract void updateAfterMisfire(Calendar cal); 864 865 883 public abstract void updateWithNewCalendar(Calendar cal, long misfireThreshold); 884 885 894 public void validate() throws SchedulerException { 895 if (name == null) { 896 throw new SchedulerException("Trigger's name cannot be null", 897 SchedulerException.ERR_CLIENT_ERROR); 898 } 899 900 if (group == null) { 901 throw new SchedulerException("Trigger's group cannot be null", 902 SchedulerException.ERR_CLIENT_ERROR); 903 } 904 905 if (jobName == null) { 906 throw new SchedulerException( 907 "Trigger's related Job's name cannot be null", 908 SchedulerException.ERR_CLIENT_ERROR); 909 } 910 911 if (jobGroup == null) { 912 throw new SchedulerException( 913 "Trigger's related Job's group cannot be null", 914 SchedulerException.ERR_CLIENT_ERROR); 915 } 916 } 917 918 931 public void setFireInstanceId(String id) { 932 this.fireInstanceId = id; 933 } 934 935 940 public String getFireInstanceId() { 941 return fireInstanceId; 942 } 943 944 949 public String toString() { 950 return "Trigger '" + getFullName() + "': triggerClass: '" 951 + getClass().getName() + " isVolatile: " + isVolatile() 952 + " calendar: '" + getCalendarName() + "' misfireInstruction: " 953 + getMisfireInstruction() + " nextFireTime: " + getNextFireTime(); 954 } 955 956 962 public int compareTo(Object obj) { 963 Trigger other = (Trigger) obj; 964 965 Date myTime = getNextFireTime(); 966 Date otherTime = other.getNextFireTime(); 967 968 if (myTime == null && otherTime == null) { 969 return 0; 970 } 971 972 if (myTime == null) { 973 return 1; 974 } 975 976 if (otherTime == null) { 977 return -1; 978 } 979 980 if(myTime.before(otherTime)) { 981 return -1; 982 } 983 984 if(myTime.after(otherTime)) { 985 return 1; 986 } 987 988 return 0; 989 } 990 991 public boolean equals(Object obj) { 992 if (!(obj instanceof Trigger)) { 993 return false; 994 } 995 996 Trigger other = (Trigger) obj; 997 998 if (!other.getName().equals(getName())) { 999 return false; 1000 } 1001 if (!other.getGroup().equals(getGroup())) { 1002 return false; 1003 } 1004 1005 return true; 1006 } 1007 1008 1009 public int hashCode() { 1010 return getFullName().hashCode(); 1011 } 1012 1013 public Object clone() { 1014 Trigger copy; 1015 try { 1016 copy = (Trigger) super.clone(); 1017 1018 copy.triggerListeners = (LinkedList )triggerListeners.clone(); 1019 1020 if (jobDataMap != null) { 1024 copy.jobDataMap = (JobDataMap)jobDataMap.clone(); 1025 } 1026 1027 } catch (CloneNotSupportedException ex) { 1028 throw new IncompatibleClassChangeError ("Not Cloneable."); 1029 } 1030 return copy; 1031 } 1032} 1033 | Popular Tags |