Triggers are now created as BeanDefinitions so that property placeholder values will be applied when parsing with the namespace support (INT-359).

This commit is contained in:
Mark Fisher
2008-11-18 22:04:39 +00:00
parent 5c752a86ec
commit 1e1b2ce9b8
6 changed files with 29 additions and 32 deletions

View File

@@ -79,7 +79,7 @@ public abstract class AnnotationConfigUtils {
public static Trigger parseTriggerFromPollerAnnotation(Poller pollerAnnotation) {
IntervalTrigger trigger = new IntervalTrigger(
pollerAnnotation.interval(), pollerAnnotation.timeUnit());
trigger.setInitialDelay(pollerAnnotation.initialDelay(), pollerAnnotation.timeUnit());
trigger.setInitialDelay(pollerAnnotation.initialDelay());
trigger.setFixedRate(pollerAnnotation.fixedRate());
return trigger;
}

View File

@@ -104,7 +104,7 @@ public abstract class AbstractConsumerEndpointParser extends AbstractSingleBeanD
builder.addPropertyValue("inputChannelName", inputChannelName);
Element pollerElement = DomUtils.getChildElementByTagName(element, POLLER_ELEMENT);
if (pollerElement != null) {
IntegrationNamespaceUtils.configureTrigger(pollerElement, builder);
IntegrationNamespaceUtils.configureTrigger(pollerElement, builder, parserContext);
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, pollerElement, "max-messages-per-poll");
Element txElement = DomUtils.getChildElementByTagName(pollerElement, "transactional");
if (txElement != null) {

View File

@@ -40,7 +40,7 @@ public abstract class AbstractOutboundChannelAdapterParser extends AbstractChann
builder.addConstructorArgReference(this.parseAndRegisterConsumer(element, parserContext));
if (pollerElement != null) {
Assert.hasText(channelName, "outbound channel adapter with a 'poller' requires a 'channel' to poll");
IntegrationNamespaceUtils.configureTrigger(pollerElement, builder);
IntegrationNamespaceUtils.configureTrigger(pollerElement, builder, parserContext);
Element txElement = DomUtils.getChildElementByTagName(pollerElement, "transactional");
if (txElement != null) {
IntegrationNamespaceUtils.configureTransactionAttributes(txElement, builder);

View File

@@ -42,7 +42,7 @@ public abstract class AbstractPollingInboundChannelAdapterParser extends Abstrac
adapterBuilder.addPropertyReference("source", source);
adapterBuilder.addPropertyReference("outputChannel", channelName);
if (pollerElement != null) {
IntegrationNamespaceUtils.configureTrigger(pollerElement, adapterBuilder);
IntegrationNamespaceUtils.configureTrigger(pollerElement, adapterBuilder, parserContext);
IntegrationNamespaceUtils.setValueIfAttributeDefined(adapterBuilder, pollerElement, "max-messages-per-poll");
Element txElement = DomUtils.getChildElementByTagName(pollerElement, "transactional");
if (txElement != null) {

View File

@@ -23,12 +23,12 @@ import org.w3c.dom.Element;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.core.Conventions;
import org.springframework.integration.scheduling.CronTrigger;
import org.springframework.integration.scheduling.IntervalTrigger;
import org.springframework.integration.scheduling.Trigger;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -142,38 +142,39 @@ public abstract class IntegrationNamespaceUtils {
* @param pollerElement the "poller" element to parse
* @param targetBuilder the builder that expects the "trigger" property
*/
public static void configureTrigger(Element pollerElement, BeanDefinitionBuilder targetBuilder) {
Trigger trigger = null;
public static void configureTrigger(Element pollerElement, BeanDefinitionBuilder targetBuilder, ParserContext parserContext) {
String triggerBeanName = null;
Element intervalElement = DomUtils.getChildElementByTagName(pollerElement, "interval-trigger");
if (intervalElement != null) {
trigger = createIntervalTrigger(intervalElement);
triggerBeanName = parseIntervalTrigger(intervalElement, parserContext);
}
else {
Element cronElement = DomUtils.getChildElementByTagName(pollerElement, "cron-trigger");
Assert.notNull(cronElement,
"A <poller> element must include either an <interval-trigger/> or <cron-trigger/> child element.");
trigger = createCronTrigger(cronElement);
triggerBeanName = parseCronTrigger(cronElement, parserContext);
}
targetBuilder.addPropertyValue("trigger", trigger);
targetBuilder.addPropertyReference("trigger", triggerBeanName);
}
private static Trigger createIntervalTrigger(Element element) {
private static String parseIntervalTrigger(Element element, ParserContext parserContext) {
String interval = element.getAttribute("interval");
Assert.hasText(interval, "the 'interval' attribute is required for an <interval-trigger/>");
TimeUnit timeUnit = TimeUnit.valueOf(element.getAttribute("time-unit"));
IntervalTrigger trigger = new IntervalTrigger(Long.valueOf(interval), timeUnit);
String initialDelay = element.getAttribute("initial-delay");
if (StringUtils.hasText(initialDelay)) {
trigger.setInitialDelay(Long.valueOf(initialDelay), timeUnit);
}
trigger.setFixedRate("true".equals(element.getAttribute("fixed-rate").toLowerCase()));
return trigger;
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(IntervalTrigger.class);
builder.addConstructorArgValue(interval);
builder.addConstructorArgValue(timeUnit);
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "initial-delay");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "fixed-rate");
return BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(), parserContext.getRegistry());
}
private static Trigger createCronTrigger(Element element) {
private static String parseCronTrigger(Element element, ParserContext parserContext) {
String cronExpression = element.getAttribute("expression");
Assert.hasText(cronExpression, "the 'expression' attribute is required for a <cron-trigger/>");
return new CronTrigger(cronExpression);
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CronTrigger.class);
builder.addConstructorArgValue(cronExpression);
return BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(), parserContext.getRegistry());
}
/**

View File

@@ -35,6 +35,8 @@ public class IntervalTrigger implements Trigger {
private final long interval;
private final TimeUnit timeUnit;
private volatile long initialDelay = 0;
private volatile boolean fixedRate = false;
@@ -44,15 +46,16 @@ public class IntervalTrigger implements Trigger {
* Create a trigger with the given interval in milliseconds.
*/
public IntervalTrigger(long interval) {
Assert.isTrue(interval >= 0, "interval must not be negative");
this.interval = interval;
this(interval, null);
}
/**
* Create a trigger with the given interval and time unit.
*/
public IntervalTrigger(long interval, TimeUnit unit) {
this(unit.toMillis(interval));
public IntervalTrigger(long interval, TimeUnit timeUnit) {
Assert.isTrue(interval >= 0, "interval must not be negative");
this.timeUnit = (timeUnit != null) ? timeUnit : TimeUnit.MILLISECONDS;
this.interval = this.timeUnit.toMillis(interval);
}
@@ -60,14 +63,7 @@ public class IntervalTrigger implements Trigger {
* Specify the delay for the initial execution.
*/
public void setInitialDelay(long initialDelay) {
this.initialDelay = initialDelay;
}
/**
* Specify the delay for the initial execution using the given time unit.
*/
public void setInitialDelay(long initialDelay, TimeUnit unit) {
this.initialDelay = unit.toMillis(initialDelay);
this.initialDelay = this.timeUnit.toMillis(initialDelay);
}
/**