TcpOutboundGateway - 无法关联响应 - 没有待处理的回复

时间:2014-04-11 20:27:40

标签: spring spring-integration tcpserver

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

  

所以服务器配置文件:

<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"/>

客户端配置文件:

<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)"/>

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

    @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);

这是带错误的日志

    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

我花了两天时间解决问题。 抱歉很久没问题。 感谢。

1 个答案:

答案 0 :(得分:0)

您正在发送嵌入了\r\n ...

的数据
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。您将看到您在

发送了请求
2014-04-12 11:27:11,307

收到回复
2014-04-12 11:27:11,340

(onMessage()call)并发送到

的回复频道
2014-04-12 11:27:11,343

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

2014-04-12 11:27:11,346

没有人等待错误信息。

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

服务器上的相应连接ID为localhost:51420:5000:66862ff3-3f40-4291-938f-0694bf3727be。原始请求的回复success已在

发送
2014-04-12 11:27:11,339

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

所以,最重要的是,您使用的是默认(de)序列化程序,它希望消息以\r\n终止,但您发送的请求中嵌入了\r\n,因此服务器端当发件人只发送一条消息时,“看到”多个请求。

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

可用的标准序列化程序和有关自定义它们的信息是documented here