SPR-8205 added support for a 'trigger' attribute (bean ref) on scheduled-task elements
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4441 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
parent
d0368e1f1c
commit
ac176da515
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2011 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.
|
||||
|
|
@ -53,6 +53,7 @@ public class ScheduledTasksBeanDefinitionParser extends AbstractSingleBeanDefini
|
|||
ManagedMap<RuntimeBeanReference, String> cronTaskMap = new ManagedMap<RuntimeBeanReference, String>();
|
||||
ManagedMap<RuntimeBeanReference, String> fixedDelayTaskMap = new ManagedMap<RuntimeBeanReference, String>();
|
||||
ManagedMap<RuntimeBeanReference, String> fixedRateTaskMap = new ManagedMap<RuntimeBeanReference, String>();
|
||||
ManagedMap<RuntimeBeanReference, RuntimeBeanReference> triggerTaskMap = new ManagedMap<RuntimeBeanReference, RuntimeBeanReference>();
|
||||
NodeList childNodes = element.getChildNodes();
|
||||
for (int i = 0; i < childNodes.getLength(); i++) {
|
||||
Node child = childNodes.item(i);
|
||||
|
|
@ -72,25 +73,35 @@ public class ScheduledTasksBeanDefinitionParser extends AbstractSingleBeanDefini
|
|||
|
||||
RuntimeBeanReference runnableBeanRef = new RuntimeBeanReference(
|
||||
createRunnableBean(ref, method, taskElement, parserContext));
|
||||
|
||||
String cronAttribute = taskElement.getAttribute("cron");
|
||||
if (StringUtils.hasText(cronAttribute)) {
|
||||
String fixedDelayAttribute = taskElement.getAttribute("fixed-delay");
|
||||
String fixedRateAttribute = taskElement.getAttribute("fixed-rate");
|
||||
String triggerAttribute = taskElement.getAttribute("trigger");
|
||||
|
||||
boolean hasCronAttribute = StringUtils.hasText(cronAttribute);
|
||||
boolean hasFixedDelayAttribute = StringUtils.hasText(fixedDelayAttribute);
|
||||
boolean hasFixedRateAttribute = StringUtils.hasText(fixedRateAttribute);
|
||||
boolean hasTriggerAttribute = StringUtils.hasText(triggerAttribute);
|
||||
|
||||
if (!(hasCronAttribute | hasFixedDelayAttribute | hasFixedRateAttribute | hasTriggerAttribute)) {
|
||||
parserContext.getReaderContext().error(
|
||||
"exactly one of the 'cron', 'fixed-delay', 'fixed-rate', or 'trigger' attributes is required", taskElement);
|
||||
// Continue with the possible next task element
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hasCronAttribute) {
|
||||
cronTaskMap.put(runnableBeanRef, cronAttribute);
|
||||
}
|
||||
else {
|
||||
String fixedDelayAttribute = taskElement.getAttribute("fixed-delay");
|
||||
if (StringUtils.hasText(fixedDelayAttribute)) {
|
||||
fixedDelayTaskMap.put(runnableBeanRef, fixedDelayAttribute);
|
||||
}
|
||||
else {
|
||||
String fixedRateAttribute = taskElement.getAttribute("fixed-rate");
|
||||
if (!StringUtils.hasText(fixedRateAttribute)) {
|
||||
parserContext.getReaderContext().error(
|
||||
"One of 'cron', 'fixed-delay', or 'fixed-rate' is required", taskElement);
|
||||
// Continue with the possible next task element
|
||||
continue;
|
||||
}
|
||||
fixedRateTaskMap.put(runnableBeanRef, fixedRateAttribute);
|
||||
}
|
||||
if (hasFixedDelayAttribute) {
|
||||
fixedDelayTaskMap.put(runnableBeanRef, fixedDelayAttribute);
|
||||
}
|
||||
if (hasFixedRateAttribute) {
|
||||
fixedRateTaskMap.put(runnableBeanRef, fixedRateAttribute);
|
||||
}
|
||||
if (hasTriggerAttribute) {
|
||||
triggerTaskMap.put(runnableBeanRef, new RuntimeBeanReference(triggerAttribute));
|
||||
}
|
||||
}
|
||||
String schedulerRef = element.getAttribute("scheduler");
|
||||
|
|
@ -100,6 +111,7 @@ public class ScheduledTasksBeanDefinitionParser extends AbstractSingleBeanDefini
|
|||
builder.addPropertyValue("cronTasks", cronTaskMap);
|
||||
builder.addPropertyValue("fixedDelayTasks", fixedDelayTaskMap);
|
||||
builder.addPropertyValue("fixedRateTasks", fixedRateTaskMap);
|
||||
builder.addPropertyValue("triggerTasks", triggerTaskMap);
|
||||
}
|
||||
|
||||
private boolean isScheduledElement(Node node, ParserContext parserContext) {
|
||||
|
|
|
|||
|
|
@ -216,6 +216,13 @@
|
|||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="trigger" type="xsd:string" use="optional">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
A reference to a bean that implements the Trigger interface.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="ref" type="xsd:string" use="required">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ package org.springframework.scheduling.config;
|
|||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
|
@ -27,6 +28,8 @@ import org.junit.Test;
|
|||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.scheduling.Trigger;
|
||||
import org.springframework.scheduling.TriggerContext;
|
||||
import org.springframework.scheduling.support.ScheduledMethodRunnable;
|
||||
|
||||
/**
|
||||
|
|
@ -98,12 +101,28 @@ public class ScheduledTasksBeanDefinitionParserTests {
|
|||
assertEquals("*/4 * 9-17 * * MON-FRI", expression);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void triggerTasks() {
|
||||
Map<Runnable, Trigger> tasks = (Map<Runnable, Trigger>) new DirectFieldAccessor(
|
||||
this.registrar).getPropertyValue("triggerTasks");
|
||||
assertEquals(1, tasks.size());
|
||||
Trigger trigger = tasks.values().iterator().next();
|
||||
assertEquals(TestTrigger.class, trigger.getClass());
|
||||
}
|
||||
|
||||
|
||||
static class TestBean {
|
||||
|
||||
public void test() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static class TestTrigger implements Trigger {
|
||||
|
||||
public Date nextExecutionTime(TriggerContext triggerContext) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,10 +12,13 @@
|
|||
<task:scheduled ref="testBean" method="test" fixed-rate="2000"/>
|
||||
<task:scheduled ref="testBean" method="test" fixed-delay="3000"/>
|
||||
<task:scheduled ref="testBean" method="test" cron="*/4 * 9-17 * * MON-FRI"/>
|
||||
<task:scheduled ref="testBean" method="test" trigger="customTrigger"/>
|
||||
</task:scheduled-tasks>
|
||||
|
||||
<task:scheduler id="testScheduler"/>
|
||||
|
||||
<bean id="testBean" class="org.springframework.scheduling.config.ScheduledTasksBeanDefinitionParserTests$TestBean"/>
|
||||
|
||||
<bean id="customTrigger" class="org.springframework.scheduling.config.ScheduledTasksBeanDefinitionParserTests$TestTrigger"/>
|
||||
|
||||
</beans>
|
||||
|
|
|
|||
Loading…
Reference in New Issue