我的 spring boot kotlin 应用程序需要满足这些:
这就是我所拥有的(简化的):
@Bean
fun inboundFlow(connectionFactory: AbstractClientConnectionFactory): IntegrationFlow {
val inbound = TcpReceivingChannelAdapter()
inbound.isClientMode = true
inbound.isAutoStartup = false
inbound.setConnectionFactory(connectionFactory)
return IntegrationFlow.from(inbound)
.channel(requests())
.get()
}
@Bean
fun outboundFlow(connectionFactory: AbstractClientConnectionFactory): IntegrationFlow {
val outbound = TcpSendingMessageHandler()
outbound.isClientMode = true
outbound.setConnectionFactory(connectionFactory)
return IntegrationFlow.from(replies())
.transform(MyTransformer::toByteArray)
.handle(outbound)
.get()
}
@Bean
fun connectionFactory(): AbstractClientConnectionFactory =
Tcp.nioClient(hostname, port)
.singleUseConnections(false)
.get()
@Bean
fun myFlow(connectionFactory: AbstractClientConnectionFactory,
myReceiverService: MyReceiverService): IntegrationFlow? {
return IntegrationFlow.from(requests())
.transform(MessageTransformer::transformMyMessage)
.handle { msg: Message<MyMessage> ->
myReceiverService.handleMessage(msg)
}
.get()
}
一切都按预期工作,除了连接控制。
显示的代码中出现不需要的行为:Spring 在 Spring Boot 应用程序启动时自动打开连接(当远程服务器此时未侦听时,会导致
connection refused
异常)。而且我也没有办法告诉它“现在连接”。
想要但缺失的行为:
补充意见: 显然
isAutoStartup=false
并没有达到我的预期。我想知道这是否与轮询有关,是否有办法调整轮询以抑制自动连接?
我还了解了使用 spring 配置文件实现连接/断开连接的可能性,即通过将相应的连接流与配置文件关联,然后“动态激活”配置文件。然而,我所看到的采用这种方法的每个解决方案还涉及重新启动整个 Spring Boot 应用程序,这不仅是不必要的延迟,而且还会(我假设这里没有测试它)重置我的其他服务的状态,这对于我的情况来说是不可接受的。如果我可以这么说的话,对我来说这并不是一个优雅的解决方案。
我读过的另一件事是控制通道的可能性,但我不知道这如何与我的示例一起工作。
谢谢您的建议。