2016-07-25 17 views
0

基本的に、私は手前のポートを認識していません。私はポート番号を送信するためにホストにRESTリクエストを行います。私はポート番号を取得していますが、受信ポート番号を使用して接続ファクトリをinBoundClientに設定できないため、先に進むことはできません。この問題を理解するには、以下のコードをご覧ください。Spring TCP統合の実行時にportとconnectionFactoryを設定する方法

私は以下のように定義されたTCP接続を持っている:

@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);で:私のクラスで

<?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> 

コンフィグマネージャ

を、私は以下しようとしています、 コンパイラエラーが発生しました。 The method setConnectionFactory(AbstractConnectionFactory) in the type TcpReceivingChannelAdapter is not applicable for the arguments (TcpConnectionFactoryFactoryBean)。 ポートを動的に設定することができますか。

EDIT:

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

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

、ゲイリーの提案を1としてしかし、私はこの下のエラーがあります:

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

私は豆のgateway.send("ACK"); 1を試してみてください

。私が持っている outBoundClient channel="tcp-client-input"

+0

もちろん例外が発生します... 'TcpConnectionFactoryFactoryBean'は明らかに' ConnectionFactory'ではありません。これは、 'ConnectionFactory'を生成する' FactoryBean'です。 –

+0

@ M.Deinum、そう、私はそれに同意する。それは私がやった試行の一つに過ぎませんでした。実際はTcpNetClientConnectionFactoryです。 –

答えて

1

ポートはコンストラクタで設定されているので、Beanを作成した後は変更できません。

isが接続ファクトリをすでに作成した後で、接続ファクトリBeanのポートを変更することもできません。

Springを作成するのではなく、自分で接続ファクトリ(new TcpNetClientConnectionFactory(...))を作成する必要があります。作成して構成した後、アダプタに追加する前に必ずafterPropertiesSet()を呼び出してください。

+0

ありがとうございます。 OKayして、この新しい接続ファクトリをinBoundClientに設定します。しかし、どうしたらoutBoundClient、それはEventDrivenConsumer型です、ここではconnectionFactoryを設定できません。さらに、私はTcpSendingMessageHandlerを試しましたが、TcpSendingMessageHandlerに型変換したEventDrivenConsumerを実装することはできません。 –

+0

'@Qualifier(" outboundClient.handler ")'を使用して、メッセージハンドラへの参照を取得します。 –

+0

私はそれを使用しましたが、私には別の問題がありましたが、これは私にとって混乱しています。私は加入者を持っていますが、加入者が見つかりません。これらの詳細を編集しました。 –

関連する問題