首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TcpOutboundGateway -无法关联响应-没有挂起的答复

TcpOutboundGateway -无法关联响应-没有挂起的答复
EN

Stack Overflow用户
提问于 2014-04-11 20:27:39
回答 1查看 1.3K关注 0票数 1

我使用spring集成来创建TCP服务器,并测试它是否与junit一起工作。问题是,我收到了一个错误: org.springframework.integration.ip.tcp.TcpOutboundGateway -无法关联响应-没有挂起的答复。请帮我修一下。这是更多的信息。我有向服务器发送一些数据的单元测试,服务器必须在每一部分数据上回答“成功”。但是在读取数据的第二部分之后,TcpOutboundGateway (在单元测试端)写错了日志。

因此,服务器配置文件:

代码语言:javascript
复制
<int-ip:tcp-connection-factory id="crLfServer"
    type="server"
    port="5000"
    single-use="false"
    so-timeout="10000"
     />

<task:executor id="pool" pool-size="16"/>

<int-ip:tcp-inbound-gateway id="gatewayCrLf"
    connection-factory="crLfServer"
    request-channel="serverBytes2StringChannel"
    error-channel="errorChannel"/>

<int:channel id="toSA" >
    <int:dispatcher task-executor="pool" />
</int:channel>

<int:service-activator input-channel="toSA"
    ref="connectionHandler"
    method="handleData" />


<bean id="connectionHandler" class="com.pc.tracker.utils.ConnectionHandler" />
<bean id="objectMapper" class="org.codehaus.jackson.map.ObjectMapper"/>

<int:transformer id="serverBytes2String"
    input-channel="serverBytes2StringChannel"
    output-channel="toSA"
    expression="new String(payload).trim()"/>

<int:transformer id="errorHandler"
    input-channel="errorChannel"
    expression="payload.failedMessage.payload + ':' + payload.cause.message"/>

客户端配置文件:

代码语言:javascript
复制
<int:gateway id="gw"
    service-interface="com.pc.tracker.tcp.ConnectionHandlerTestHellperGateway"
    default-request-channel="input"/>

<int-ip:tcp-connection-factory id="client"
    type="client"
    host="localhost"
    port="5000"
    single-use="false"
    so-timeout="10000"/>

<int:channel id="input" />

<int-ip:tcp-outbound-gateway id="outGateway"
    request-channel="input"
    reply-channel="clientBytes2StringChannel"
    connection-factory="client"
    request-timeout="10000"
    reply-timeout="10000"/>

<int:transformer id="clientBytes2String"
    input-channel="clientBytes2StringChannel"
    expression="new String(payload)"/>

以及产生已经提到的错误的测试。

代码语言:javascript
复制
    @Test
