2016-07-25 59 views
0

基本上,我并不知道端口。我将向主机发送REST请求以发送端口号。我正在获取端口号,但无法继续前进,因为我无法将连接工厂设置为inBoundClient和接收的端口号。请看下面的代码,了解问题。如何在运行时为Spring TCP集成设置port和connectionFactory

我已经定义的TCP连接如下:

<?xml version="1.0" encoding="UTF-8"?> 
<context:component-scan base-package="com.tcpclient" /> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration" 
xmlns:int-ip="http://www.springframework.org/schema/integration/ip" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd 
    http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd"> 
<context:annotation-config /> 
<!--Deserializer for incoming data on the socket --> 
<bean 
class="org.springframework.integration.ip.tcp.serializer.ByteArraySingleTerminatorSerializer" 
id="serializeAndDeserializer"> 
<constructor-arg type="byte" value="0" /> 
</bean> 



<!-- TCP Client configuration --> 

<!-- Channels for communication --> 

<int:channel id="tcp-client-input" /> 

<int:channel id="message" /> 

<int:channel id="message-serviceActivator" /> 

<int:gateway id="gateway" service-interface="com.tcpclient.ClientGateway" 
default-request-channel="tcp-client-input" default-reply-channel="message" /> 



<int-ip:tcp-outbound-channel-adapter 
id="outBoundClient" channel="tcp-client-input" 
retry-interval="60000" auto-startup="false" /> 

<int-ip:tcp-inbound-channel-adapter 
id="inBoundClient" channel="message" 
client-mode="true" auto-startup="false" retry-interval="60000" /> 


<int:object-to-string-transformer 
input-channel="message" output-channel="message-serviceActivator" /> 
<int:service-activator input-channel="message-serviceActivator" 
method="onRtlsMessageArrival"> 
<bean class="com.tcpclient.HandleMessage" /> 
</int:service-activator> 
</beans> 

配置经理

在我的课,我想下面:

@component 
public class ConfigManager { 

@Autowired 
@Qualifier("clientFactory") 
TcpConnectionFactoryFactoryBean connFactory; 

@Autowired 
@Qualifier("inBoundClient") 
SmartLifecycle inboundClient; 

@Autowired 
@Qualifier("outBoundClient") 
SmartLifecycle outBoundClient; 

public void initialize(Boolean canStart) { 

try { 
    if (canStart) { 
        String portResponse = restTemplate.postForObject(SYSTEM_OPEN_SOCK_URI, openSockeEntity, 
          String.class); 
        int portNumber = parsePortFromJson(portResponse); 
        connFactory.setPort(portNumber); 
        TcpReceivingChannelAdapter receiver = (TcpReceivingChannelAdapter) inboundClient; 
        receiver.setConnectionFactory(connFactory); 
        receiver.start(); 
        EventDrivenConsumer sender = (EventDrivenConsumer) outBoundClient; 
        sender.start(); 
    } 

} catch (Exception e) { 
    logger.error("Error occured while fetching data."); 
} 
} 
} 

但是,在该行receiver.setConnectionFactory(connFactory); , 我有编译器错误, The method setConnectionFactory(AbstractConnectionFactory) in the type TcpReceivingChannelAdapter is not applicable for the arguments (TcpConnectionFactoryFactoryBean)。 无论如何我可以动态设置端口。

编辑:按照加里的建议,

@Autowired 
@Qualifier("outboundClient.handler") 
TcpSendingMessageHandler outBoundClient;`, 

outBoundClient.setConnectionFactory(connFactory); 
outBoundClient.setRetryInterval(60000); 
outBoundClient.afterPropertiesSet(); 
outBoundClient.start(); 

不过,我有这样的错误如下:

Dispatcher has no subscribers for channel 'application.tcp-client-input'.; nested exception is org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers

当我尝试我的豆gateway.send("ACK");之一。但我有我的outBoundClientchannel="tcp-client-input"

+0

当然你会得到一个异常......一个'TcpConnectionFactoryFactoryBean'显然不是'ConnectionFactory'。它是一个'FactoryBean',它创建一个'ConnectionFactory'。 –

+0

@ M.Deinum,是的,我同意这一点。这只是我做过的一个试验,实际上它是TcpNetClientConnectionFactory。 –

回答

1

由于端口是在一个构造函数中设置的,所以在创建bean后不能更改它。

在is已创建连接工厂之后,您也无法更改连接工厂bean上的端口。

您需要自己创建连接工厂(new TcpNetClientConnectionFactory(...)),而不是让Spring创建连接工厂 - 确保在创建并配置连接工厂并在将其添加到适配器之前调用afterPropertiesSet()

+0

谢谢。 OKay会这样做,我将把这个新的连接工厂设置为inBoundClient。但是,怎么样outBoundClient,它的类型EventDrivenConsumer,我不能在这里设置connectionFactory。而且更多的是,我尝试了TcpSendingMessageHandler,但我无法将类型为EventDrivenConsumer的TcpSendingMessageHandler。 –

+0

使用'@Qualifier(“outboundClient.handler”)'获得对消息处理程序的引用。 –

+0

我已经使用了它,现在我有一个不同的问题,这是令我困惑的。虽然我有用户,但它抱怨说,没有用户找到。我用这些细节编辑了问题。 –

相关问题