1. /**//* 2. * Copyright 2002-2005 the original author or authors. 3. * 4. * Licensed under the Apache License, Version 2.0 (the "License"); 5. * you may not use this file except in compliance with the License. 6. * You may obtain a copy of the License at 7. * 8. * http://www.apache.org/licenses/LICENSE-2.0 9. * 10. * Unless required by applicable law or agreed to in writing, software 11. * distributed under the License is distributed on an "AS IS" BASIS, 12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13. * See the License for the specific language governing permissions and 14. * limitations under the License. 15. */ 16. 17. package org.springframework.scheduling.timer; 18. 19. import java.util.TimerTask; 20. 21. /** *//** 22. * JavaBean that describes a scheduled TimerTask, consisting of 23. * the TimerTask itself (or a Runnable to create a TimerTask for) 24. * and a delay plus period. Period needs to be specified; 25. * there is no point in a default for it. 26. * 27. * The JDK Timer does not offer more sophisticated scheduling 28. * options such as cron expressions. Consider using Quartz for 29. * such advanced needs. 30. * 31. * Note that Timer uses a TimerTask instance that is shared 32. * between repeated executions, in contrast to Quartz which 33. * instantiates a new Job for each execution. 34. * 35. * @author Juergen Hoeller 36. * @since 19.02.2004 37. * @see java.util.TimerTask 38. * @see java.util.Timer#schedule(TimerTask, long, long) 39. * @see java.util.Timer#scheduleAtFixedRate(TimerTask, long, long) 40. */ 41. publicclass ScheduledTimerTask ...{ 42. 43. private TimerTask timerTask; 44. 45. privatelong delay =0; 46. 47. privatelong period =0; 48. 49. privateboolean fixedRate =false; 50. 51. 52. /** *//** 53. * Create a new ScheduledTimerTask, 54. * to be populated via bean properties. 55. * @see #setTimerTask 56. * @see #setDelay 57. * @see #setPeriod 58. * @see #setFixedRate 59. */ 60. public ScheduledTimerTask() ...{ 61. } 62. 63. /** *//** 64. * Create a new ScheduledTimerTask, with default 65. * one-time execution without delay. 66. * @param timerTask the TimerTask to schedule 67. */ 68. public ScheduledTimerTask(TimerTask timerTask) ...{ 69. this.timerTask = timerTask; 70. } 71. 72. /** *//** 73. * Create a new ScheduledTimerTask, with default 74. * one-time execution with the given delay. 75. * @param timerTask the TimerTask to schedule 76. * @param delay the delay before starting the task for the first time (ms) 77. */ 78. public ScheduledTimerTask(TimerTask timerTask, long delay) ...{ 79. this.timerTask = timerTask; 80. this.delay = delay; 81. } 82. 83. /** *//** 84. * Create a new ScheduledTimerTask. 85. * @param timerTask the TimerTask to schedule 86. * @param delay the delay before starting the task for the first time (ms) 87. * @param period the period between repeated task executions (ms) 88. * @param fixedRate whether to schedule as fixed-rate execution 89. */ 90. public ScheduledTimerTask(TimerTask timerTask, long delay, long period, boolean fixedRate) ...{ 91. this.timerTask = timerTask; 92. this.delay = delay; 93. this.period = period; 94. this.fixedRate = fixedRate; 95. } 96. 97. /** *//** 98. * Create a new ScheduledTimerTask, with default 99. * one-time execution without delay. 100. * @param timerTask the Runnable to schedule as TimerTask 101. */ 102. public ScheduledTimerTask(Runnable timerTask) ...{ 103. setRunnable(timerTask); 104. } 105. 106. /** *//** 107. * Create a new ScheduledTimerTask, with default 108. * one-time execution with the given delay. 109. * @param timerTask the Runnable to schedule as TimerTask 110. * @param delay the delay before starting the task for the first time (ms) 111. */ 112. public ScheduledTimerTask(Runnable timerTask, long delay) ...{ 113. setRunnable(timerTask); 114. this.delay = delay; 115. } 116. 117. /** *//** 118. * Create a new ScheduledTimerTask. 119. * @param timerTask the Runnable to schedule as TimerTask 120. * @param delay the delay before starting the task for the first time (ms) 121. * @param period the period between repeated task executions (ms) 122. * @param fixedRate whether to schedule as fixed-rate execution 123. */ 124. public ScheduledTimerTask(Runnable timerTask, long delay, long period, boolean fixedRate) ...{ 125. setRunnable(timerTask); 126. this.delay = delay; 127. this.period = period; 128. this.fixedRate = fixedRate; 129. } 130. 131. 132. /** *//** 133. * Set the Runnable to schedule as TimerTask. 134. * @see DelegatingTimerTask 135. */ 136. publicvoid setRunnable(Runnable timerTask) ...{ 137. this.timerTask =new DelegatingTimerTask(timerTask); 138. } 139. 140. /** *//** 141. * Set the TimerTask to schedule. 142. */ 143. publicvoid setTimerTask(TimerTask timerTask) ...{ 144. this.timerTask = timerTask; 145. } 146. 147. /** *//** 148. * Return the TimerTask to schedule. 149. */ 150. public TimerTask getTimerTask() ...{ 151. return timerTask; 152. } 153. 154. /** *//** 155. * Set the delay before starting the task for the first time, 156. * in milliseconds. Default is 0, immediately starting the 157. * task after successful scheduling. 158. */ 159. publicvoid setDelay(long delay) ...{ 160. this.delay = delay; 161. } 162. 163. /** *//** 164. * Return the delay before starting the job for the first time. 165. */ 166. publiclong getDelay() ...{ 167. return delay; 168. } 169. 170. /** *//** 171. * Set the period between repeated task executions, in milliseconds. 172. * Default is 0, leading to one-time execution. In case of a positive 173. * value, the task will be executed repeatedly, with the given interval 174. * inbetween executions. 175. * Note that the semantics of the period vary between fixed-rate 176. * and fixed-delay execution. 177. * @see #setFixedRate 178. */ 179. publicvoid setPeriod(long period) ...{ 180. this.period = period; 181. } 182. 183. /** *//** 184. * Return the period between repeated task executions. 185. */ 186. publiclong getPeriod() ...{ 187. return period; 188. } 189. 190. /** *//** 191. * Set whether to schedule as fixed-rate execution, rather than 192. * fixed-delay execution. Default is "false", i.e. fixed delay. 193. * See Timer javadoc for details on those execution modes. 194. * @see java.util.Timer#schedule(TimerTask, long, long) 195. * @see java.util.Timer#scheduleAtFixedRate(TimerTask, long, long) 196. */ 197. publicvoid setFixedRate(boolean fixedRate) ...{ 198. this.fixedRate = fixedRate; 199. } 200. 201. /** *//** 202. * Return whether to schedule as fixed-rate execution. 203. */ 204. publicboolean isFixedRate() ...{ 205. return fixedRate; 206. } 207. 208. }