2016-07-25 57 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

我已經使用了它,現在我有一個不同的問題,這是令我困惑的。雖然我有用戶,但它抱怨說,沒有用戶找到。我用這些細節編輯了問題。 –

相關問題