TCP Connection Factory FactoryBean Improvements
Add ctor argument so that early `getObjectType()` calls can return a narrower type (server Vs. client CF).
This commit is contained in:
committed by
Artem Bilan
parent
69ee423ce2
commit
0fd72d2d18
@@ -27,6 +27,7 @@ import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
import org.springframework.core.serializer.Deserializer;
|
||||
import org.springframework.core.serializer.Serializer;
|
||||
import org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory;
|
||||
import org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory;
|
||||
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
|
||||
import org.springframework.integration.ip.tcp.connection.DefaultTcpNetSSLSocketFactorySupport;
|
||||
@@ -121,6 +122,14 @@ public class TcpConnectionFactoryFactoryBean extends AbstractFactoryBean<Abstrac
|
||||
|
||||
private volatile BeanFactory beanFactory;
|
||||
|
||||
|
||||
public TcpConnectionFactoryFactoryBean() {
|
||||
}
|
||||
|
||||
public TcpConnectionFactoryFactoryBean(String type) {
|
||||
setType(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setBeanFactory(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
@@ -128,8 +137,11 @@ public class TcpConnectionFactoryFactoryBean extends AbstractFactoryBean<Abstrac
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return this.connectionFactory != null ? this.connectionFactory.getClass()
|
||||
: AbstractConnectionFactory.class;
|
||||
return this.connectionFactory != null ? this.connectionFactory.getClass() :
|
||||
this.type == null ? AbstractConnectionFactory.class :
|
||||
isServer() ? AbstractServerConnectionFactory.class :
|
||||
isClient() ? AbstractClientConnectionFactory.class :
|
||||
AbstractConnectionFactory.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -138,14 +150,15 @@ public class TcpConnectionFactoryFactoryBean extends AbstractFactoryBean<Abstrac
|
||||
this.mapper.setBeanFactory(this.beanFactory);
|
||||
}
|
||||
if (this.usingNio) {
|
||||
if ("server".equals(this.type)) {
|
||||
if (isServer()) {
|
||||
TcpNioServerConnectionFactory connectionFactory = new TcpNioServerConnectionFactory(this.port);
|
||||
this.setCommonAttributes(connectionFactory);
|
||||
this.setServerAttributes(connectionFactory);
|
||||
connectionFactory.setUsingDirectBuffers(this.usingDirectBuffers);
|
||||
connectionFactory.setTcpNioConnectionSupport(this.obtainNioConnectionSupport());
|
||||
this.connectionFactory = connectionFactory;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
TcpNioClientConnectionFactory connectionFactory = new TcpNioClientConnectionFactory(
|
||||
this.host, this.port);
|
||||
this.setCommonAttributes(connectionFactory);
|
||||
@@ -155,13 +168,14 @@ public class TcpConnectionFactoryFactoryBean extends AbstractFactoryBean<Abstrac
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ("server".equals(this.type)) {
|
||||
if (isServer()) {
|
||||
TcpNetServerConnectionFactory connectionFactory = new TcpNetServerConnectionFactory(this.port);
|
||||
this.setCommonAttributes(connectionFactory);
|
||||
this.setServerAttributes(connectionFactory);
|
||||
connectionFactory.setTcpSocketFactorySupport(this.obtainSocketFactorySupport());
|
||||
this.connectionFactory = connectionFactory;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
TcpNetClientConnectionFactory connectionFactory = new TcpNetClientConnectionFactory(
|
||||
this.host, this.port);
|
||||
this.setCommonAttributes(connectionFactory);
|
||||
@@ -258,8 +272,9 @@ public class TcpConnectionFactoryFactoryBean extends AbstractFactoryBean<Abstrac
|
||||
/**
|
||||
* @param type the type to set
|
||||
*/
|
||||
public void setType(String type) {
|
||||
public final void setType(String type) {
|
||||
this.type = type;
|
||||
Assert.isTrue(isServer() || isClient(), "type must be 'server' or 'client'");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -509,5 +524,13 @@ public class TcpConnectionFactoryFactoryBean extends AbstractFactoryBean<Abstrac
|
||||
this.applicationEventPublisher = applicationEventPublisher;
|
||||
}
|
||||
|
||||
private boolean isClient() {
|
||||
return "client".equals(this.type);
|
||||
}
|
||||
|
||||
private boolean isServer() {
|
||||
return "server".equals(this.type);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ public class TcpConnectionFactoryParser extends AbstractBeanDefinitionParser {
|
||||
" must be 'client' or 'server' for a TCP Connection Factory", element);
|
||||
}
|
||||
builder = BeanDefinitionBuilder.genericBeanDefinition(TcpConnectionFactoryFactoryBean.class);
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "type");
|
||||
IpAdapterParserUtils.addConstructorValueIfAttributeDefined(builder, element, "type");
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element,
|
||||
IpAdapterParserUtils.HOST);
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element,
|
||||
|
||||
@@ -55,6 +55,7 @@ import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.integration.channel.NullChannel;
|
||||
import org.springframework.integration.context.IntegrationContextUtils;
|
||||
import org.springframework.integration.ip.config.TcpConnectionFactoryFactoryBean;
|
||||
import org.springframework.integration.ip.event.IpIntegrationEvent;
|
||||
import org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter;
|
||||
import org.springframework.integration.test.support.LogAdjustingTestSupport;
|
||||
@@ -71,6 +72,16 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
*/
|
||||
public class ConnectionFactoryTests extends LogAdjustingTestSupport {
|
||||
|
||||
@Test
|
||||
public void factoryBeanTests() {
|
||||
TcpConnectionFactoryFactoryBean fb = new TcpConnectionFactoryFactoryBean("client");
|
||||
assertEquals(AbstractClientConnectionFactory.class, fb.getObjectType());
|
||||
fb = new TcpConnectionFactoryFactoryBean("server");
|
||||
assertEquals(AbstractServerConnectionFactory.class, fb.getObjectType());
|
||||
fb = new TcpConnectionFactoryFactoryBean();
|
||||
assertEquals(AbstractConnectionFactory.class, fb.getObjectType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObtainConnectionIdsNet() throws Exception {
|
||||
TcpNetServerConnectionFactory serverFactory = new TcpNetServerConnectionFactory(0);
|
||||
|
||||
Reference in New Issue
Block a user