就拿新闻发布系统来说,如果新闻的数据更新太快,势必涉及一个问题,这些新闻不能由人工的去发布,应该让系统自己发布,这就需要用到定时定制任务了,以前定制任务无非就是设计一个Thread,并且设置运行时间片,让它到了那个时间执行一次,就ok了,让系统启动的时候启动它,想来也够简单的。不过有了spring,我想这事情就更简单了。
看看spring的配置文件,想来就只有这个配置文件了
上面三个配置文件中只有一个配置文件是涉及到您自己的class的,其他的都是spring的类。很简单吧
我们只需要涉及一个class让他继承java.util.TimerTask;
下面让我们来看看 spring的源代码
The JDK Timer does not offer more sophisticated scheduling
Note that Timer uses a TimerTask instance that is shared
Note that the semantics of the period vary between fixed-rate
See Timer javadoc for details on those execution modes.
说实话这个类也没什么,只是简单的包装了我们的timertask,里面也就只有几个属性,一个是时间片,一个是任务等。
真正运行我们的任务的类是:
Allows for registration of ScheduledTimerTasks, automatically starting
Note that Timer uses a TimerTask instance that is shared between
Default is "false": The timer will automatically get cancelled on
这个类就是运行我们任务的类了,我们可以定制N个任务,只需要塞到这里就ok了。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12467/viewspace-148295/ ,如需转载,请注明出处,否则将追究法律责任。