提交 72420c79 编写于 作者: M Mark Fisher

SPR-8205 added support for a 'trigger' attribute (bean ref) on scheduled-task elements

上级 da41c9bb
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -53,6 +53,7 @@ public class ScheduledTasksBeanDefinitionParser extends AbstractSingleBeanDefini ...@@ -53,6 +53,7 @@ public class ScheduledTasksBeanDefinitionParser extends AbstractSingleBeanDefini
ManagedMap<RuntimeBeanReference, String> cronTaskMap = new ManagedMap<RuntimeBeanReference, String>(); ManagedMap<RuntimeBeanReference, String> cronTaskMap = new ManagedMap<RuntimeBeanReference, String>();
ManagedMap<RuntimeBeanReference, String> fixedDelayTaskMap = new ManagedMap<RuntimeBeanReference, String>(); ManagedMap<RuntimeBeanReference, String> fixedDelayTaskMap = new ManagedMap<RuntimeBeanReference, String>();
ManagedMap<RuntimeBeanReference, String> fixedRateTaskMap = new ManagedMap<RuntimeBeanReference, String>(); ManagedMap<RuntimeBeanReference, String> fixedRateTaskMap = new ManagedMap<RuntimeBeanReference, String>();
ManagedMap<RuntimeBeanReference, RuntimeBeanReference> triggerTaskMap = new ManagedMap<RuntimeBeanReference, RuntimeBeanReference>();
NodeList childNodes = element.getChildNodes(); NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) { for (int i = 0; i < childNodes.getLength(); i++) {
Node child = childNodes.item(i); Node child = childNodes.item(i);
...@@ -72,25 +73,35 @@ public class ScheduledTasksBeanDefinitionParser extends AbstractSingleBeanDefini ...@@ -72,25 +73,35 @@ public class ScheduledTasksBeanDefinitionParser extends AbstractSingleBeanDefini
RuntimeBeanReference runnableBeanRef = new RuntimeBeanReference( RuntimeBeanReference runnableBeanRef = new RuntimeBeanReference(
createRunnableBean(ref, method, taskElement, parserContext)); createRunnableBean(ref, method, taskElement, parserContext));
String cronAttribute = taskElement.getAttribute("cron"); 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); cronTaskMap.put(runnableBeanRef, cronAttribute);
} }
else { if (hasFixedDelayAttribute) {
String fixedDelayAttribute = taskElement.getAttribute("fixed-delay"); fixedDelayTaskMap.put(runnableBeanRef, fixedDelayAttribute);
if (StringUtils.hasText(fixedDelayAttribute)) { }
fixedDelayTaskMap.put(runnableBeanRef, fixedDelayAttribute); if (hasFixedRateAttribute) {
} fixedRateTaskMap.put(runnableBeanRef, fixedRateAttribute);
else { }
String fixedRateAttribute = taskElement.getAttribute("fixed-rate"); if (hasTriggerAttribute) {
if (!StringUtils.hasText(fixedRateAttribute)) { triggerTaskMap.put(runnableBeanRef, new RuntimeBeanReference(triggerAttribute));
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);
}
} }
} }
String schedulerRef = element.getAttribute("scheduler"); String schedulerRef = element.getAttribute("scheduler");
...@@ -100,6 +111,7 @@ public class ScheduledTasksBeanDefinitionParser extends AbstractSingleBeanDefini ...@@ -100,6 +111,7 @@ public class ScheduledTasksBeanDefinitionParser extends AbstractSingleBeanDefini
builder.addPropertyValue("cronTasks", cronTaskMap); builder.addPropertyValue("cronTasks", cronTaskMap);
builder.addPropertyValue("fixedDelayTasks", fixedDelayTaskMap); builder.addPropertyValue("fixedDelayTasks", fixedDelayTaskMap);
builder.addPropertyValue("fixedRateTasks", fixedRateTaskMap); builder.addPropertyValue("fixedRateTasks", fixedRateTaskMap);
builder.addPropertyValue("triggerTasks", triggerTaskMap);
} }
private boolean isScheduledElement(Node node, ParserContext parserContext) { private boolean isScheduledElement(Node node, ParserContext parserContext) {
......
...@@ -216,6 +216,13 @@ ...@@ -216,6 +216,13 @@
]]></xsd:documentation> ]]></xsd:documentation>
</xsd:annotation> </xsd:annotation>
</xsd:attribute> </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:attribute name="ref" type="xsd:string" use="required">
<xsd:annotation> <xsd:annotation>
<xsd:documentation><![CDATA[ <xsd:documentation><![CDATA[
......
...@@ -18,6 +18,7 @@ package org.springframework.scheduling.config; ...@@ -18,6 +18,7 @@ package org.springframework.scheduling.config;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Collection; import java.util.Collection;
import java.util.Date;
import java.util.Map; import java.util.Map;
import static org.junit.Assert.*; import static org.junit.Assert.*;
...@@ -27,6 +28,8 @@ import org.junit.Test; ...@@ -27,6 +28,8 @@ import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.support.ScheduledMethodRunnable; import org.springframework.scheduling.support.ScheduledMethodRunnable;
/** /**
...@@ -98,12 +101,28 @@ public class ScheduledTasksBeanDefinitionParserTests { ...@@ -98,12 +101,28 @@ public class ScheduledTasksBeanDefinitionParserTests {
assertEquals("*/4 * 9-17 * * MON-FRI", expression); 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 { static class TestBean {
public void test() { public void test() {
} }
}
static class TestTrigger implements Trigger {
public Date nextExecutionTime(TriggerContext triggerContext) {
return null;
}
} }
} }
...@@ -12,10 +12,13 @@ ...@@ -12,10 +12,13 @@
<task:scheduled ref="testBean" method="test" fixed-rate="2000"/> <task:scheduled ref="testBean" method="test" fixed-rate="2000"/>
<task:scheduled ref="testBean" method="test" fixed-delay="3000"/> <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" cron="*/4 * 9-17 * * MON-FRI"/>
<task:scheduled ref="testBean" method="test" trigger="customTrigger"/>
</task:scheduled-tasks> </task:scheduled-tasks>
<task:scheduler id="testScheduler"/> <task:scheduler id="testScheduler"/>
<bean id="testBean" class="org.springframework.scheduling.config.ScheduledTasksBeanDefinitionParserTests$TestBean"/> <bean id="testBean" class="org.springframework.scheduling.config.ScheduledTasksBeanDefinitionParserTests$TestBean"/>
<bean id="customTrigger" class="org.springframework.scheduling.config.ScheduledTasksBeanDefinitionParserTests$TestTrigger"/>
</beans> </beans>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册