From 0efdd3d5668115b5463c119392e93dc830c9de60 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 22 Jan 2013 22:10:29 +0100 Subject: [PATCH] Aligned XML scheduled-task elements with @Scheduled in terms of kicking in after context refresh Issue: SPR-9231 --- ...ontextLifecycleScheduledTaskRegistrar.java | 65 +++++++++++++++++++ .../config/ScheduledTaskRegistrar.java | 12 +++- .../ScheduledTasksBeanDefinitionParser.java | 6 +- 3 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 spring-context/src/main/java/org/springframework/scheduling/config/ContextLifecycleScheduledTaskRegistrar.java diff --git a/spring-context/src/main/java/org/springframework/scheduling/config/ContextLifecycleScheduledTaskRegistrar.java b/spring-context/src/main/java/org/springframework/scheduling/config/ContextLifecycleScheduledTaskRegistrar.java new file mode 100644 index 00000000000..eacf7ed25c1 --- /dev/null +++ b/spring-context/src/main/java/org/springframework/scheduling/config/ContextLifecycleScheduledTaskRegistrar.java @@ -0,0 +1,65 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.scheduling.config; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextRefreshedEvent; + +/** + * {@link ScheduledTaskRegistrar} subclass that redirect the actual scheduling + * of tasks to the {@link ContextRefreshedEvent} callback. Falls back to regular + * {@link ScheduledTaskRegistrar} behavior when not running within an ApplicationContext. + * + * @author Juergen Hoeller + * @since 3.2.1 + */ +public class ContextLifecycleScheduledTaskRegistrar extends ScheduledTaskRegistrar + implements ApplicationContextAware, ApplicationListener { + + private ApplicationContext applicationContext; + + + public void setApplicationContext(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + + /** + * If we're running within an ApplicationContext, don't schedule the tasks + * right here; wait for this context's ContextRefreshedEvent instead. + */ + @Override + public void afterPropertiesSet() { + if (this.applicationContext == null) { + scheduleTasks(); + } + } + + /** + * Actually schedule the tasks at the right time of the context lifecycle, + * if we're running within an ApplicationContext. + */ + public void onApplicationEvent(ContextRefreshedEvent event) { + if (event.getApplicationContext() != this.applicationContext) { + return; + } + scheduleTasks(); + } + +} diff --git a/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java b/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java index 592fd033c13..2efd26cea9e 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java +++ b/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -274,11 +274,19 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean (this.triggerTasks != null && !this.triggerTasks.isEmpty()); } + + /** + * Calls {@link #scheduleTasks()} at bean construction time. + */ + public void afterPropertiesSet() { + scheduleTasks(); + } + /** * Schedule all registered tasks against the underlying {@linkplain * #setTaskScheduler(TaskScheduler) task scheduler}. */ - public void afterPropertiesSet() { + protected void scheduleTasks() { long now = System.currentTimeMillis(); if (this.taskScheduler == null) { diff --git a/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParser.java b/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParser.java index a892eb414b1..8f6963614f4 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParser.java +++ b/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,8 +38,10 @@ import org.w3c.dom.NodeList; public class ScheduledTasksBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { private static final String ELEMENT_SCHEDULED = "scheduled"; + private static final long ZERO_INITIAL_DELAY = 0; + @Override protected boolean shouldGenerateId() { return true; @@ -47,7 +49,7 @@ public class ScheduledTasksBeanDefinitionParser extends AbstractSingleBeanDefini @Override protected String getBeanClassName(Element element) { - return "org.springframework.scheduling.config.ScheduledTaskRegistrar"; + return "org.springframework.scheduling.config.ContextLifecycleScheduledTaskRegistrar"; } @Override