如何在 Spring TCP 集成的运行时设置端口和 connectionFactory [英] How to set port and connectionFactory during run time for Spring TCP integration
问题描述
基本上,我事先不知道端口.我将向主机发出 REST 请求以发送端口号.我正在获取端口号,但无法继续,因为我无法使用接收到的端口号将连接工厂设置为 inBoundClient
.请看下面的代码,以了解问题.
Basically, I am not aware of port before hand. I will make a REST request to a host to send the port number. I am getting the port number, but not able to move ahead as I am not able to set connection factory into inBoundClient
with received port number. Please have a look at the code below, to understand the problem.
我的 tcp 连接定义如下:
I have tcp connections defined as below:
<?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>
<小时>
配置管理器
Config manager
在我的课堂上,我正在尝试以下内容:
In my class, I am trying below:
@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);
行,我有编译器错误,TcpReceivingChannelAdapter 类型中的 setConnectionFactory(AbstractConnectionFactory) 方法不适用于参数 (TcpConnectionFactoryFactoryBean)
.无论如何我可以动态设置端口.
But, at the line receiver.setConnectionFactory(connFactory);
,
I have compiler error,
The method setConnectionFactory(AbstractConnectionFactory) in the type TcpReceivingChannelAdapter is not applicable for the arguments (TcpConnectionFactoryFactoryBean)
.
Is there anyway I could set port dynamically.
根据加里的建议,
@Autowired
@Qualifier("outboundClient.handler")
TcpSendingMessageHandler outBoundClient;`,
outBoundClient.setConnectionFactory(connFactory);
outBoundClient.setRetryInterval(60000);
outBoundClient.afterPropertiesSet();
outBoundClient.start();
但是,我有以下错误:
Dispatcher 没有频道application.tcp-client-input"的订阅者.嵌套异常是 org.springframework.integration.MessageDispatchingException: Dispatcher has nosubscribes
当我尝试 gateway.send("ACK");
我的一个 bean 时.但我有我的 outBoundClient
有 channel="tcp-client-input"
when I try gateway.send("ACK");
one of my beans. but I have my outBoundClient
having channel="tcp-client-input"
推荐答案
由于端口是在构造函数中设置的,因此在创建 bean 后无法更改它.
Since the port is set in a constructor, you can't change it after the bean is created.
在连接工厂 bean 已经创建其连接工厂后,您也无法更改其端口.
You also can't change the port on a connection factory bean after is has already created its connection factory.
您需要自己创建连接工厂 (new TcpNetClientConnectionFactory(...)
) 而不是让 Spring 创建它 - 务必在创建后调用 afterPropertiesSet()
并在将其添加到适配器之前对其进行配置.
You need to create the connection factory yourself (new TcpNetClientConnectionFactory(...)
) rather than have Spring create it - be sure to call afterPropertiesSet()
after creating and configuring it and before you add it to the adapter.
这篇关于如何在 Spring TCP 集成的运行时设置端口和 connectionFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!