RSS
热门关键字:  Linux  图形  项目管理  LAMP  java
当前位置 : 主页>开源技术>Java技术>列表

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

来源:开源社区 作者:sherman 时间:2007-10-01 点击:

相信做软件的朋友都有这样的经历,我的软件是不是少了点什么东西呢?比如定时任务啊, 字串3

就拿新闻发布系统来说,如果新闻的数据更新太快,势必涉及一个问题,这些新闻不能由人工的去发布,应该让系统自己发布,这就需要用到定时定制任务了,以前定制任务无非就是设计一个Thread,并且设置运行时间片,让它到了那个时间执行一次,就ok了,让系统启动的时候启动它,想来也够简单的。不过有了 spring,我想这事情就更简单了。 字串3

看看spring的配置文件,想来就只有这个配置文件了

字串9

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

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

字串1

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

字串2

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

字串2

字串5

字串2

字串8

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

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

    字串8

  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 ;
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册