彻底理解spring的定制任务(scheduling)

下面让我们来看看 spring的源代码

java 代码
  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. package org.springframework.scheduling.timer;
  17. import java.util.TimerTask;
  18. /**
  19. * JavaBean that describes a scheduled TimerTask, consisting of
  20. * the TimerTask itself (or a Runnable to create a TimerTask for)
  21. * and a delay plus period. Period needs to be specified;
  22. * there is no point in a default for it.
  23. *
  24. *

    The JDK Timer does not offer more sophisticated scheduling 字串2

  25. * options such as cron expressions. Consider using Quartz for
  26. * such advanced needs.
  27. *
  28. *

    Note that Timer uses a TimerTask instance that is shared

    字串1
  29. * between repeated executions, in contrast to Quartz which
  30. * instantiates a new Job for each execution.
  31. *
  32. * @author Juergen Hoeller
  33. * @since 19.02.2004
  34. * @see java.util.TimerTask
  35. * @see java.util.Timer#schedule(TimerTask, long, long)
  36. * @see java.util.Timer#scheduleAtFixedRate(TimerTask, long, long)
  37. */
  38. public class ScheduledTimerTask {
  39. private TimerTask timerTask;
  40. private long delay = 0 ;
  41. private long period = 0 ;
  42. private boolean fixedRate = false ;
  43. /**
  44. * Create a new ScheduledTimerTask,
  45. * to be populated via bean properties.
  46. * @see #setTimerTask
  47. * @see #setDelay
  48. * @see #setPeriod
  49. * @see #setFixedRate
  50. */
  51. public ScheduledTimerTask() {
  52. }
  53. /**
  54. * Create a new ScheduledTimerTask, with default
  55. * one-time execution without delay.
  56. * @param timerTask the TimerTask to schedule
  57. */
  58. public ScheduledTimerTask(TimerTask timerTask) {
  59. this .timerTask = timerTask;
  60. }
  61. /**
  62. * Create a new ScheduledTimerTask, with default
  63. * one-time execution with the given delay.
  64. * @param timerTask the TimerTask to schedule
  65. * @param delay the delay before starting the task for the first time (ms)
  66. */
  67. public ScheduledTimerTask(TimerTask timerTask, long delay) {
  68. this .timerTask = timerTask;
  69. this .delay = delay;
  70. }

    字串9

  71. /**
  72. * Create a new ScheduledTimerTask.
  73. * @param timerTask the TimerTask to schedule
  74. * @param delay the delay before starting the task for the first time (ms)
  75. * @param period the period between repeated task executions (ms)
  76. * @param fixedRate whether to schedule as fixed-rate execution
  77. */
  78. public ScheduledTimerTask(TimerTask timerTask, long delay, long period, boolean fixedRate) {
  79. this .timerTask = timerTask;
  80. this .delay = delay;
  81. this .period = period;
  82. this .fixedRate = fixedRate;
  83. }
  84. /**
  85. * Create a new ScheduledTimerTask, with default
  86. * one-time execution without delay.
  87. * @param timerTask the Runnable to schedule as TimerTask
  88. */
  89. public ScheduledTimerTask(Runnable timerTask) {
  90. setRunnable(timerTask);
  91. }
  92. /**
  93. * Create a new ScheduledTimerTask, with default
  94. * one-time execution with the given delay.
  95. * @param timerTask the Runnable to schedule as TimerTask
  96. * @param delay the delay before starting the task for the first time (ms)
  97. */
  98. public ScheduledTimerTask(Runnable timerTask, long delay) {
  99. setRunnable(timerTask);
  100. this .delay = delay;
  101. }
  102. /**
  103. * Create a new ScheduledTimerTask.
  104. * @param timerTask the Runnable to schedule as TimerTask
  105. * @param delay the delay before starting the task for the first time (ms)
  106. * @param period the period between repeated task executions (ms)
  107. * @param fixedRate whether to schedule as fixed-rate execution
  108. */
  109. public ScheduledTimerTask(Runnable timerTask, long delay, long period, boolean fixedRate) {
  110. setRunnable(timerTask);
  111. this .delay = delay;
  112. this .period = period;
  113. this .fixedRate = fixedRate;
  114. }
  115. /**
  116. * Set the Runnable to schedule as TimerTask.
  117. * @see DelegatingTimerTask
  118. */
  119. public void setRunnable(Runnable timerTask) {
  120. this .timerTask = new DelegatingTimerTask(timerTask);
  121. }
  122. /**
  123. * Set the TimerTask to schedule.
  124. */
  125. public void setTimerTask(TimerTask timerTask) {
  126. this .timerTask = timerTask;
  127. }
  128. /**
  129. * Return the TimerTask to schedule.
  130. */
  131. public TimerTask getTimerTask() {
  132. return timerTask;
  133. }
  134. /**
  135. * Set the delay before starting the task for the first time,
  136. * in milliseconds. Default is 0, immediately starting the
  137. * task after successful scheduling.
  138. */
  139. public void setDelay( long delay) {
  140. this .delay = delay;
  141. }
  142. /**
  143. * Return the delay before starting the job for the first time.
  144. */
  145. public long getDelay() {
  146. return delay;
  147. }
  148. /**
  149. * Set the period between repeated task executions, in milliseconds.
  150. * Default is 0, leading to one-time execution. In case of a positive
  151. * value, the task will be executed repeatedly, with the given interval
  152. * inbetween executions.
  153. *

    Note that the semantics of the period vary between fixed-rate

    字串8
  154. * and fixed-delay execution.
  155. * @see #setFixedRate
  156. */
  157. public void setPeriod( long period) {
  158. this .period = period;
  159. }
  160. /**
  161. * Return the period between repeated task executions.
  162. */
  163. public long getPeriod() {
  164. return period;
  165. }
  166. /**
  167. * Set whether to schedule as fixed-rate execution, rather than
  168. * fixed-delay execution. Default is "false", i.e. fixed delay.
  169. *

    字串5

  170. See Timer javadoc for details on those execution modes. 字串8

  171. * @see java.util.Timer#schedule(TimerTask, long, long)
  172. * @see java.util.Timer#scheduleAtFixedRate(TimerTask, long, long)
  173. */
  174. public void setFixedRate( boolean fixedRate) {
  175. this .fixedRate = fixedRate;
  176. }
  177. /**
  178. * Return whether to schedule as fixed-rate execution.
  179. */
  180. public boolean isFixedRate() {
  181. return fixedRate;
  182. }
  183. }

