, DisposableBean { ................@Override
public void onApplicationEvent(ContextRefreshedEvent event) { if (event.getApplicationContext() == this.applicationContext) {
// Running in an ApplicationContext -> register tasks this late...
// giving other ContextRefreshedEvent listeners a chance to perform // their work at the same time (e.g. Spring Batch's job registration). finishRegistration(); }
}
private void finishRegistration() { if (this.scheduler != null) {
this.registrar.setScheduler(this.scheduler); }
.......................if (this.registrar.hasTasks() && this.registrar.getScheduler() == null) {
Assert.state(this.beanFactory != null, \"BeanFactory must be set to find scheduler by type\"); try {
// Search for TaskScheduler bean...
this.registrar.setTaskScheduler(resolveSchedulerBean(beanFactory, TaskScheduler.class, false)); }
catch (NoUniqueBeanDefinitionException ex) {
logger.debug(\"Could not find unique TaskScheduler bean\ try {
this.registrar.setTaskScheduler(resolveSchedulerBean(beanFactory, TaskScheduler.class, true)); }
catch (NoSuchBeanDefinitionException ex2) { if (logger.isInfoEnabled()) {
logger.info(\"More than one TaskScheduler bean exists within the context, and \" +
\"none is named 'taskScheduler'. Mark one of them as primary or name it 'taskScheduler' \" + \"(possibly as an alias); or implement the SchedulingConfigurer interface and call \" +
\"ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback: \" + ex.getBeanNamesFound()); } } }
catch (NoSuchBeanDefinitionException ex) {
logger.debug(\"Could not find default TaskScheduler bean\ // Search for ScheduledExecutorService bean next... try {
this.registrar.setScheduler(resolveSchedulerBean(beanFactory, ScheduledExecutorService.class, false)); }
catch (NoUniqueBeanDefinitionException ex2) {
logger.debug(\"Could not find unique ScheduledExecutorService bean\ try {
this.registrar.setScheduler(resolveSchedulerBean(beanFactory, ScheduledExecutorService.class, true)); }
catch (NoSuchBeanDefinitionException ex3) { if (logger.isInfoEnabled()) {
logger.info(\"More than one ScheduledExecutorService bean exists within the context, and \" + \"none is named 'taskScheduler'. Mark one of them as primary or name it 'taskScheduler' \" + \"(possibly as an alias); or implement the SchedulingConfigurer interface and call \" +
\"ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback: \" + ex2.getBeanNamesFound()); } } }
catch (NoSuchBeanDefinitionException ex2) {
logger.debug(\"Could not find default ScheduledExecutorService bean\ // Giving up -> falling back to default scheduler within the registrar...
logger.info(\"No TaskScheduler/ScheduledExecutorService bean found for scheduled processing\"); } } }
this.registrar.afterPropertiesSet(); }}
if (this.taskScheduler == null) {
this.localExecutor = Executors.newSingleThreadScheduledExecutor(); this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor);}
1.
Spring 定时任务执⾏原理实际使⽤的是 JDK ⾃带的 ScheduledExecutorService