Added support for 'advice-chain' sub-element for the 'poller' element (INT-503).

This commit is contained in:
Mark Fisher
2008-12-16 23:02:21 +00:00
parent 71ba875cdc
commit 17d0b27230
8 changed files with 173 additions and 3 deletions

View File

@@ -138,6 +138,7 @@ public class ConsumerEndpointFactoryBean implements FactoryBean, BeanFactoryAwar
pollingConsumer.setTaskExecutor(this.pollerMetadata.getTaskExecutor());
pollingConsumer.setTransactionManager(this.pollerMetadata.getTransactionManager());
pollingConsumer.setTransactionDefinition(this.pollerMetadata.getTransactionDefinition());
pollingConsumer.setAdviceChain(this.pollerMetadata.getAdviceChain());
this.endpoint = pollingConsumer;
}
else {

View File

@@ -19,11 +19,18 @@ package org.springframework.integration.config.xml;
import java.util.concurrent.TimeUnit;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.context.IntegrationContextUtils;
@@ -69,6 +76,10 @@ public class PollerParser extends AbstractBeanDefinitionParser {
}
configureTrigger(element, metadataBuilder, parserContext);
IntegrationNamespaceUtils.setValueIfAttributeDefined(metadataBuilder, element, "max-messages-per-poll");
Element adviceChainElement = DomUtils.getChildElementByTagName(element, "advice-chain");
if (adviceChainElement != null) {
configureAdviceChain(adviceChainElement, metadataBuilder, parserContext);
}
Element txElement = DomUtils.getChildElementByTagName(element, "transactional");
if (txElement != null) {
configureTransactionAttributes(txElement, metadataBuilder);
@@ -142,4 +153,39 @@ public class PollerParser extends AbstractBeanDefinitionParser {
targetBuilder.addPropertyValue("transactionDefinition", txDefinition);
}
/**
* Parses the 'advice-chain' element's sub-elements.
*/
@SuppressWarnings("unchecked")
private void configureAdviceChain(Element adviceChainElement, BeanDefinitionBuilder targetBuilder, ParserContext parserContext) {
ManagedList adviceChain = new ManagedList();
NodeList childNodes = adviceChainElement.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node child = childNodes.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
Element childElement = (Element) child;
String localName = child.getLocalName();
if ("bean".equals(localName)) {
BeanDefinitionHolder holder = parserContext.getDelegate().parseBeanDefinitionElement(
childElement, targetBuilder.getBeanDefinition());
parserContext.registerBeanComponent(new BeanComponentDefinition(holder));
adviceChain.add(new RuntimeBeanReference(holder.getBeanName()));
}
else if ("ref".equals(localName)) {
String ref = childElement.getAttribute("bean");
adviceChain.add(new RuntimeBeanReference(ref));
}
else {
BeanDefinition customBeanDefinition = parserContext.getDelegate().parseCustomElement(
childElement, targetBuilder.getBeanDefinition());
if (customBeanDefinition == null) {
parserContext.getReaderContext().error(
"failed to parse custom element '" + localName + "'", childElement);
}
}
}
}
targetBuilder.addPropertyValue("adviceChain", adviceChain);
}
}

View File

@@ -368,9 +368,12 @@
<xsd:attributeGroup ref="headerEnricherAttributes"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="transformer" type="handlerType"/>
<xsd:element name="object-to-string-transformer" type="handlerType"/>
<xsd:element name="payload-deserializing-transformer" type="handlerType"/>
<xsd:element name="payload-serializing-transformer" type="handlerType"/>
<xsd:element name="service-activator" type="handlerType"/>
<xsd:element name="splitter" type="handlerType"/>
<xsd:element name="transformer" type="handlerType"/>
<xsd:any namespace="##other" processContents="strict" minOccurs="0" maxOccurs="unbounded"/>
</xsd:choice>
<xsd:element name="router" minOccurs="0" maxOccurs="1">
@@ -424,6 +427,20 @@
<xsd:element ref="cron-trigger"/>
</xsd:choice>
<xsd:element name="transactional" type="transactionalType" minOccurs="0" maxOccurs="1"/>
<xsd:element name="advice-chain" minOccurs="0" maxOccurs="1">
<xsd:complexType>
<xsd:sequence>
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="ref" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="bean" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:any namespace="##other" processContents="strict" minOccurs="0" maxOccurs="unbounded"/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="receive-timeout" type="xsd:string"/>
<xsd:attribute name="send-timeout" type="xsd:string"/>

View File

@@ -119,8 +119,12 @@ public abstract class AbstractPollingEndpoint extends AbstractEndpoint implement
}
public void setAdviceChain(List<Advice> adviceChain) {
this.adviceChain.clear();
this.adviceChain.addAll(adviceChain);
synchronized (this.adviceChain) {
this.adviceChain.clear();
if (adviceChain != null) {
this.adviceChain.addAll(adviceChain);
}
}
}
private TransactionTemplate getTransactionTemplate() {

View File

@@ -16,6 +16,10 @@
package org.springframework.integration.scheduling;
import java.util.List;
import org.aopalliance.aop.Advice;
import org.springframework.core.task.TaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
@@ -31,6 +35,8 @@ public class PollerMetadata {
private volatile long receiveTimeout = 1000;
private List<Advice> adviceChain;
private volatile TaskExecutor taskExecutor;
private volatile PlatformTransactionManager transactionManager;
@@ -62,6 +68,14 @@ public class PollerMetadata {
return this.receiveTimeout;
}
public void setAdviceChain(List<Advice> adviceChain) {
this.adviceChain = adviceChain;
}
public List<Advice> getAdviceChain() {
return this.adviceChain;
}
public void setTaskExecutor(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}

View File

@@ -25,6 +25,7 @@ import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.scheduling.PollerMetadata;
/**
* @author Mark Fisher
@@ -62,4 +63,19 @@ public class PollerParserTests {
"topLevelPollerWithoutId.xml", PollerParserTests.class);
}
@Test
public void pollerWithAdviceChain() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"pollerWithAdviceChain.xml", PollerParserTests.class);
Object poller = context.getBean("poller");
assertNotNull(poller);
PollerMetadata metadata = (PollerMetadata) poller;
assertNotNull(metadata.getAdviceChain());
assertEquals(3, metadata.getAdviceChain().size());
assertEquals(context.getBean("adviceBean1"), metadata.getAdviceChain().get(0));
assertEquals(TestAdviceBean.class, metadata.getAdviceChain().get(1).getClass());
assertEquals(2, ((TestAdviceBean) metadata.getAdviceChain().get(1)).getId());
assertEquals(context.getBean("adviceBean3"), metadata.getAdviceChain().get(2));
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2002-2008 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.integration.config.xml;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
/**
* @author Mark Fisher
*/
public class TestAdviceBean implements MethodBeforeAdvice {
private final int id;
public TestAdviceBean(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
public void before(Method method, Object[] args, Object target) throws Throwable {
}
}

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<poller id="poller">
<interval-trigger interval="5000"/>
<advice-chain>
<ref bean="adviceBean1"/>
<beans:bean class="org.springframework.integration.config.xml.TestAdviceBean">
<beans:constructor-arg value="2"/>
</beans:bean>
<ref bean="adviceBean3"/>
</advice-chain>
</poller>
<beans:bean id="adviceBean1" class="org.springframework.integration.config.xml.TestAdviceBean">
<beans:constructor-arg value="1"/>
</beans:bean>
<beans:bean id="adviceBean3" class="org.springframework.integration.config.xml.TestAdviceBean">
<beans:constructor-arg value="3"/>
</beans:bean>
</beans:beans>