Spring integration Spring集成TCP入站适配器客户端断开连接问题

Spring integration Spring集成TCP入站适配器客户端断开连接问题,spring-integration,spring-integration-dsl,Spring Integration,Spring Integration Dsl,我们已经为入站TCP服务器创建了一个IntegrationFlow。无论何时我们进行测试,集成都是正常的,直到客户机断开连接,然后我们得到一个连续的空消息流 Spring集成5.2.5.1版本 流量: @Bean 公共集成流inboundFlow(MyService MyService){ 返回IntegrationFlows.from( Tcp.inboundAdapter( Tcp.netServer(12345) .deserializer(TcpCodecs.lengthHeader4(

我们已经为入站TCP服务器创建了一个IntegrationFlow。无论何时我们进行测试,集成都是正常的,直到客户机断开连接,然后我们得到一个连续的空消息流

Spring集成5.2.5.1版本

流量:

@Bean
公共集成流inboundFlow(MyService MyService){
返回IntegrationFlows.from(
Tcp.inboundAdapter(
Tcp.netServer(12345)
.deserializer(TcpCodecs.lengthHeader4())
.transform(Transformers.objectToString())
.log(INFO)//仅用于测试的日志记录
.handle(myService,“process”)//对消息进行一些处理
.get();
}
试验方法:

public void sendpackages()引发异常{
List files=getallfilesorted(“/some/location”);
try(套接字=新套接字(“localhost”,12345)){
log.info(“本地端口:{}”,socket.getLocalPort());
try(OutputStream OutputStream=socket.getOutputStream()){
对于(int i=0;i<10;i++){
Path=files.get(i);
log.info(“{}({}of{})”,path.toString(),i,files.size();
字节[]字节=文件。readAllBytes(路径);
outputStream.write(字节);
outputStream.flush();
睡眠(200);
}
}
}
}
测试完成后的部分日志:

2020-05-05 11:00:52.963  INFO 31793 --- [pool-1-thread-2] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=, headers={ip_tcp_remotePort=59407, ip_connectionId=localhost:59407:44745:df23388b-b8b0-4250-b926-4199030b2ff6, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=813ccf6b-9eb7-2ff3-0f8d-f2810966d8d7, ip_hostname=localhost, timestamp=1588672852963}]
2020-05-05 11:00:52.963  INFO 31793 --- [pool-1-thread-2] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=, headers={ip_tcp_remotePort=59407, ip_connectionId=localhost:59407:44745:df23388b-b8b0-4250-b926-4199030b2ff6, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=1fd5c4c7-b2e0-6a89-7de6-894c8e42e242, ip_hostname=localhost, timestamp=1588672852963}]
2020-05-05 11:00:52.963  INFO 31793 --- [pool-1-thread-2] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=, headers={ip_tcp_remotePort=59407, ip_connectionId=localhost:59407:44745:df23388b-b8b0-4250-b926-4199030b2ff6, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=78b852e8-0094-f8b9-8d05-37bc4912d096, ip_hostname=localhost, timestamp=1588672852963}]
2020-05-05 11:00:52.963  INFO 31793 --- [pool-1-thread-2] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=, headers={ip_tcp_remotePort=59407, ip_connectionId=localhost:59407:44745:df23388b-b8b0-4250-b926-4199030b2ff6, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=4e4a7ee0-c66e-9501-9ef7-4d25143531b3, ip_hostname=localhost, timestamp=1588672852963}]
2020-05-05 11:00:52.963  INFO 31793 --- [pool-1-thread-2] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=, headers={ip_tcp_remotePort=59407, ip_connectionId=localhost:59407:44745:df23388b-b8b0-4250-b926-4199030b2ff6, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=c90abe17-7036-94cc-af59-3b0cac5e75e4, ip_hostname=localhost, timestamp=1588672852963}]
2020-05-05 11:00:52.963  INFO 31793 --- [pool-1-thread-2] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=, headers={ip_tcp_remotePort=59407, ip_connectionId=localhost:59407:44745:df23388b-b8b0-4250-b926-4199030b2ff6, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=425d2e2a-75c5-0184-06da-6d27eb546931, ip_hostname=localhost, timestamp=1588672852963}]
2020-05-05 11:00:52.965  INFO 31793 --- [pool-1-thread-2] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=, headers={ip_tcp_remotePort=59407, ip_connectionId=localhost:59407:44745:df23388b-b8b0-4250-b926-4199030b2ff6, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=4c55d1d0-7421-1832-23ca-03744419c847, ip_hostname=localhost, timestamp=1588672852965}]
2020-05-05 11:00:52.965  INFO 31793 --- [pool-1-thread-2] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=, headers={ip_tcp_remotePort=59407, ip_connectionId=localhost:59407:44745:df23388b-b8b0-4250-b926-4199030b2ff6, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=05b6fea6-8f76-5c74-e4cc-33bd8099d8e8, ip_hostname=localhost, timestamp=1588672852965}]
2020-05-05 11:00:52.965  INFO 31793 --- [pool-1-thread-2] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=, headers={ip_tcp_remotePort=59407, ip_connectionId=localhost:59407:44745:df23388b-b8b0-4250-b926-4199030b2ff6, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=9b8fe16c-07f4-b64b-faec-794eb743559c, ip_hostname=localhost, timestamp=1588672852965}]
2020-05-05 11:00:52.965  INFO 31793 --- [pool-1-thread-2] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=, headers={ip_tcp_remotePort=59407, ip_connectionId=localhost:59407:44745:df23388b-b8b0-4250-b926-4199030b2ff6, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=222725e8-19c6-62b6-8ba7-103892191c96, ip_hostname=localhost, timestamp=1588672852965}]
2020-05-05 11:00:52.965  INFO 31793 --- [pool-1-thread-2] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=, headers={ip_tcp_remotePort=59407, ip_connectionId=localhost:59407:44745:df23388b-b8b0-4250-b926-4199030b2ff6, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=951403b6-18a9-ffe9-29b7-67b8c01ea311, ip_hostname=localhost, timestamp=1588672852965}]
2020-05-05 11:00:52.965  INFO 31793 --- [pool-1-thread-2] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=, headers={ip_tcp_remotePort=59407, ip_connectionId=localhost:59407:44745:df23388b-b8b0-4250-b926-4199030b2ff6, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=2ac3002b-ccd1-9506-f3aa-60f4b63e26b8, ip_hostname=localhost, timestamp=1588672852965}]
2020-05-05 11:00:52.965  INFO 31793 --- [pool-1-thread-2] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=, headers={ip_tcp_remotePort=59407, ip_connectionId=localhost:59407:44745:df23388b-b8b0-4250-b926-4199030b2ff6, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=dbbf12cf-0a27-407b-0a96-5b27ab0539db, ip_hostname=localhost, timestamp=1588672852965}]
2020-05-05 11:00:52.965  INFO 31793 --- [pool-1-thread-2] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=, headers={ip_tcp_remotePort=59407, ip_connectionId=localhost:59407:44745:df23388b-b8b0-4250-b926-4199030b2ff6, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=3282d09e-9508-b053-3763-cc23d3c817a8, ip_hostname=localhost, timestamp=1588672852965}]
2020-05-05 11:00:52.966  INFO 31793 --- [pool-1-thread-2] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=, headers={ip_tcp_remotePort=59407, ip_connectionId=localhost:59407:44745:df23388b-b8b0-4250-b926-4199030b2ff6, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=549b2326-341f-19c0-db98-6b31da1901d8, ip_hostname=localhost, timestamp=1588672852966}]