public void testRecivedBackupedData() {
    String testData = 
             "[{\"s\":1,\"t\":0,\"b\":0,\"sp\":0,\"long\":0,\"sa\":0,\"lat\":0,\"a\":0,\"i\":\"device_for_unit_tests\"},"
            + "{\"s\":12,\"t\":0,\"b\":0,\"sp\":0,\"long\":0,\"sa\":0,\"lat\":0,\"a\":0,\"i\":\"device_for_unit_tests\"}]\r\n"
           + "[{\"s\":13,\"t\":0,\"b\":0,\"sp\":0,\"long\":0,\"sa\":0,\"lat\":0,\"a\":0,\"i\":\"device_for_unit_tests\"},"
            + "{\"s\":24,\"t\":0,\"b\":0,\"sp\":0,\"long\":0,\"sa\":0,\"lat\":0,\"a\":0,\"i\":\"device_for_unit_tests\"}]\r\n"
            + "[{\"s\":1,\"t\":0,\"b\":0,\"sp\":0,\"long\":0,\"sa\":0,\"lat\":0,\"a\":0,\"i\":\"device_for_unit_tests\"},"
            + "{\"s\":12,\"t\":0,\"b\":0,\"sp\":0,\"long\":0,\"sa\":0,\"lat\":0,\"a\":0,\"i\":\"device_for_unit_tests\"}]\r\n"
            +"[{\"s\":1,\"t\":0,\"b\":0,\"sp\":0,\"long\":0,\"sa\":0,\"lat\":0,\"a\":0,\"i\":\"device_for_unit_tests\"},"
            + "{\"s\":12,\"t\":0,\"b\":0,\"sp\":0,\"long\":0,\"sa\":0,\"lat\":0,\"a\":0,\"i\":\"device_for_unit_tests\"}]\r\n";
    String result = gw.send(testData);

这是带有错误的日志

代码语言:javascript
复制
    2014-04-11 23:12:23,059 [pool-1] ERROR com.pc.tracker.utils.ConnectionHandler - recived data: [{"s":1,"t":0,"b":0,"sp":0,"long":0,"sa":0,"lat":0,"a":0,"i":"device_for_unit_tests"},{"s":12,"t":0,"b":0,"sp":0,"long":0,"sa":0,"lat":0,"a":0,"i":"device_for_unit_tests"}]
2014-04-11 23:12:23,263 [pool-1] ERROR com.pc.tracker.utils.ConnectionHandler - parsisted
2014-04-11 23:12:23,265 [pool-2] ERROR com.pc.tracker.utils.ConnectionHandler - recived data: [{"s":13,"t":0,"b":0,"sp":0,"long":0,"sa":0,"lat":0,"a":0,"i":"device_for_unit_tests"},{"s":24,"t":0,"b":0,"sp":0,"long":0,"sa":0,"lat":0,"a":0,"i":"device_for_unit_tests"}]
2014-04-11 23:12:23,330 [pool-2] ERROR com.pc.tracker.utils.ConnectionHandler - parsisted
2014-04-11 23:12:23,331 [pool-2-thread-1] ERROR org.springframework.integration.ip.tcp.TcpOutboundGateway - Cannot correlate response - no pending reply
2014-04-11 23:12:23,332 [pool-3] ERROR com.pc.tracker.utils.ConnectionHandler - recived data: [{"s":1,"t":0,"b":0,"sp":0,"long":0,"sa":0,"lat":0,"a":0,"i":"device_for_unit_tests"},{"s":12,"t":0,"b":0,"sp":0,"long":0,"sa":0,"lat":0,"a":0,"i":"device_for_unit_tests"}]
2014-04-11 23:12:23,409 [pool-3] ERROR com.pc.tracker.utils.ConnectionHandler - parsisted
2014-04-11 23:12:23,409 [pool-2-thread-1] ERROR org.springframework.integration.ip.tcp.TcpOutboundGateway - Cannot correlate response - no pending reply
2014-04-11 23:12:23,410 [pool-4] ERROR com.pc.tracker.utils.ConnectionHandler - recived data: [{"s":1,"t":0,"b":0,"sp":0,"long":0,"sa":0,"lat":0,"a":0,"i":"device_for_unit_tests"},{"s":12,"t":0,"b":0,"sp":0,"long":0,"sa":0,"lat":0,"a":0,"i":"device_for_unit_tests"}]
2014-04-11 23:12:23,487 [pool-4] ERROR com.pc.tracker.utils.ConnectionHandler - parsisted
2014-04-11 23:12:23,488 [pool-2-thread-1] ERROR org.springframework.integration.ip.tcp.TcpOutboundGateway - Cannot correlate response - no pending reply
2014-04-11 23:12:23,489 [pool-5] ERROR com.pc.tracker.utils.ConnectionHandler - recived data: 
2014-04-11 23:12:23,490 [pool-2-thread-1] ERROR org.springframework.integration.ip.tcp.TcpOutboundGateway - Cannot correlate response - no pending reply

我花了两天时间解决这个问题。很抱歉提了很长的问题。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-12 14:22:47

你用嵌入式\r\n发送数据..。

代码语言:javascript
复制
public void testRecivedBackupedData() {
    String testData = 
         "[{\"s\":1,\"t\":0,\"b\":0,\"sp\":0,\"long\":0,\"sa\":0,\"lat\":0,\"a\":0,\"i\":\"device_for_unit_tests\"},"
        + "{\"s\":12,\"t\":0,\"b\":0,\"sp\":0,\"long\":0,\"sa\":0,\"lat\":0,\"a\":0,\"i\":\"device_for_unit_tests\"}]\r\n"
       + "[{\"s\":13,\"t\":0,\"b\":0,\"sp\":0,\"long\":0,\"sa\":0,\"lat\":0,\"a\":0,\"i\":\"device_for_unit_tests\"},"
        + "{\"s\":24,\"t\":0,\"b\":0,\"sp\":0,\"long\":0,\"sa\":0,\"lat\":0,\"a\":0,\"i\":\"device_for_unit_tests\"}]\r\n"
        + "[{\"s\":1,\"t\":0,\"b\":0,\"sp\":0,\"long\":0,\"sa\":0,\"lat\":0,\"a\":0,\"i\":\"device_for_unit_tests\"},"
        + "{\"s\":12,\"t\":0,\"b\":0,\"sp\":0,\"long\":0,\"sa\":0,\"lat\":0,\"a\":0,\"i\":\"device_for_unit_tests\"}]\r\n"
        +"[{\"s\":1,\"t\":0,\"b\":0,\"sp\":0,\"long\":0,\"sa\":0,\"lat\":0,\"a\":0,\"i\":\"device_for_unit_tests\"},"
        + "{\"s\":12,\"t\":0,\"b\":0,\"sp\":0,\"long\":0,\"sa\":0,\"lat\":0,\"a\":0,\"i\":\"device_for_unit_tests\"}]\r\n";
String result = gw.send(testData);

将它们全部删除;框架将在最后添加一个。

您的服务器正在套接字上为一个请求发送多个回复。搜索连接id localhost:5000:51420:9f93417a-c879-4753-a61e-0b9485940d14。您将看到您发送您的请求

代码语言:javascript
复制
2014-04-12 11:27:11,307

收到答复的网址是:

代码语言:javascript
复制
2014-04-12 11:27:11,340

(onMessage() call)并发送到

代码语言:javascript
复制
2014-04-12 11:27:11,343

该套接字上的下一个活动是接收另一条消息。

代码语言:javascript
复制
2014-04-12 11:27:11,346

因为没有人在等待,因此出现了错误消息。

现在,看看连接id,我们可以看到远程套接字是51420,所以让我们看看服务器端.

服务器上相应的连接id是localhost:51420:5000:66862ff3-3f40-4291-938f-0694bf3727be。对原始请求的答复success被发送到

代码语言:javascript
复制
2014-04-12 11:27:11,339

但是,同一个线程读取另一条消息(没有另一条发送)--注意消息Available to read:525

因此,底线是您使用的是默认(反)序列化程序,它期望消息以\r\n结束,但您发送的请求中嵌入了\r\n,因此当发送方只发送一条消息时,服务器端将“看到”多个请求。

TCP是一种流协议--您需要以某种方式对数据进行帧设置,以便服务器端知道消息何时完成。如果需要发送包含\r\n的数据,则需要使用不同的反序列化器来检测数据的末尾(可能是长度头实现,这是最有效的)。

可用的标准序列化程序和有关自定义它们的信息是记录在这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23021978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档