解析spring schedule (转)

Spring在schedule这块支持JDK Timer、concurrent、quartz三种,这三种任务调度方案在实现机制和调用方法上都不同,但spring通过对其包装,使得基于spring能用统一的配置和编码风格来使用这三种schedule方案。

总得来说这三种schedule都是基于scheduler->trigger->job的基本流程,因此spring通过TimerFactoryBean、ScheduledExecutorFactoryBean和SchedulerFactoryBean分别实现JDK Timer、concurrent和quartz的基本流程。主要逻辑如下代码所示:

TimerFactoryBean

view plaincopy to clipboardprint?
public void afterPropertiesSet() {
logger.info("Initializing Timer");
this.timer = createTimer(this.beanName, this.daemon);
// Register specified ScheduledTimerTasks, if necessary.
if (!ObjectUtils.isEmpty(this.scheduledTimerTasks)) {
//注册task和timer registerTasks(this.scheduledTimerTasks, this.timer);
}
}
public void afterPropertiesSet() {
logger.info("Initializing Timer");
this.timer = createTimer(this.beanName, this.daemon);
// Register specified ScheduledTimerTasks, if necessary.
if (!ObjectUtils.isEmpty(this.scheduledTimerTasks)) {
//注册task和timer registerTasks(this.scheduledTimerTasks, this.timer);
}
}

ScheduledExecutorFactoryBean

view plaincopy to clipboardprint?
public void afterPropertiesSet() {
if (logger.isInfoEnabled()) {
logger.info("Initializing ScheduledExecutorService" +
(this.beanName != null ? " '" + this.beanName + "'" : ""));
}
ScheduledExecutorService executor =
createExecutor(this.poolSize, this.threadFactory, this.rejectedExecutionHandler);
// Register specified ScheduledExecutorTasks, if necessary.
if (!ObjectUtils.isEmpty(this.scheduledExecutorTasks)) {
registerTasks(this.scheduledExecutorTasks, executor);
}
// Wrap executor with an unconfigurable decorator.
this.executor = (this.exposeUnconfigurableExecutor ?
Executors.unconfigurableScheduledExecutorService(executor) : executor);
}
public void afterPropertiesSet() {
if (logger.isInfoEnabled()) {
logger.info("Initializing ScheduledExecutorService" +
(this.beanName != null ? " '" + this.beanName + "'" : ""));
}
ScheduledExecutorService executor =
createExecutor(this.poolSize, this.threadFactory, this.rejectedExecutionHandler);
// Register specified ScheduledExecutorTasks, if necessary.
if (!ObjectUtils.isEmpty(this.scheduledExecutorTasks)) {
registerTasks(this.scheduledExecutorTasks, executor);
}
// Wrap executor with an unconfigurable decorator.
this.executor = (this.exposeUnconfigurableExecutor ?
Executors.unconfigurableScheduledExecutorService(executor) : executor);
}

SchedulerFactoryBean

view plaincopy to clipboardprint?
public void afterPropertiesSet() throws Exception {
.....
// 初始化sceduler
initSchedulerFactory(schedulerFactory);
this.scheduler = createScheduler(schedulerFactory, this.schedulerName);
//注册listener、trigger和job
registerListeners();
registerJobsAndTriggers();
......
//启动scheduler
startScheduler(this.scheduler, this.startupDelay);
......
}
}
public void afterPropertiesSet() throws Exception {
.....
// 初始化sceduler
initSchedulerFactory(schedulerFactory);
this.scheduler = createScheduler(schedulerFactory, this.schedulerName);
//注册listener、trigger和job
registerListeners();
registerJobsAndTriggers();
......
//启动scheduler
startScheduler(this.scheduler, this.startupDelay);
......
}
}

顺着scheduler->trigger->job的思路,Spring又分别对JDK Timer、concurrent、quartz的trigger进行了封装,暴露出时间调度的配置参数,三种封装类分别为ScheduledTimerTask、ScheduledExecutorTask和CronTriggerBean+SimpleTriggerBean。三种trigger封装类分别根据实现机制的特点暴露出时间调度配置并串联起scheduler和具体job任务


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cutesource/archive/2009/11/29/4900020.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值