Java 如何在Spring TCP集成的运行时设置端口和connectionFactory

Java 如何在Spring TCP集成的运行时设置端口和connectionFactory,java,spring,tcp,spring-integration,Java,Spring,Tcp,Spring Integration,基本上,我不知道港口在手。我将向主机发出REST请求以发送端口号。我正在获取端口号,但无法前进,因为我无法使用收到的端口号将连接工厂设置为inBoundClient。请看下面的代码,以了解问题 我有如下定义的tcp连接: <?xml version="1.0" encoding="UTF-8"?> <context:component-scan base-package="com.tcpclient" /> <beans xmlns="http://www.spri

基本上,我不知道港口在手。我将向主机发出REST请求以发送端口号。我正在获取端口号,但无法前进,因为我无法使用收到的端口号将连接工厂设置为
inBoundClient
。请看下面的代码,以了解问题

我有如下定义的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>
但是,在第
行receiver.setConnectionFactory(connFactory),,
我有一个编译器错误,
类型TcpReceivingChannelAdapter中的方法setConnectionFactory(AbstractConnectionFactory)不适用于参数(TcpConnectionFactoryFactoryBean)
。 是否有我可以动态设置端口的方法

编辑:根据Gary的建议

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

outBoundClient.setConnectionFactory(connFactory);
outBoundClient.setRetryInterval(60000);
outBoundClient.afterPropertiesSet();
outBoundClient.start();
但是,我有以下错误:

Dispatcher没有通道“application.tcp客户端输入”的订阅服务器。;嵌套异常为org.springframework.integration.MessageDispatchingException:Dispatcher没有订户


当我尝试
gateway.send(“ACK”)我的一个bean。但是我有我的
outBoundClient
拥有
channel=“tcp client input”

,因为端口是在构造函数中设置的,所以创建bean后不能更改它

在is已经创建了连接工厂之后,您也不能更改连接工厂bean上的端口


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

因为端口是在构造函数中设置的,创建bean后,您不能更改它

在is已经创建了连接工厂之后,您也不能更改连接工厂bean上的端口


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

当然,您会得到一个异常。。。
TcpConnectionFactoryFactoryBean
显然不是
ConnectionFactory
。这是一个
FactoryBean
,它创建了一个
ConnectionFactory
@M.Deinum,是的,我同意这一点。这只是我做的试验之一,实际上是TcpNetClientConnectionFactory。当然你会得到一个例外。。。
TcpConnectionFactoryFactoryBean
显然不是
ConnectionFactory
。这是一个
FactoryBean
,它创建了一个
ConnectionFactory
@M.Deinum,是的,我同意这一点。这只是我做的试验之一,实际上是TcpNetClientConnectionFactory。谢谢。好的,我将把这个新的连接工厂设置为inBoundClient。但是,outBoundClient呢,它是EventDrivenConsumer类型,我不能在这里设置connectionFactory。此外,我尝试使用TcpSendingMessageHandler,但我无法将EventDrivenConsumer类型转换为TcpSendingMessageHandler。使用
@Qualifier(“outboundClient.handler”)
获取对消息处理程序的引用。我使用过它,现在我遇到了另一个问题,这让我感到困惑。虽然我有订户,但它抱怨说,找不到订户。我已经编辑了带有这些详细信息的问题。您的适配器(EventDrivenConsumer)将不会订阅该频道,直到它是
start()
ed-将“自动启动”更改为true,或者在将连接工厂插入处理程序后注入端点并启动它。谢谢。好的,我将把这个新的连接工厂设置为inBoundClient。但是,outBoundClient呢,它是EventDrivenConsumer类型,我不能在这里设置connectionFactory。此外,我尝试使用TcpSendingMessageHandler,但我无法将EventDrivenConsumer类型转换为TcpSendingMessageHandler。使用
@Qualifier(“outboundClient.handler”)
获取对消息处理程序的引用。我使用过它,现在我遇到了另一个问题,这让我感到困惑。虽然我有订户,但它抱怨说,找不到订户。我已经编辑了带有这些详细信息的问题。您的适配器(EventDrivenConsumer)将不会订阅通道,直到它是
start()
ed-将自动启动更改为true,或者在将连接工厂插入处理程序后注入端点并启动它。
@Autowired
@Qualifier("outboundClient.handler")
TcpSendingMessageHandler outBoundClient;`,

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