Browse Source

Aligned XML scheduled-task elements with @Scheduled in terms of kicking in after context refresh

Issue: SPR-9231
pull/218/head
Juergen Hoeller 12 years ago committed by unknown
parent
commit
0efdd3d566
  1. 65
      spring-context/src/main/java/org/springframework/scheduling/config/ContextLifecycleScheduledTaskRegistrar.java
  2. 12
      spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java
  3. 6
      spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParser.java

65
spring-context/src/main/java/org/springframework/scheduling/config/ContextLifecycleScheduledTaskRegistrar.java

@ -0,0 +1,65 @@ @@ -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<ContextRefreshedEvent> {
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();
}
}

12
spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java

@ -1,5 +1,5 @@ @@ -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 @@ -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) {

6
spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParser.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 @@ -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

Loading…
Cancel
Save