读书人

spring 任务配备

发布时间: 2012-10-10 13:58:11 作者: rapoo

spring 任务配置
xml 代码

  1. <bean?id="infoCenterAutoBuildTask"?? ????class="com.teesoo.teanet.scheduling.InfoCenterAutoBuildTask">??
  2. ????<property?name="baseService"?ref="baseService"?/>?? ????<property?name="htmlCreator"?ref="htmlCreator"?/>??
  3. </bean>?? ??
  4. <bean?id="scheduledTask"?? ????class="org.springframework.scheduling.timer.ScheduledTimerTask">??
  5. ????<!--?wait?10?seconds?before?starting?repeated?execution?-->?? ????<property?name="delay"?value="10000"?/>??
  6. ????<!--?run?every?50?seconds?-->?? ????<property?name="period"?value="1000000"?/>??
  7. ????<property?name="timerTask"?ref="infoCenterAutoBuildTask"?/>?? </bean>??
  8. ?? ??
  9. <bean?id="timerFactory"?class="org.springframework.scheduling.timer.TimerFactoryBean">?? ???<property?name="scheduledTimerTasks">??
  10. ???????<list>?? ???????????<!--?see?the?example?above?-->??
  11. ???????????<ref?bean="scheduledTask"?/>?? ???????</list>??
  12. ???</property>?? </bean>??

上面三个配置文件中只有一个配置文件是涉及到您自己的class的,其他的都是spring的类。很简单吧

我们只需要涉及一个class让他继承java.util.TimerTask;

java 代码
  1. BaseTask?extends?java.util.TimerTask?{ ?? //用户只需要实现这个方面,把自己的任务放到这里 ??
  2. public?void?run(){ ?? } ??
  3. }??

?