日志中没有关于客户端断开连接的信息。我们将在日志中看到套接字关闭活动

日志表明我们正在接收包含
0x00000000
的数据包

使用wireshark或类似工具查看导线上的活动

编辑

您的自定义反序列化程序导致此问题

if(读取<0){
返回0;
}
当检测到套接字关闭时,需要抛出
SoftEndOfStreamException

这一点在本文中进行了解释

当反序列化程序在消息之间检测到一个关闭的输入流时,它必须抛出一个
SoftEndOfStreamException
;这是向框架发出的一个信号,表明关闭是“正常的”。如果在解码消息时关闭了流,则应引发其他一些异常


我不明白这怎么可能;为
org.springframework.integration
启用调试日志记录。如果这不能解释发生了什么;将日志张贴到某个地方(如pastebin),并在此处评论您已经这样做了。您还应该编辑该问题,以显示您正在使用的Spring Integration的哪个版本。@GaryRussell上载了启用调试的日志-您可以在第137行看到第一条空消息。我看不到任何关于这一点的东西可能表明是什么导致了这一点。
com.hawkeyeinnovations.netsing.scoringprovider.config.SmartDirectorTCPIT$TestClientConfig';来源:“bean方法客户端”
。看起来您已经为该测试环境配置了一些
客户端。可能它连接到同一台服务器…@ArtemBilan是的,我忘了删除那个客户端。移除后再次运行,行为相同。如果我将test
sendpackages
方法提取到它自己的java应用程序中,我会得到相同的行为(为了方便起见一直在
@springbootest
中运行)@ArtemBilan创建了一个简单的项目-Wireshark没有显示包含
0x00000000
的数据包。在最后一个成功的数据包之后,我看到一个FIN来自客户端套接字连接,然后另一个FIN来自服务器到客户端;FIN应导致套接字关闭并终止
池-1-thread-2
。您的示例使用的是自定义去奇异化器,而您的问题有一个标准问题。请参阅对我的答案的编辑。您还应该调用
read()
方法,而不是
inputStream.read()
,以获取标题。在一次读取中不可能获得全部4个字节的可能性很小(但不是零)。