Skip to content

Commit 72420c7

Browse files
committed
SPR-8205 added support for a 'trigger' attribute (bean ref) on scheduled-task elements
1 parent da41c9b commit 72420c7

File tree

4 files changed

+58
-17
lines changed

4 files changed

+58
-17
lines changed

org.springframework.context/src/main/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParser.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,6 +53,7 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
5353
ManagedMap<RuntimeBeanReference, String> cronTaskMap = new ManagedMap<RuntimeBeanReference, String>();
5454
ManagedMap<RuntimeBeanReference, String> fixedDelayTaskMap = new ManagedMap<RuntimeBeanReference, String>();
5555
ManagedMap<RuntimeBeanReference, String> fixedRateTaskMap = new ManagedMap<RuntimeBeanReference, String>();
56+
ManagedMap<RuntimeBeanReference, RuntimeBeanReference> triggerTaskMap = new ManagedMap<RuntimeBeanReference, RuntimeBeanReference>();
5657
NodeList childNodes = element.getChildNodes();
5758
for (int i = 0; i < childNodes.getLength(); i++) {
5859
Node child = childNodes.item(i);
@@ -72,25 +73,35 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
7273

7374
RuntimeBeanReference runnableBeanRef = new RuntimeBeanReference(
7475
createRunnableBean(ref, method, taskElement, parserContext));
76+
7577
String cronAttribute = taskElement.getAttribute("cron");
76-
if (StringUtils.hasText(cronAttribute)) {
78+
String fixedDelayAttribute = taskElement.getAttribute("fixed-delay");
79+
String fixedRateAttribute = taskElement.getAttribute("fixed-rate");
80+
String triggerAttribute = taskElement.getAttribute("trigger");
81+
82+
boolean hasCronAttribute = StringUtils.hasText(cronAttribute);
83+
boolean hasFixedDelayAttribute = StringUtils.hasText(fixedDelayAttribute);
84+
boolean hasFixedRateAttribute = StringUtils.hasText(fixedRateAttribute);
85+
boolean hasTriggerAttribute = StringUtils.hasText(triggerAttribute);
86+
87+
if (!(hasCronAttribute | hasFixedDelayAttribute | hasFixedRateAttribute | hasTriggerAttribute)) {
88+
parserContext.getReaderContext().error(
89+
"exactly one of the 'cron', 'fixed-delay', 'fixed-rate', or 'trigger' attributes is required", taskElement);
90+
// Continue with the possible next task element
91+
continue;
92+
}
93+
94+
if (hasCronAttribute) {
7795
cronTaskMap.put(runnableBeanRef, cronAttribute);
7896
}
79-
else {
80-
String fixedDelayAttribute = taskElement.getAttribute("fixed-delay");
81-
if (StringUtils.hasText(fixedDelayAttribute)) {
82-
fixedDelayTaskMap.put(runnableBeanRef, fixedDelayAttribute);
83-
}
84-
else {
85-
String fixedRateAttribute = taskElement.getAttribute("fixed-rate");
86-
if (!StringUtils.hasText(fixedRateAttribute)) {
87-
parserContext.getReaderContext().error(
88-
"One of 'cron', 'fixed-delay', or 'fixed-rate' is required", taskElement);
89-
// Continue with the possible next task element
90-
continue;
91-
}
92-
fixedRateTaskMap.put(runnableBeanRef, fixedRateAttribute);
93-
}
97+
if (hasFixedDelayAttribute) {
98+
fixedDelayTaskMap.put(runnableBeanRef, fixedDelayAttribute);
99+
}
100+
if (hasFixedRateAttribute) {
101+
fixedRateTaskMap.put(runnableBeanRef, fixedRateAttribute);
102+
}
103+
if (hasTriggerAttribute) {
104+
triggerTaskMap.put(runnableBeanRef, new RuntimeBeanReference(triggerAttribute));
94105
}
95106
}
96107
String schedulerRef = element.getAttribute("scheduler");
@@ -100,6 +111,7 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
100111
builder.addPropertyValue("cronTasks", cronTaskMap);
101112
builder.addPropertyValue("fixedDelayTasks", fixedDelayTaskMap);
102113
builder.addPropertyValue("fixedRateTasks", fixedRateTaskMap);
114+
builder.addPropertyValue("triggerTasks", triggerTaskMap);
103115
}
104116

105117
private boolean isScheduledElement(Node node, ParserContext parserContext) {

org.springframework.context/src/main/resources/org/springframework/scheduling/config/spring-task-3.1.xsd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@
216216
]]></xsd:documentation>
217217
</xsd:annotation>
218218
</xsd:attribute>
219+
<xsd:attribute name="trigger" type="xsd:string" use="optional">
220+
<xsd:annotation>
221+
<xsd:documentation><![CDATA[
222+
A reference to a bean that implements the Trigger interface.
223+
]]></xsd:documentation>
224+
</xsd:annotation>
225+
</xsd:attribute>
219226
<xsd:attribute name="ref" type="xsd:string" use="required">
220227
<xsd:annotation>
221228
<xsd:documentation><![CDATA[

org.springframework.context/src/test/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParserTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.lang.reflect.Method;
2020
import java.util.Collection;
21+
import java.util.Date;
2122
import java.util.Map;
2223

2324
import static org.junit.Assert.*;
@@ -27,6 +28,8 @@
2728
import org.springframework.beans.DirectFieldAccessor;
2829
import org.springframework.context.ApplicationContext;
2930
import org.springframework.context.support.ClassPathXmlApplicationContext;
31+
import org.springframework.scheduling.Trigger;
32+
import org.springframework.scheduling.TriggerContext;
3033
import org.springframework.scheduling.support.ScheduledMethodRunnable;
3134

3235
/**
@@ -98,12 +101,28 @@ public void cronTasks() {
98101
assertEquals("*/4 * 9-17 * * MON-FRI", expression);
99102
}
100103

104+
@Test
105+
public void triggerTasks() {
106+
Map<Runnable, Trigger> tasks = (Map<Runnable, Trigger>) new DirectFieldAccessor(
107+
this.registrar).getPropertyValue("triggerTasks");
108+
assertEquals(1, tasks.size());
109+
Trigger trigger = tasks.values().iterator().next();
110+
assertEquals(TestTrigger.class, trigger.getClass());
111+
}
112+
101113

102114
static class TestBean {
103115

104116
public void test() {
105117
}
118+
}
119+
106120

121+
static class TestTrigger implements Trigger {
122+
123+
public Date nextExecutionTime(TriggerContext triggerContext) {
124+
return null;
125+
}
107126
}
108127

109128
}

org.springframework.context/src/test/resources/org/springframework/scheduling/config/scheduledTasksContext.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
<task:scheduled ref="testBean" method="test" fixed-rate="2000"/>
1313
<task:scheduled ref="testBean" method="test" fixed-delay="3000"/>
1414
<task:scheduled ref="testBean" method="test" cron="*/4 * 9-17 * * MON-FRI"/>
15+
<task:scheduled ref="testBean" method="test" trigger="customTrigger"/>
1516
</task:scheduled-tasks>
1617

1718
<task:scheduler id="testScheduler"/>
1819

1920
<bean id="testBean" class="org.springframework.scheduling.config.ScheduledTasksBeanDefinitionParserTests$TestBean"/>
2021

22+
<bean id="customTrigger" class="org.springframework.scheduling.config.ScheduledTasksBeanDefinitionParserTests$TestTrigger"/>
23+
2124
</beans>

0 commit comments

Comments
 (0)