java 代码(spring的源代码)

  1. /* ? ?*?Copyright?2002-2005?the?original?author?or?authors. ?
  2. ?*? ? ?*?Licensed?under?the?Apache?License,?Version?2.0?(the?"License"); ?
  3. ?*?you?may?not?use?this?file?except?in?compliance?with?the?License. ? ?*?You?may?obtain?a?copy?of?the?License?at ?
  4. ?*? ? ?*??????http://www.apache.org/licenses/LICENSE-2.0 ?
  5. ?*? ? ?*?Unless?required?by?applicable?law?or?agreed?to?in?writing,?software ?
  6. ?*?distributed?under?the?License?is?distributed?on?an?"AS?IS"?BASIS, ? ?*?WITHOUT?WARRANTIES?OR?CONDITIONS?OF?ANY?KIND,?either?express?or?implied. ?
  7. ?*?See?the?License?for?the?specific?language?governing?permissions?and ? ?*?limitations?under?the?License. ?
  8. ?*/?? ??
  9. package?org.springframework.scheduling.timer; ?? ??
  10. import?java.util.TimerTask; ?? ??
  11. /** ? ?*?JavaBean?that?describes?a?scheduled?TimerTask,?consisting?of ?
  12. ?*?the?TimerTask?itself?(or?a?Runnable?to?create?a?TimerTask?for) ? ?*?and?a?delay?plus?period.?Period?needs?to?be?specified; ?
  13. ?*?there?is?no?point?in?a?default?for?it. ? ?* ?
  14. ?*?<p>The?JDK?Timer?does?not?offer?more?sophisticated?scheduling ? ?*?options?such?as??cron?expressions.?Consider?using?Quartz?for ?
  15. ?*?such?advanced?needs. ? ?* ?
  16. ?*?<p>Note?that?Timer?uses?a?TimerTask?instance?that?is?shared ? ?*?between?repeated?executions,?in?contrast?to?Quartz?which ?
  17. ?*?instantiates?a?new?Job?for?each?execution. ? ?* ?
  18. ?*?@author?Juergen?Hoeller ? ?*?@since?19.02.2004 ?
  19. ?*?@see?java.util.TimerTask ? ?*?@see?java.util.Timer#schedule(TimerTask,?long,?long) ?
  20. ?*?@see?java.util.Timer#scheduleAtFixedRate(TimerTask,?long,?long) ? ?*/??
  21. public?class?ScheduledTimerTask?{ ?? ??
  22. ????private?TimerTask?timerTask; ?? ??
  23. ????private?long?delay?=?0; ?? ??
  24. ????private?long?period?=?0; ?? ??
  25. ????private?boolean?fixedRate?=?false; ?? ??
  26. ?? ????/** ?
  27. ?????*?Create?a?new?ScheduledTimerTask, ? ?????*?to?be?populated?via?bean?properties. ?
  28. ?????*?@see?#setTimerTask ? ?????*?@see?#setDelay ?
  29. ?????*?@see?#setPeriod ? ?????*?@see?#setFixedRate ?
  30. ?????*/?? ????public?ScheduledTimerTask()?{ ??
  31. ????} ?? ??
  32. ????/** ? ?????*?Create?a?new?ScheduledTimerTask,?with?default ?
  33. ?????*?one-time?execution?without?delay. ? ?????*?@param?timerTask?the?TimerTask?to?schedule ?
  34. ?????*/?? ????public?ScheduledTimerTask(TimerTask?timerTask)?{ ??
  35. ????????this.timerTask?=?timerTask; ?? ????} ??
  36. ?? ????/** ?
  37. ?????*?Create?a?new?ScheduledTimerTask,?with?default ? ?????*?one-time?execution?with?the?given?delay. ?
  38. ?????*?@param?timerTask?the?TimerTask?to?schedule ? ?????*?@param?delay?the?delay?before?starting?the?task?for?the?first?time?(ms) ?
  39. ?????*/?? ????public?ScheduledTimerTask(TimerTask?timerTask,?long?delay)?{ ??
  40. ????????this.timerTask?=?timerTask; ?? ????????this.delay?=?delay; ??
  41. ????} ?? ??
  42. ????/** ? ?????*?Create?a?new?ScheduledTimerTask. ?
  43. ?????*?@param?timerTask?the?TimerTask?to?schedule ? ?????*?@param?delay?the?delay?before?starting?the?task?for?the?first?time?(ms) ?
  44. ?????*?@param?period?the?period?between?repeated?task?executions?(ms) ? ?????*?@param?fixedRate?whether?to?schedule?as?fixed-rate?execution ?
  45. ?????*/?? ????public?ScheduledTimerTask(TimerTask?timerTask,?long?delay,?long?period,?boolean?fixedRate)?{ ??
  46. ????????this.timerTask?=?timerTask; ?? ????????this.delay?=?delay; ??
  47. ????????this.period?=?period; ?? ????????this.fixedRate?=?fixedRate; ??
  48. ????} ?? ??
  49. ????/** ? ?????*?Create?a?new?ScheduledTimerTask,?with?default ?
  50. ?????*?one-time?execution?without?delay. ? ?????*?@param?timerTask?the?Runnable?to?schedule?as?TimerTask ?
  51. ?????*/?? ????public?ScheduledTimerTask(Runnable?timerTask)?{ ??
  52. ????????setRunnable(timerTask); ?? ????} ??
  53. ?? ????/** ?
  54. ?????*?Create?a?new?ScheduledTimerTask,?with?default ? ?????*?one-time?execution?with?the?given?delay. ?
  55. ?????*?@param?timerTask?the?Runnable?to?schedule?as?TimerTask ? ?????*?@param?delay?the?delay?before?starting?the?task?for?the?first?time?(ms) ?
  56. ?????*/?? ????public?ScheduledTimerTask(Runnable?timerTask,?long?delay)?{ ??
  57. ????????setRunnable(timerTask); ?? ????????this.delay?=?delay; ??
  58. ????} ?? ??
  59. ????/** ? ?????*?Create?a?new?ScheduledTimerTask. ?
  60. ?????*?@param?timerTask?the?Runnable?to?schedule?as?TimerTask ? ?????*?@param?delay?the?delay?before?starting?the?task?for?the?first?time?(ms) ?
  61. ?????*?@param?period?the?period?between?repeated?task?executions?(ms) ? ?????*?@param?fixedRate?whether?to?schedule?as?fixed-rate?execution ?
  62. ?????*/?? ????public?ScheduledTimerTask(Runnable?timerTask,?long?delay,?long?period,?boolean?fixedRate)?{ ??
  63. ????????setRunnable(timerTask); ?? ????????this.delay?=?delay; ??
  64. ????????this.period?=?period; ?? ????????this.fixedRate?=?fixedRate; ??
  65. ????} ?? ??
  66. ?? ????/** ?
  67. ?????*?Set?the?Runnable?to?schedule?as?TimerTask. ? ?????*?@see?DelegatingTimerTask ?
  68. ?????*/?? ????public?void?setRunnable(Runnable?timerTask)?{ ??
  69. ????????this.timerTask?=?new?DelegatingTimerTask(timerTask); ?? ????} ??
  70. ?? ????/** ?
  71. ?????*?Set?the?TimerTask?to?schedule. ? ?????*/??
  72. ????public?void?setTimerTask(TimerTask?timerTask)?{ ?? ????????this.timerTask?=?timerTask; ??
  73. ????} ?? ??
  74. ????/** ? ?????*?Return?the?TimerTask?to?schedule. ?
  75. ?????*/?? ????public?TimerTask?getTimerTask()?{ ??
  76. ????????return?timerTask; ?? ????} ??
  77. ?? ????/** ?
  78. ?????*?Set?the?delay?before?starting?the?task?for?the?first?time, ? ?????*?in?milliseconds.?Default?is?0,?immediately?starting?the ?
  79. ?????*?task?after?successful?scheduling. ? ?????*/??
  80. ????public?void?setDelay(long?delay)?{ ?? ????????this.delay?=?delay; ??
  81. ????} ?? ??
  82. ????/** ? ?????*?Return?the?delay?before?starting?the?job?for?the?first?time. ?
  83. ?????*/?? ????public?long?getDelay()?{ ??
  84. ????????return?delay; ?? ????} ??
  85. ?? ????/** ?
  86. ?????*?Set?the?period?between?repeated?task?executions,?in?milliseconds. ? ?????*?Default?is?0,?leading?to?one-time?execution.?In?case?of?a?positive ?
  87. ?????*?value,?the?task?will?be?executed?repeatedly,?with?the?given?interval ? ?????*?inbetween?executions. ?
  88. ?????*?<p>Note?that?the?semantics?of?the?period?vary?between?fixed-rate ? ?????*?and?fixed-delay?execution. ?
  89. ?????*?@see?#setFixedRate ? ?????*/??
  90. ????public?void?setPeriod(long?period)?{ ?? ????????this.period?=?period; ??
  91. ????} ?? ??
  92. ????/** ? ?????*?Return?the?period?between?repeated?task?executions. ?
  93. ?????*/?? ????public?long?getPeriod()?{ ??
  94. ????????return?period; ?? ????} ??
  95. ?? ????/** ?
  96. ?????*?Set?whether?to?schedule?as?fixed-rate?execution,?rather?than ? ?????*?fixed-delay?execution.?Default?is?"false",?i.e.?fixed?delay. ?
  97. ?????*?<p>See?Timer?javadoc?for?details?on?those?execution?modes. ? ?????*?@see?java.util.Timer#schedule(TimerTask,?long,?long) ?
  98. ?????*?@see?java.util.Timer#scheduleAtFixedRate(TimerTask,?long,?long) ? ?????*/??
  99. ????public?void?setFixedRate(boolean?fixedRate)?{ ?? ????????this.fixedRate?=?fixedRate; ??
  100. ????} ?? ??
  101. ????/** ? ?????*?Return?whether?to?schedule?as?fixed-rate?execution. ?
  102. ?????*/?? ????public?boolean?isFixedRate()?{ ??
  103. ????????return?fixedRate; ?? ????} ??
  104. ?? } ??

