TaskInformation.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package com.uas.search.schedule;
  2. import com.uas.search.util.NumberGenerator;
  3. import java.util.Objects;
  4. /**
  5. * 定时任务信息
  6. *
  7. * @author sunyj
  8. * @since 2016年12月19日 上午10:21:28
  9. */
  10. public class TaskInformation {
  11. /**
  12. * 任务 code
  13. */
  14. private String code;
  15. /**
  16. * 任务标题
  17. */
  18. private String title;
  19. /**
  20. * 所执行的任务
  21. */
  22. private Executable command;
  23. /**
  24. * 第一次执行的延迟时间间隔(毫秒)
  25. */
  26. private long initialDelay;
  27. /**
  28. * 两次任务之间的等待时间间隔(毫秒)
  29. */
  30. private long interval;
  31. private ScheduleType scheduleType;
  32. public TaskInformation() {
  33. super();
  34. }
  35. public TaskInformation(String title, Executable command, long initialDelay, long interval, ScheduleType scheduleType) {
  36. init();
  37. this.title = title;
  38. this.command = command;
  39. this.initialDelay = initialDelay;
  40. this.interval = interval;
  41. this.scheduleType = scheduleType;
  42. }
  43. public void init(){
  44. code = NumberGenerator.generateId();
  45. }
  46. public String getCode() {
  47. return code;
  48. }
  49. public void setCode(String code) {
  50. this.code = code;
  51. }
  52. public String getTitle() {
  53. return title;
  54. }
  55. public void setTitle(String title) {
  56. this.title = title;
  57. }
  58. public Executable getCommand() {
  59. return command;
  60. }
  61. public void setCommand(Executable command) {
  62. this.command = command;
  63. }
  64. public long getInitialDelay() {
  65. return initialDelay;
  66. }
  67. public void setInitialDelay(long initialDelay) {
  68. this.initialDelay = initialDelay;
  69. }
  70. public long getInterval() {
  71. return interval;
  72. }
  73. public void setInterval(long interval) {
  74. this.interval = interval;
  75. }
  76. public ScheduleType getScheduleType() {
  77. return scheduleType;
  78. }
  79. public void setScheduleType(ScheduleType scheduleType) {
  80. this.scheduleType = scheduleType;
  81. }
  82. @Override
  83. public boolean equals(Object obj) {
  84. if (this == obj) {
  85. return true;
  86. }
  87. if (obj == null || getClass() != obj.getClass() || !(obj instanceof TaskInformation)) {
  88. return false;
  89. }
  90. TaskInformation other = (TaskInformation) obj;
  91. // command不好比较,不进行比较,也不比较 code
  92. return Objects.equals(title, other.getTitle()) && initialDelay == other.getInitialDelay()
  93. && interval == other.getInterval() && scheduleType == other.getScheduleType();
  94. }
  95. @Override
  96. public String toString() {
  97. return "TaskInformation [code=" + code + ", title=" + title + ", initialDelay=" + initialDelay + ", interval=" + interval + ", scheduleType=" + scheduleType + "]";
  98. }
  99. /**
  100. * 定时的间隔类型
  101. */
  102. public enum ScheduleType {
  103. /**
  104. * Creates and executes a periodic action that becomes enabled first
  105. * after the given initial delay, and subsequently with the given
  106. * period; that is executions will commence after
  107. * <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then
  108. * <tt>initialDelay + 2 * period</tt>, and so on.
  109. * If any execution of the task
  110. * encounters an exception, subsequent executions are suppressed.
  111. * Otherwise, the task will only terminate via cancellation or
  112. * termination of the executor. If any execution of this task
  113. * takes longer than its period, then subsequent executions
  114. * may start late, but will not concurrently execute.
  115. */
  116. FixedRate,
  117. /**
  118. * Creates and executes a periodic action that becomes enabled first
  119. * after the given initial delay, and subsequently with the
  120. * given delay between the termination of one execution and the
  121. * commencement of the next. If any execution of the task
  122. * encounters an exception, subsequent executions are suppressed.
  123. * Otherwise, the task will only terminate via cancellation or
  124. * termination of the executor.
  125. */
  126. FixedDelay
  127. }
  128. }