2014-04-11 31 views
0

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

So Server configuration file:

<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 

我花了两天时间解决问题。 对于很长的问题感到抱歉。 谢谢。

+0

您使用的是什么版本的Spring集成? –

+0

我用Spring集成3.0.0 –

+0

这对我来说没有意义;它意味着使用回复消息返回到网关的消息头有问题 - “ip_connectionId”用于将回复与响应关联起来。没有什么能从你的配置中跳出来;我建议你打开'TRACE'级日志记录,并将结果与​​tcp-client-server样本https://github.com/spring-projects/spring-integration-samples/tree/master/basic/tcp-client-服务器,看看你是否可以看到一些差异。 StackOverflow不允许它,但如果你可以在任何地方发布日志,我可以看一看。 –

回答

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()调用)被接收,并在

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

所以,底线是你使用的是默认的(反)序列,其期待与\r\n被终止的消息,但您要发送具有\r\n嵌入了请求,这样服务器端“看到”多个请求时发件人只发送一条消息。

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

可用的标准串行器和有关定制它们的信息是documented here

+0

非常感谢。 –