?

真正运行的任务的类是:

java 代码
  1. /* ? ?*?Copyright?2002-2006?the?original?author?or?authors. ?
  2. ?* ? ?*?Licensed?under?the?Apache?License,?Version?2.0?(the?"License"); ?
  3. ?*?you?may?not?use?this?file?except?in?compliance?with?the?License. ? ?*?You?may?obtain?a?copy?of?the?License?at ?
  4. ?* ? ?*??????http://www.apache.org/licenses/LICENSE-2.0 ?
  5. ?* ? ?*?Unless?required?by?applicable?law?or?agreed?to?in?writing,?software ?
  6. ?*?distributed?under?the?License?is?distributed?on?an?"AS?IS"?BASIS, ? ?*?WITHOUT?WARRANTIES?OR?CONDITIONS?OF?ANY?KIND,?either?express?or?implied. ?
  7. ?*?See?the?License?for?the?specific?language?governing?permissions?and ? ?*?limitations?under?the?License. ?
  8. ?*/?? ??
  9. package?org.springframework.scheduling.timer; ?? ??
  10. import?java.util.Timer; ?? ??
  11. import?org.apache.commons.logging.Log; ?? import?org.apache.commons.logging.LogFactory; ??
  12. ?? import?org.springframework.beans.factory.DisposableBean; ??
  13. import?org.springframework.beans.factory.FactoryBean; ?? import?org.springframework.beans.factory.InitializingBean; ??
  14. ?? /** ?
  15. ?*?FactoryBean?that?sets?up?a?JDK?1.3+?Timer?and?exposes?it?for?bean?references. ? ?* ?
  16. ?*?<p>Allows?for?registration?of?ScheduledTimerTasks,?automatically?starting ? ?*?the?Timer?on?initialization?and?cancelling?it?on?destruction?of?the?context. ?
  17. ?*?In?scenarios?that?just?require?static?registration?of?tasks?at?startup, ? ?*?there?is?no?need?to?access?the?Timer?instance?itself?in?application?code. ?
  18. ?* ? ?*?<p>Note?that?Timer?uses?a?TimerTask?instance?that?is?shared?between ?
  19. ?*?repeated?executions,?in?contrast?to?Quartz?which?instantiates?a?new ? ?*?Job?for?each?execution. ?
  20. ?* ? ?*?@author?Juergen?Hoeller ?
  21. ?*?@since?19.02.2004 ? ?*?@see?ScheduledTimerTask ?
  22. ?*?@see?java.util.Timer ? ?*?@see?java.util.TimerTask ?
  23. ?*/?? public?class?TimerFactoryBean?implements?FactoryBean,?InitializingBean,?DisposableBean?{ ??
  24. ?? ????protected?final?Log?logger?=?LogFactory.getLog(getClass()); ??
  25. ?? ????private?ScheduledTimerTask[]?scheduledTimerTasks; ??
  26. ?? ????private?boolean?daemon?=?false; ??
  27. ?? ????private?Timer?timer; ??
  28. ?? ??
  29. ????/** ? ?????*?Register?a?list?of?ScheduledTimerTask?objects?with?the?Timer?that ?
  30. ?????*?this?FactoryBean?creates.?Depending?on?each?SchedulerTimerTask's ? ?????*?settings,?it?will?be?registered?via?one?of?Timer's?schedule?methods. ?
  31. ?????*?@see?java.util.Timer#schedule(java.util.TimerTask,?long) ? ?????*?@see?java.util.Timer#schedule(java.util.TimerTask,?long,?long) ?
  32. ?????*?@see?java.util.Timer#scheduleAtFixedRate(java.util.TimerTask,?long,?long) ? ?????*/??
  33. ????public?void?setScheduledTimerTasks(ScheduledTimerTask[]?scheduledTimerTasks)?{ ?? ????????this.scheduledTimerTasks?=?scheduledTimerTasks; ??
  34. ????} ?? ??
  35. ????/** ? ?????*?Set?whether?the?timer?should?use?a?daemon?thread, ?
  36. ?????*?just?executing?as?long?as?the?application?itself?is?running. ? ?????*?<p>Default?is?"false":?The?timer?will?automatically?get?cancelled?on ?
  37. ?????*?destruction?of?this?FactoryBean.?Hence,?if?the?application?shuts?down, ? ?????*?tasks?will?by?default?finish?their?execution.?Specify?"true"?for?eager ?
  38. ?????*?shutdown?of?threads?that?execute?tasks. ? ?????*?@see?java.util.Timer#Timer(boolean) ?
  39. ?????*/?? ????public?void?setDaemon(boolean?daemon)?{ ??
  40. ????????this.daemon?=?daemon; ?? ????} ??
  41. ?? ??
  42. ????public?void?afterPropertiesSet()?{ ?? ????????logger.info("Initializing?Timer"); ??
  43. ????????this.timer?=?createTimer(this.daemon); ?? ??
  44. ????????//?Register?all?ScheduledTimerTasks. ?? ????????if?(this.scheduledTimerTasks?!=?null)?{ ??
  45. ????????????for?(int?i?=?0;?i?<?this.scheduledTimerTasks.length;?i++)?{ ?? ????????????????ScheduledTimerTask?scheduledTask?=?this.scheduledTimerTasks[i]; ??
  46. ????????????????if?(scheduledTask.getPeriod()?>?0)?{ ?? ????????????????????//?repeated?task?execution ??
  47. ????????????????????if?(scheduledTask.isFixedRate())?{ ?? ????????????????????????this.timer.scheduleAtFixedRate( ??
  48. ????????????????????????????????scheduledTask.getTimerTask(),?scheduledTask.getDelay(),?scheduledTask.getPeriod()); ?? ????????????????????} ??
  49. ????????????????????else?{ ?? ????????????????????????this.timer.schedule( ??
  50. ????????????????????????????????scheduledTask.getTimerTask(),?scheduledTask.getDelay(),?scheduledTask.getPeriod()); ?? ????????????????????} ??
  51. ????????????????} ?? ????????????????else?{ ??
  52. ????????????????????//?One-time?task?execution. ?? ????????????????????this.timer.schedule(scheduledTask.getTimerTask(),?scheduledTask.getDelay()); ??
  53. ????????????????} ?? ????????????} ??
  54. ????????} ?? ????} ??
  55. ?? ????/** ?
  56. ?????*?Create?a?new?Timer?instance.?Called?by?<code>afterPropertiesSet</code>. ? ?????*?Can?be?overridden?in?subclasses?to?provide?custom?Timer?subclasses. ?
  57. ?????*?@param?daemon?whether?to?create?a?Timer?that?runs?as?daemon?thread ? ?????*?@return?a?new?Timer?instance ?
  58. ?????*?@see?#afterPropertiesSet() ? ?????*?@see?java.util.Timer#Timer(boolean) ?
  59. ?????*/?? ????protected?Timer?createTimer(boolean?daemon)?{ ??
  60. ????????return?new?Timer(daemon); ?? ????} ??
  61. ?? ??
  62. ????public?Object?getObject()?{ ?? ????????return?this.timer; ??
  63. ????} ?? ??
  64. ????public?Class?getObjectType()?{ ?? ????????return?Timer.class; ??
  65. ????} ?? ??
  66. ????public?boolean?isSingleton()?{ ?? ????????return?true; ??
  67. ????} ?? ??
  68. ?? ????/** ?
  69. ?????*?Cancel?the?Timer?on?bean?factory?shutdown,?stopping?all?scheduled?tasks. ? ?????*?@see?java.util.Timer#cancel() ?
  70. ?????*/?? ????public?void?destroy()?{ ??
  71. ????????logger.info("Cancelling?Timer"); ?? ????????this.timer.cancel(); ??
  72. ????} ?? ??
  73. } ??

?

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

读书人网 >软件架构设计

热点推荐