기본적으로, 저는 이전에 포트를 인식하지 못했습니다. 호스트에 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"
물론 예외가 발생합니다 ... TcpConnectionFactoryFactoryBean은 분명히 ConnectionFactory가 아닙니다. 'FactoryFactory'를 생성하는'FactoryBean'입니다. –
@ M.Deinum, 예, 그렇습니다. 그것은 제가 한 시련 중 하나였습니다. 사실 TcpNetClientConnectionFactory입니다. –