2014-04-11 59 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

非常感謝。 –