说实话这个类也没什么,只是简单的包装了我们的timertask,里面也就只有几个属性,一个是时间片,一个是任务等。 字串6

真正运行我们的任务的类是: 字串3

java 代码
  1. /*
  2. * Copyright 2002-2006 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. package org.springframework.scheduling.timer;
  17. import java.util.Timer;
  18. import org.apache.commons.logging.Log;
  19. import org.apache.commons.logging.LogFactory;
  20. import org.springframework.beans.factory.DisposableBean;
  21. import org.springframework.beans.factory.FactoryBean;
  22. import org.springframework.beans.factory.InitializingBean;
  23. /**
  24. * FactoryBean that sets up a JDK 1.3+ Timer and exposes it for bean references.
  25. *
  26. *

    Allows for registration of ScheduledTimerTasks, automatically starting

    字串4

  27. * the Timer on initialization and cancelling it on destruction of the context.
  28. * In scenarios that just require static registration of tasks at startup,
  29. * there is no need to access the Timer instance itself in application code.
  30. *
  31. *

    Note that Timer uses a TimerTask instance that is shared between 字串2

  32. * repeated executions, in contrast to Quartz which instantiates a new
  33. * Job for each execution.
  34. *
  35. * @author Juergen Hoeller
  36. * @since 19.02.2004
  37. * @see ScheduledTimerTask
  38. * @see java.util.Timer
  39. * @see java.util.TimerTask
  40. */
  41. public class TimerFactoryBean implements FactoryBean, InitializingBean, DisposableBean {
  42. protected final Log logger = LogFactory.getLog(getClass());
  43. private ScheduledTimerTask[] scheduledTimerTasks;
  44. private boolean daemon = false ;
  45. private Timer timer;
  46. /**
  47. * Register a list of ScheduledTimerTask objects with the Timer that
  48. * this FactoryBean creates. Depending on each SchedulerTimerTask"s
  49. * settings, it will be registered via one of Timer"s schedule methods.
  50. * @see java.util.Timer#schedule(java.util.TimerTask, long)
  51. * @see java.util.Timer#schedule(java.util.TimerTask, long, long)
  52. * @see java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, long, long)
  53. */
  54. public void setScheduledTimerTasks(ScheduledTimerTask[] scheduledTimerTasks) {
  55. this .scheduledTimerTasks = scheduledTimerTasks;
  56. }
  57. /**
  58. * Set whether the timer should use a daemon thread,
  59. * just executing as long as the application itself is running.
  60. *
  61. 字串8


    字串5

  62. Default is "false": The timer will automatically get cancelled on 字串7

  63. * destruction of this FactoryBean. Hence, if the application shuts down,
  64. * tasks will by default finish their execution. Specify "true" for eager
  65. * shutdown of threads that execute tasks.
  66. * @see java.util.Timer#Timer(boolean)
  67. */
  68. public void setDaemon( boolean daemon) {
  69. this .daemon = daemon;
  70. }
  71. public void afterPropertiesSet() {
  72. logger.info( "Initializing Timer" );
  73. this .timer = createTimer( this .daemon);
  74. // Register all ScheduledTimerTasks.
  75. if ( this .scheduledTimerTasks != null ) {
  76. for ( int i = 0 ; i < this .scheduledTimerTasks.length; i++) {
  77. ScheduledTimerTask scheduledTask = this .scheduledTimerTasks[i];
  78. if (scheduledTask.getPeriod() > 0 ) {
  79. // repeated task execution
  80. if (scheduledTask.isFixedRate()) {
  81. this .timer.scheduleAtFixedRate(
  82. scheduledTask.getTimerTask(), scheduledTask.getDelay(), scheduledTask.getPeriod());
  83. }
  84. else {
  85. this .timer.schedule(
  86. scheduledTask.getTimerTask(), scheduledTask.getDelay(), scheduledTask.getPeriod());
  87. }
  88. }
  89. else {
  90. // One-time task execution.
  91. this .timer.schedule(scheduledTask.getTimerTask(), scheduledTask.getDelay());
  92. }
  93. }
  94. }
  95. }
  96. /**
  97. * Create a new Timer instance. Called by afterPropertiesSet.
  98. * Can be overridden in subclasses to provide custom Timer subclasses.
  99. * @param daemon whether to create a Timer that runs as daemon thread
  100. * @return a new Timer instance
  101. * @see #afterPropertiesSet()
  102. * @see java.util.Timer#Timer(boolean)
  103. */
  104. protected Timer createTimer( boolean daemon) {
  105. return new Timer(daemon);
  106. }
  107. public Object getObject() {
  108. return this .timer;
  109. }
  110. public Class getObjectType() {
  111. return Timer. class ;
  112. }
  113. public boolean isSingleton() {
  114. return true ;
  115. }
  116. /**
  117. * Cancel the Timer on bean factory shutdown, stopping all scheduled tasks.
  118. * @see java.util.Timer#cancel()
  119. */
  120. public void destroy() {
  121. logger.info( "Cancelling Timer" );
  122. this .timer.cancel();
  123. }
  124. }

这个类就是运行我们任务的类了,我们可以定制N个任务,只需要塞到这里就ok了。

全文阅读:http://www.javah.net/Spring/20070425/421.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值