2016-04-21 66 views
2

我想測試一個簡單的tcp發送/接收序列使用柑橘框架。我能夠很好地發送消息,但接收消息證明存在問題。基本上,柑橘和柑橘沒有檢測到答覆,然後超時。從Citrix框架和春季集成tcp服務器接收郵件超時

柑橘bean配置:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:citrus="http://www.citrusframework.org/schema/config" 
    xmlns:int="http://www.springframework.org/schema/integration" 
    xmlns:int-ip="http://www.springframework.org/schema/integration/ip" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 

    http://www.citrusframework.org/schema/config http://www.citrusframework.org/schema/config/citrus-config.xsd 
    http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd 
    http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd"> 

<citrus:channel-endpoint id="citrusServiceInputEndpoint" 
         channel-name="input" /> 

<citrus:channel-endpoint id="citrusServiceRepliesEndpoint" 
         channel-name="replies" /> 

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

<int:channel id="input" /> 

<int:channel id="replies"> 
    <int:queue capacity="10" /> 
</int:channel> 

<int-ip:tcp-outbound-channel-adapter 
     id="outboundClient" channel="input" connection-factory="client" /> 

<int-ip:tcp-inbound-channel-adapter 
     id="inboundClient" channel="replies" connection-factory="client" /> 

柑橘測試用例:

@Test 
@CitrusTest(name = "sendSpringIntegrationMessageTest") 
public void sendSpringIntegrationMessageTest() throws Exception { 
    send("citrusServiceInputEndpoint").payload("Req"); 
    receive("citrusServiceRepliesEndpoint").payload("Rsp"); 
} 

im使用一個簡單的netcat服務器進行測試:

nc -kl 9123 

運行測試用例時,「Req」文本在netcat上正常運行。但是,在netcat上輸入「Rsp」時,數據不會到達柑橘測試。大約5秒後,測試用例失敗,並顯示以下消息:

com.consol.citrus.exceptions.TestCaseFailedException: Test case failed 

at com.consol.citrus.TestCase.executeAction(TestCase.java:218) 
at com.consol.citrus.TestCase.doExecute(TestCase.java:139) 
at com.consol.citrus.actions.AbstractTestAction.execute(AbstractTestAction.java:42) 
at com.consol.citrus.Citrus.run(Citrus.java:243) 
at com.consol.citrus.dsl.junit.JUnit4CitrusTest.invokeTestMethod(JUnit4CitrusTest.java:90) 
at com.consol.citrus.dsl.junit.JUnit4CitrusTestDesigner.invokeTestMethod(JUnit4CitrusTestDesigner.java:68) 
at com.consol.citrus.dsl.junit.JUnit4CitrusTest.run(JUnit4CitrusTest.java:71) 
at com.consol.citrus.junit.CitrusJUnit4Runner$InvokeRunMethod.evaluate(CitrusJUnit4Runner.java:212) 
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) 
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) 
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) 
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) 
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:483) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 
Caused by: com.consol.citrus.exceptions.ActionTimeoutException: Action timeout while receiving message from channel 'replies' 
    at com.consol.citrus.channel.ChannelConsumer.receive(ChannelConsumer.java:97) 
    at com.consol.citrus.messaging.AbstractSelectiveMessageConsumer.receive(AbstractSelectiveMessageConsumer.java:50) 
    at com.consol.citrus.actions.ReceiveMessageAction.receive(ReceiveMessageAction.java:146) 
    at com.consol.citrus.actions.ReceiveMessageAction.doExecute(ReceiveMessageAction.java:125) 
    at com.consol.citrus.actions.AbstractTestAction.execute(AbstractTestAction.java:42) 
    at com.consol.citrus.TestCase.executeAction(TestCase.java:211) 
    ... 33 more 

因此基本上Citrus沒有收到響應。請注意,我必須爲回覆通道定義一個隊列,因爲這是Citrus所期望的,回覆通道必須實現Interface PollableChannel。

請幫我理解爲什麼Citrus隊列中沒有收到數據?

回答

2

默認情況下,tcp連接工廠預計消息由\r\n分隔。

nc僅終止響應\n因此連接工廠看不到完整的消息。

查看discussion in the documentation about serializers/deserializers。您需要將serializerdeserializer配置爲指向一個ByteArrayLfSerializer bean,它只是期望\n來終止該消息。

+0

感謝這個解決我的問題! – mdewit

0

這是正確的bean配置,使這個測試工作。按照Gary的建議添加串行器/解串器。

我還必須添加一個消息轉換器(對象到字符串轉換器),因爲Spring在回覆通道上提供的數據將是byte []類型。 Citrus希望對字符串類型進行驗證,因此需要進行轉換以將字節數組轉換爲字符串。

希望它能幫助未來的人!

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:citrus="http://www.citrusframework.org/schema/config" 
    xmlns:int="http://www.springframework.org/schema/integration" 
    xmlns:int-ip="http://www.springframework.org/schema/integration/ip" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 

http://www.citrusframework.org/schema/config http://www.citrusframework.org/schema/config/citrus-config.xsd 
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd 
http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd"> 

<citrus:channel-endpoint id="citrusServiceInputEndpoint" 
         channel-name="input" /> 

<citrus:channel-endpoint id="citrusServiceRepliesEndpoint" 
         channel-name="response" /> 

<int-ip:tcp-connection-factory id="client" 
           type="client" host="localhost" port="9123" single-use="false" 
           so-timeout="10000" using-nio="true" deserializer="javaSerializer" 
           serializer="javaSerializer" /> 

<bean id="javaSerializer" 
     class="org.springframework.integration.ip.tcp.serializer.ByteArrayLfSerializer" /> 

<int:channel id="input" /> 

<int:channel id="replies"> 
</int:channel> 

<int:channel id="response"> 
    <int:queue /> 
</int:channel> 

<int:object-to-string-transformer id="transformer" input-channel="replies" output-channel="response" /> 

<int-ip:tcp-outbound-channel-adapter 
     id="outboundClient" channel="input" connection-factory="client" /> 

<int-ip:tcp-inbound-channel-adapter 
     id="inboundClient" channel="replies" connection-factory="client" />