2016-07-25 1 views
0

기본적으로, 저는 이전에 포트를 인식하지 못했습니다. 호스트에 REST 요청을 만들어 포트 번호를 보냅니다. 포트 번호를 얻는 중이지만 수신 포트 번호가 inBoundClient 인 연결 팩토리를 설정할 수 없으므로 앞으로 이동할 수 없습니다. 문제를 이해하려면 아래 코드를 살펴보십시오.Spring TCP 통합을 위해 런타임 중에 port와 connectionFactory를 설정하는 방법

내가 TCP 연결을 아래와 같이 정의 : 라인 receiver.setConnectionFactory(connFactory);에서

@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."); 
} 
} 
} 

을하지만 :

<?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). 어쨌든 포트를 동적으로 설정할 수 있습니까?

편집 :

@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"); 하나를 시도

. 하지만 난 내 outBoundClient 가지고 channel="tcp-client-input"

+0

물론 예외가 발생합니다 ... TcpConnectionFactoryFactoryBean은 분명히 ConnectionFactory가 아닙니다. 'FactoryFactory'를 생성하는'FactoryBean'입니다. –

+0

@ M.Deinum, 예, 그렇습니다. 그것은 제가 한 시련 중 하나였습니다. 사실 TcpNetClientConnectionFactory입니다. –

답변

1

포트가 생성자에서 설정되어 있으므로 빈을 만든 후에 변경할 수 없습니다.

is가 연결 팩토리를 이미 작성한 후에도 연결 팩토리 bean의 포트를 변경할 수 없습니다.

스프링을 생성하는 대신 연결 팩토리 (new TcpNetClientConnectionFactory(...))를 직접 만들어야합니다. 생성 및 구성한 후 어댑터에 추가하기 전에 afterPropertiesSet()을 호출해야합니다.

+0

감사. 좋습니다. 그러면 새 연결 팩토리가 inBoundClient로 설정됩니다. 하지만, outBoundClient는 EventDrivenConsumer 유형이며, 여기서는 connectionFactory를 설정할 수 없습니다. 그리고 더 이상 TcpSendingMessageHandler를 시도했지만 TcpSendingMessageHandler로 EventDrivenConsumer를 형변환 할 수 없습니다. –

+0

'@Qualifier ("outboundClient.handler")'를 사용하여 메시지 핸들러에 대한 참조를 가져옵니다. –

+0

나는 그것을 사용했다, 지금 나는 다른 문제가있다. 그것은 나에게 혼란 스럽다. 내가 구독자를 가지고 있지만, 그것은 불평하고, 어떤 가입자도 찾지 못한다. 나는이 세부 사항으로 질문을 편집했다. –

관련 문제