<message-bus/> will automatically add a MessageBusAwareBeanPostProcessor to the context.
This commit is contained in:
@@ -16,30 +16,36 @@
|
||||
|
||||
package org.springframework.integration.config;
|
||||
|
||||
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.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.core.Conventions;
|
||||
import org.springframework.integration.ConfigurationException;
|
||||
import org.springframework.integration.bus.MessageBus;
|
||||
import org.springframework.integration.bus.MessageBusAwareBeanPostProcessor;
|
||||
import org.springframework.integration.endpoint.ConcurrencyPolicy;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
/**
|
||||
* Parser for the <em>message-bus</em> element of the integration namespace.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Marius Bogoevici
|
||||
*/
|
||||
public class MessageBusParser extends AbstractSimpleBeanDefinitionParser {
|
||||
|
||||
public static final String MESSAGE_BUS_BEAN_NAME = "internal.MessageBus";
|
||||
|
||||
public static final String MESSAGE_BUS_AWARE_POST_PROCESSOR_BEAN_NAME = "internal.MessageBusAwareBeanPostProcessor";
|
||||
|
||||
private static final Class<?> MESSAGE_BUS_CLASS = MessageBus.class;
|
||||
|
||||
private static final String ERROR_CHANNEL_ATTRIBUTE = "error-channel";
|
||||
@@ -48,7 +54,6 @@ public class MessageBusParser extends AbstractSimpleBeanDefinitionParser {
|
||||
|
||||
private static final String DEFAULT_CONCURRENCY_PROPERTY = "defaultConcurrencyPolicy";
|
||||
|
||||
|
||||
@Override
|
||||
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext)
|
||||
throws BeanDefinitionStoreException {
|
||||
@@ -93,4 +98,21 @@ public class MessageBusParser extends AbstractSimpleBeanDefinitionParser {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
super.doParse(element, parserContext, builder);
|
||||
addPostProcessors(parserContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds extra post-processors to the context, to inject the objects configured by the MessageBus
|
||||
*/
|
||||
private void addPostProcessors(ParserContext parserContext) {
|
||||
BeanDefinition postProcessorDefinition = new RootBeanDefinition(MessageBusAwareBeanPostProcessor.class);
|
||||
postProcessorDefinition.getConstructorArgumentValues().addGenericArgumentValue(
|
||||
new RuntimeBeanReference(MessageBusParser.MESSAGE_BUS_BEAN_NAME));
|
||||
parserContext.getRegistry().registerBeanDefinition(MESSAGE_BUS_AWARE_POST_PROCESSOR_BEAN_NAME,
|
||||
postProcessorDefinition);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,12 +28,14 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.ConfigurationException;
|
||||
import org.springframework.integration.bus.MessageBus;
|
||||
import org.springframework.integration.bus.TestMessageBusAwareImpl;
|
||||
import org.springframework.integration.endpoint.TargetEndpoint;
|
||||
import org.springframework.integration.handler.TestHandlers;
|
||||
import org.springframework.integration.scheduling.Subscription;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @author Marius Bogoevici
|
||||
*/
|
||||
public class MessageBusParserTests {
|
||||
|
||||
@@ -97,7 +99,12 @@ public class MessageBusParserTests {
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
exceptionThrown = true;
|
||||
assertEquals(ConfigurationException.class, e.getCause().getClass());
|
||||
// an exception is thrown when creating the post-processor, which
|
||||
// tries to get a reference to the message bus
|
||||
assertEquals(BeanCreationException.class, e.getCause().getClass());
|
||||
assertEquals(e.getBeanName(), MessageBusParser.MESSAGE_BUS_AWARE_POST_PROCESSOR_BEAN_NAME);
|
||||
assertEquals(ConfigurationException.class, ((BeanCreationException) e.getCause()).getCause().getClass());
|
||||
assertEquals(((BeanCreationException) e.getCause()).getBeanName(), MessageBusParser.MESSAGE_BUS_BEAN_NAME);
|
||||
}
|
||||
assertTrue(exceptionThrown);
|
||||
}
|
||||
@@ -128,5 +135,13 @@ public class MessageBusParserTests {
|
||||
assertEquals(14, endpoint2.getConcurrencyPolicy().getCoreSize());
|
||||
assertEquals(17, endpoint2.getConcurrencyPolicy().getMaxSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessageBusAwareAutomaticallyAddedByNamespace() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("messageBusWithMessageBusAware.xml",
|
||||
this.getClass());
|
||||
TestMessageBusAwareImpl messageBusAware = (TestMessageBusAwareImpl) context.getBean("messageBusAwareBean");
|
||||
assertTrue(messageBusAware.getMessageBus() == context.getBean(MessageBusParser.MESSAGE_BUS_BEAN_NAME));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<?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-core-1.0.xsd">
|
||||
|
||||
<message-bus/>
|
||||
|
||||
<beans:bean id="messageBusAwareBean" class="org.springframework.integration.bus.TestMessageBusAwareImpl"/>
|
||||
|
||||
</beans:beans>
|
||||
Reference in New Issue
Block a user