快速业务通道

彻底理解Spring的定制任务 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-22
t just require static registration of tasks at startup, * there is no need to access the Timer instance itself in application code. * *

Note that Timer uses a TimerTask instance that is shared between

* repeated executions,in contrast to Quartz which instantiates a new * Job for each execution. * * @author Juergen Hoeller * @since 19.02.2004 * @see ScheduledTimerTask * @see java.util.Timer * @see java.util.TimerTask */ public class TimerFactoryBean implements FactoryBean,InitializingBean,DisposableBean { protected  final Log logger = LogFactory.getLog(getClass()); private ScheduledTimerTask[] scheduledTimerTasks; private  boolean daemon = false ; private Timer timer; /** * Register a list of ScheduledTimerTask objects with the Timer that * this FactoryBean creates. Depending on each SchedulerTimerTask''s  * settings,it will be registered via one of Timer''s schedule methods. * @see java.util.Timer#schedule(java.util.TimerTask,long) * @see java.util.Timer#schedule(java.util.TimerTask,long,long) * @see java.util.Timer#scheduleAtFixedRate(java.util.TimerTask,long,long) */ public  void setScheduledTimerTasks(ScheduledTimerTask[] scheduledTimerTasks) { this .scheduledTimerTasks = scheduledTimerTasks; } /** * Set whether the timer should use a daemon thread, * just executing as long as the application itself is running. *

Default is "false": The timer will automatically get cancelled on

* destruction of this FactoryBean. Hence,if the application shuts down, * tasks will by default finish their execution. Specify "true" for eager * shutdown of threads that execute tasks. * @see java.util.Timer#Timer(boolean) */ public  void setDaemon( boolean daemon) { this .daemon = daemon; } public  void afterPropertiesSet() { logger.info( "Initializing Timer" ); this .timer = createTimer( this .daemon); // Register all ScheduledTimerTasks. if ( this .scheduledTimerTasks != null ) { for ( int i = 0 ; i < this .scheduledTimerTasks.length; i++) { ScheduledTimerTask scheduledTask = this .scheduledTimerTasks[i]; if (scheduledTask.getPeriod() > 0 ) { // repeated task execution if (scheduledTask.isFixedRate()) { this .timer.scheduleAtFixedRate( scheduledTask.getTimerTask(),scheduledTask.getDelay(), scheduledTask.getPeriod()); } else { this .timer.schedule( scheduledTask.getTimerTask(),scheduledTask.getDelay(), scheduledTask.getPeriod()); } } else { // One-time task execution. this .timer.schedule(scheduledTask.getTimerTask(), scheduledTask.getDelay()); } } } } /** * Create a new Timer instance. Called by afterPropertiesSet. * Can be overridden in subclasses to provide custom Timer subclasses. * @param daemon whether to create a Timer that runs as daemon thread * @return a new Timer instance * @see #afterPropertiesSet() * @see java.util.Timer#Timer(boolean) */ protected Timer createTimer( boolean daemon) { return  new Timer(daemon); } public Object getObject() { return  this .timer; } public Class getObj

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号