ChannelFactory now accepts the channel name (INT-280).
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.integration.bus;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
@@ -37,8 +38,11 @@ import org.springframework.util.Assert;
|
||||
* an ApplicationContext.
|
||||
*
|
||||
* @author Marius Bogoevici
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class DefaultChannelFactoryBean implements ApplicationContextAware, FactoryBean {
|
||||
public class DefaultChannelFactoryBean implements ApplicationContextAware, FactoryBean, BeanNameAware {
|
||||
|
||||
private volatile String beanName;
|
||||
|
||||
private volatile ChannelFactory channelFactory;
|
||||
|
||||
@@ -52,6 +56,10 @@ public class DefaultChannelFactoryBean implements ApplicationContextAware, Facto
|
||||
}
|
||||
|
||||
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setApplicationContext(ApplicationContext applicationContext){
|
||||
Map map = applicationContext.getBeansOfType(MessageBus.class);
|
||||
@@ -70,7 +78,7 @@ public class DefaultChannelFactoryBean implements ApplicationContextAware, Facto
|
||||
|
||||
public Object getObject() throws Exception {
|
||||
Assert.notNull(channelFactory, "ChannelFactory not set on this instance. Is this used within an ApplicationContext?");
|
||||
return channelFactory.getChannel(dispatcherPolicy, interceptors);
|
||||
return channelFactory.getChannel(this.beanName, dispatcherPolicy, interceptors);
|
||||
}
|
||||
|
||||
public Class<?> getObjectType() {
|
||||
|
||||
@@ -368,7 +368,7 @@ public class MessageBus implements ChannelRegistry, EndpointRegistry,
|
||||
if (this.logger.isInfoEnabled()) {
|
||||
logger.info("auto-creating channel '" + channelName + "'");
|
||||
}
|
||||
channel = channelFactory.getChannel(null, null);
|
||||
channel = channelFactory.getChannel(channelName, null, null);
|
||||
this.registerChannel(channelName, channel);
|
||||
}
|
||||
return channel;
|
||||
|
||||
@@ -35,11 +35,14 @@ public abstract class AbstractChannelFactory implements ChannelFactory {
|
||||
super();
|
||||
}
|
||||
|
||||
public final MessageChannel getChannel(DispatcherPolicy dispatcherPolicy, List<ChannelInterceptor> interceptors) {
|
||||
public final MessageChannel getChannel(String name, DispatcherPolicy dispatcherPolicy, List<ChannelInterceptor> interceptors) {
|
||||
AbstractMessageChannel channel = createChannelInternal(dispatcherPolicy);
|
||||
if (null != interceptors) {
|
||||
channel.setInterceptors(interceptors);
|
||||
}
|
||||
if (name != null && channel.getName() == null) {
|
||||
channel.setName(name);
|
||||
}
|
||||
return channel;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,14 +24,14 @@ import org.springframework.integration.channel.MessageChannel;
|
||||
|
||||
/**
|
||||
* Interface for a channel factory.
|
||||
*
|
||||
* @author Marius Bogoevici
|
||||
*/
|
||||
public interface ChannelFactory {
|
||||
|
||||
/**
|
||||
* Creates a channel, based on the provided dispatcher policy, and with the given interceptors.
|
||||
* @return
|
||||
* Creates a channel based on the provided name, dispatcher policy, and interceptors.
|
||||
*/
|
||||
MessageChannel getChannel(DispatcherPolicy dispatcherPolicy, List<ChannelInterceptor> interceptors);
|
||||
MessageChannel getChannel(String name, DispatcherPolicy dispatcherPolicy, List<ChannelInterceptor> interceptors);
|
||||
|
||||
}
|
||||
|
||||
@@ -28,6 +28,8 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.integration.bus.DefaultChannelFactoryBean;
|
||||
import org.springframework.integration.bus.MessageBus;
|
||||
@@ -74,8 +76,9 @@ public class ChannelFactoryTests {
|
||||
DirectChannelFactory channelFactory = new DirectChannelFactory();
|
||||
assertNotNull(interceptors);
|
||||
AbstractMessageChannel channel = (AbstractMessageChannel)
|
||||
channelFactory.getChannel(dispatcherPolicy, interceptors);
|
||||
channelFactory.getChannel("testChannel", dispatcherPolicy, interceptors);
|
||||
assertEquals(DirectChannel.class, channel.getClass());
|
||||
assertEquals("testChannel", channel.getName());
|
||||
assertInterceptors(channel);
|
||||
}
|
||||
|
||||
@@ -96,8 +99,9 @@ public class ChannelFactoryTests {
|
||||
ThreadLocalChannelFactory channelFactory = new ThreadLocalChannelFactory();
|
||||
assertNotNull(interceptors);
|
||||
AbstractMessageChannel channel = (AbstractMessageChannel)
|
||||
channelFactory.getChannel(dispatcherPolicy, interceptors);
|
||||
channelFactory.getChannel("testChannel", dispatcherPolicy, interceptors);
|
||||
assertEquals(ThreadLocalChannel.class, channel.getClass());
|
||||
assertEquals("testChannel", channel.getName());
|
||||
assertInterceptors(channel);
|
||||
}
|
||||
|
||||
@@ -111,20 +115,34 @@ public class ChannelFactoryTests {
|
||||
messageBusDefinitionBuilder.getBeanDefinition().getPropertyValues().addPropertyValue("channelFactory", channelFactory);
|
||||
applicationContext.registerBeanDefinition("messageBus", messageBusDefinitionBuilder.getBeanDefinition());
|
||||
DefaultChannelFactoryBean channelFactoryBean = new DefaultChannelFactoryBean(dispatcherPolicy);
|
||||
channelFactoryBean.setBeanName("testChannel");
|
||||
channelFactoryBean.setApplicationContext(applicationContext);
|
||||
channelFactoryBean.setInterceptors(interceptors);
|
||||
StubChannel channel = (StubChannel)channelFactoryBean.getObject();
|
||||
StubChannel channel = (StubChannel) channelFactoryBean.getObject();
|
||||
assertEquals("testChannel", channel.getName());
|
||||
assertTrue(dispatcherPolicy == channel.getDispatcherPolicy());
|
||||
assertInterceptors(channel);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultChannelFactoryBeanInApplicationContext() throws Exception{
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"defaultChannelFactoryBeanTests.xml", this.getClass());
|
||||
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
|
||||
assertEquals(StubChannel.class, channel.getClass());
|
||||
assertEquals("testChannel", channel.getName());
|
||||
DispatcherPolicy dispatcherPolicy = (DispatcherPolicy) context.getBean("dispatcherPolicy");
|
||||
assertTrue(dispatcherPolicy == channel.getDispatcherPolicy());
|
||||
}
|
||||
|
||||
|
||||
private void genericChannelFactoryTests(ChannelFactory channelFactory, Class<?> expectedChannelClass) {
|
||||
assertNotNull(dispatcherPolicy);
|
||||
assertNotNull(interceptors);
|
||||
AbstractMessageChannel channel = (AbstractMessageChannel)
|
||||
channelFactory.getChannel(dispatcherPolicy, interceptors);
|
||||
channelFactory.getChannel("testChannel", dispatcherPolicy, interceptors);
|
||||
assertEquals(expectedChannelClass, channel.getClass());
|
||||
assertEquals("testChannel", channel.getName());
|
||||
assertTrue(channel.getDispatcherPolicy() == dispatcherPolicy);
|
||||
assertInterceptors(channel);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<?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 channel-factory="factory"/>
|
||||
|
||||
<beans:bean id="factory" class="org.springframework.integration.channel.factory.StubChannelFactory"/>
|
||||
|
||||
<beans:bean id="testChannel" class="org.springframework.integration.bus.DefaultChannelFactoryBean">
|
||||
<beans:constructor-arg ref="dispatcherPolicy"/>
|
||||
</beans:bean>
|
||||
|
||||
<beans:bean id="dispatcherPolicy" class="org.springframework.integration.channel.DispatcherPolicy"/>
|
||||
|
||||
</beans:beans>
|
||||
Reference in New Issue
Block a user