gpt4 book ai didi

java - 如何动态更改 tcp-ibound-gateway 使用的端口

转载 作者:行者123 更新时间:2023-12-02 03:17:27 25 4
gpt4 key购买 nike

有没有办法动态更改 tcp 入站网关使用的端口?我想根据数据库中保存的配置来设置 tcp-inbound-gateway 使用的端口和超时,并且能够在不重新启动应用程序的情况下即时更改它们。为此,我决定使用“发布-订阅者”模式和扩展的 TcpInboundGateway 类:

public class RuntimeInboundGateway extends TcpInboundGateway implements SettingsSubscriber {

@Autowired
private Settings settings;

@PostConstruct
public void subscribe() {
settings.subscribe(this);
}

@Override
public void onSettingsChanged(Settings settings) {
this.stop();
AbstractByteArraySerializer serializer = new ByteArrayLfSerializer();
TcpNetServerConnectionFactory connectionFactory = new TcpNetServerConnectionFactory(settings.getPort());
connectionFactory.setSerializer(serializer);
connectionFactory.afterPropertiesSet();
this.setConnectionFactory(connectionFactory);
this.afterPropertiesSet();
this.start();
}
}

settings object 是一个单例 bean,当它更改时,tcp 入站网关确实开始监听新端口,但看起来它不会进一步发送入站消息。以下是 xml 配置的摘录:

<int-ip:tcp-connection-factory id="connFactory" type="server" port="${port}"
serializer="serializer"
deserializer="serializer"/>

<bean id="serializer" class="org.springframework.integration.ip.tcp.serializer.ByteArrayLfSerializer"/>

<bean id="inboundGateway" class="com.example.RuntimeInboundGateway">
<property name="connectionFactory" ref="connFactory"/>
<property name="requestChannel" ref="requestChannel"/>
<property name="replyChannel" ref="responseChannel"/>
<property name="errorChannel" ref="exceptionChannel"/>
<property name="autoStartup" value="true"/>
</bean>

配置中有logging-channel-adapter,它可以记录对服务的任何请求,在设置更改之前不会出现任何问题。之后,它没有,尽管我可以通过 telnet localhost <NEW_PORT> 连接到新端口,但我没有看到收到任何消息。 。有人可以看一下并说明如何实现所需的行为吗?

最佳答案

快速查看您的代码表明它应该可以正常工作,所以我刚刚编写了一个快速的 Spring Boot 应用程序,它对我来说工作得很好......

@SpringBootApplication
public class So40084223Application {

public static void main(String[] args) throws Exception {
ConfigurableApplicationContext ctx = SpringApplication.run(So40084223Application.class, args);
Socket socket = SocketFactory.getDefault().createSocket("localhost", 1234);
socket.getOutputStream().write("foo\r\n".getBytes());
socket.close();
QueueChannel queue = ctx.getBean("queue", QueueChannel.class);
System.out.println(queue.receive(10000));
ctx.getBean(MyInboundGateway.class).recycle(1235);
socket = SocketFactory.getDefault().createSocket("localhost", 1235);
socket.getOutputStream().write("fooo\r\n".getBytes());
socket.close();
System.out.println(queue.receive(10000));
ctx.close();
}

@Bean
public TcpNetServerConnectionFactory cf() {
return new TcpNetServerConnectionFactory(1234);
}

@Bean
public MyInboundGateway gate(TcpNetServerConnectionFactory cf) {
MyInboundGateway gate = new MyInboundGateway();
gate.setConnectionFactory(cf);
gate.setRequestChannel(queue());
return gate;
}

@Bean
public QueueChannel queue() {
return new QueueChannel();
}

public static class MyInboundGateway extends TcpInboundGateway implements ApplicationEventPublisherAware {

private ApplicationEventPublisher applicationEventPublisher;

@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}

public void recycle(int port) {
stop();
TcpNetServerConnectionFactory sf = new TcpNetServerConnectionFactory(port);
sf.setApplicationEventPublisher(this.applicationEventPublisher);
sf.afterPropertiesSet();
setConnectionFactory(sf);
afterPropertiesSet();
start();
}

}

}

我会打开调试日志记录以查看它是否为您提供任何线索。

您可能还想探索使用新的 DSL dynamic flow registration反而。 tcp-dynamic-client展示如何使用该技术动态添加/删除流程片段。它位于客户端,但可以在服务器端使用类似的技术来注册/注销您的网关和连接工厂。

关于java - 如何动态更改 tcp-ibound-gateway 使用的端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40084223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com