2017-06-03 111 views
1

我創建了一個TcpClient的與SI與cusctom serialzer和deserialzer它發送一個字符串的「kksingh」序列化的字節數組服務器並接收響應,字節數組,反序列化迴響應string.I能夠發送請求到服務器,但我無法得到迴應。 但它表明在日誌中,從池線程1已收到味精,但我的主線程是不是能夠得到它。 我TcpClientclassTCP客戶端無法從服務器獲取

public final class Main 
    { 

     /** 
     * Prevent instantiation. 
     */ 
     private Main() 
     { 
     } 


      /** 
      * Load the Spring Integration Application Context 
      * 
      * @param args 
      *   - command line arguments 
      */ 
      public static void main(final String... args) 
      { 


       System.out.println("\n=========================================================" 
         + "\n               " 
         + "\n Welcome to the Spring Integration     " 
         + "\n   TCP-Client-Server Sample!      " 
         + "\n               " 
         + "\n For more information please visit:     " 
         + "\n http://www.springintegration.org/     " 
         + "\n               " 
         + "\n========================================================="); 

       final GenericXmlApplicationContext context = Main.setupContext(); 
       final SimpleGateway gateway = context.getBean(SimpleGateway.class); 

       System.out.println("Please enter some text and press <enter>: "); 
       System.out.println("\tNote:"); 
       System.out.println("\t- Entering FAIL will create an exception"); 
       System.out.println("\t- Entering q will quit the application"); 
       System.out.print("\n"); 
       System.out.println("\t--> Please also check out the other samples, " + "that are provided as JUnit tests."); 

       System.out.println("main" + Thread.currentThread().getName()); 
       final String msg = gateway.send("kksingh"); 
       System.out.println(Thread.currentThread().getName() + "---response--" + msg); 

       System.out.println("Exiting application...bye."); 
       System.exit(0); 

      } 

      public static GenericXmlApplicationContext setupContext() 
      { 
       final GenericXmlApplicationContext context = new GenericXmlApplicationContext(); 

       context.load("classpath:META-INF/spring/integration/tcpClientServerDemo-context.xml"); 
       context.registerShutdownHook(); 
       context.refresh(); 
       return context; 
      } 
     } 
     Gatewayinterface 
     public interface SimpleGateway { 

      public String send(String text); 

     } 
     Custom serialzer and deserialzer 
     public class UCCXImprovedSerializer implements Serializer<String>, Deserializer<String> 
     { 
      @Override 
      public String deserialize(InputStream initialStream) throws IOException 
      { 

       byte binput[] = new byte[initialStream.available()]; 
       initialStream.read(binput); 
       String target = new String(binput); 
       System.out.println(Thread.currentThread().getName() + "---reply deserialize----" + target); 
       return target; 
      } 

      @Override 
      public void serialize(String msg, OutputStream os) throws IOException 
      { 
       System.out.println(msg + "---serialize---" + Thread.currentThread().getName()); 
       os.write(msg.getBytes()); 
      } 

     } 

我clientserverdemo-context.xml中:

<bean id="UCCXImprovedSerializerDeserialier" 
    class="org.springframework.integration.samples.tcpclientserver.UCCXImprovedSerializer" /> 
<int:gateway id="gw" 
    service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway" 
    default-request-channel="input"/> 
<int:channel id="input" /> 
<int-ip:tcp-connection-factory id="client" 
    type="client" host="localhost" port="9999" single-use="true" 
    serializer="UCCXImprovedSerializerDeserialier" 
    deserializer="UCCXImprovedSerializerDeserialier" mapper="mapper" /> 
<int-ip:tcp-outbound-gateway id="outGateway" 
    request-channel="input" connection-factory="client" request-timeout="10000"/> 
<bean id="mapper" 
    class="org.springframework.integration.samples.tcpclientserver.CustomMapper"> 
    </bean> 
    </beans> 

輸出有時

 kksingh---serialize---main 
     00:00:10.690 [main] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - localhost:9999:49662:ff770a35-0e6c-401a-a79d-14439ccf345c Message sent GenericMessage [payload=kksingh, headers={replyChannel=org.springframewor[email protected]7ee8290b, errorChannel=org.springframewor[email protected]7ee8290b, id=d6332cbd-24b0-4300-eb9e-f2a5e7b07148, timestamp=1496514610673}] 
     00:00:10.691 [pool-1-thread-1] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - localhost:9999:49662:ff770a35-0e6c-401a-a79d-14439ccf345c Reading... 
     pool-1-thread-1---reply deserialize----kksingh 
     00:00:10.692 [pool-1-thread-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'messageBuilderFactory' 
     00:00:10.692 [pool-1-thread-1] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - Message 
received GenericMessage [payload=kksingh, headers={ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:49662:ff770a35-0e6c-401a-a79d-14439ccf345c, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=b14c7c14-dde6-551d-0cd0-5fa71af42ead, ip_hostname=localhost, timestamp=1496514610692}] 
     pool-1-thread-1---reply deserialize---- 
     00:00:10.693 [main] DEBUG org.springframework.integration.ip.tcp.TcpOutboundGateway - Response GenericMessage [payload=kksingh, headers={ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:49662:ff770a35-0e6c-401a-a79d-14439ccf345c, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=b14c7c14-dde6-551d-0cd0-5fa71af42ead, ip_hostname=localhost, timestamp=1496514610692}] 
     00:00:10.693 [main] DEBUG org.springframework.integration.ip.tcp.TcpOutboundGateway - Removed pending reply localhost:9999:49662:ff770a35-0e6c-401a-a79d-14439ccf345c 
     00:00:10.696 [main] DEBUG org.springframework.integration.channel.DirectChannel - preSend on channel 'output', message: GenericMessage [payload=kksingh, headers={replyChannel=org.springframewor[email protected]7ee8290b, errorChannel=org.springframewor[email protected]7ee8290b, ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:49662:ff770a35-0e6c-401a-a79d-14439ccf345c, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=32afced5-e1dd-9b93-4407-e7e48087f333, ip_hostname=localhost, timestamp=1496514610696}] 
     00:00:10.699 [main] DEBUG org.springframework.integration.handler.BridgeHandler - [email protected] received message: GenericMessage [payload=kksingh, headers={replyChannel=org.springframewor[email protected]7ee8290b, errorChannel=org.springframewor[email protected]7ee8290b, ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:49662:ff770a35-0e6c-401a-a79d-14439ccf345c, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=32afced5-e1dd-9b93-4407-e7e48087f333, ip_hostname=localhost, timestamp=1496514610696}] 
     00:00:10.700 [main] DEBUG org.springframework.integration.channel.DirectChannel - postSend (sent=true) on channel 'output', message: GenericMessage [payload=kksingh, headers={replyChannel=org.springframework.messaging.cor`enter code here`[email protected], errorChannel=org.springframewor[email protected]7ee8290b, ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:49662:ff770a35-0e6c-401a-a79d-14439ccf345c, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=32afced5-e1dd-9b93-4407-e7e48087f333, ip_hostname=localhost, timestamp=1496514610696}] 
     00:00:10.701 [main] DEBUG org.springframework.integration.channel.DirectChannel - postSend (sent=true) on channel 'input', message: GenericMessage [payload=kksingh, headers={replyChannel=org.springframewor[email protected]7ee8290b, errorChannel=org.springframewor[email protected]7ee8290b, id=d6332cbd-24b0-4300-eb9e-f2a5e7b07148, timestamp=1496514610673}] 
     00:00:10.701 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'integrationConversionService' 
     main---response--kksingh 
     Exiting application...bye. 

輸出有時

mainmain 

    00:10:25.227 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'integrationGlobalProperties' 
    00:10:25.227 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'messageBuilderFactory' 
    00:10:25.227 [main] INFO org.springframework.integration.endpoint.EventDrivenConsumer - Adding {bridge:null} as a subscriber to the 'output' channel 
    00:10:25.227 [main] INFO org.springframework.integration.channel.DirectChannel - Channel 'o[email protected]cac736f.output' has 1 subscriber(s). 
    00:10:25.227 [main] INFO org.springframework.integration.endpoint.EventDrivenConsumer - started [email protected]b269 
    00:10:25.228 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'integrationEvaluationContext' 
    00:10:25.234 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'integrationGlobalProperties' 
    00:10:25.235 [main] DEBUG org.springframework.integration.channel.DirectChannel - preSend on channel 'input', message: GenericMessage [payload=kksingh, headers={replyChannel=org.springframewor[email protected]7ee8290b, errorChannel=org.springframewor[email protected]7ee8290b, id=f43023da-3596-8cf2-4439-45496c087cd9, timestamp=1496515225235}] 
    00:10:25.235 [main] DEBUG org.springframework.integration.ip.tcp.TcpOutboundGateway - org.springframework.integration.ip.tcp.TcpOutboundGateway#0 received message: GenericMessage [payload=kksingh, headers={replyChannel=org.springframewor[email protected]7ee8290b, errorChannel=org.springframewor[email protected]7ee8290b, id=f43023da-3596-8cf2-4439-45496c087cd9, timestamp=1496515225235}] 
    00:10:25.235 [main] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory - Opening new socket connection to localhost:9999 
    00:10:25.244 [main] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - New connection localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c 
    00:10:25.244 [main] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory - client: Added new connection: localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c 
    00:10:25.245 [pool-1-thread-1] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c Reading... 
    pool-1-thread-1---reply deserialize---- 
    00:10:25.246 [pool-1-thread-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'messageBuilderFactory' 
    00:10:25.246 [pool-1-thread-1] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - Message received GenericMessage [payload=, headers={ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=008f25f9-2a8f-8bf7-b960-d9c9ff6c4d5e, ip_hostname=localhost, timestamp=1496515225246}] 
    00:10:25.246 [pool-1-thread-1] ERROR org.springframework.integration.ip.tcp.TcpOutboundGateway - Cannot correlate response - no pending reply for localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c 
    pool-1-thread-1---reply deserialize---- 
    00:10:25.247 [pool-1-thread-1] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - Message received GenericMessage [payload=, headers={ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=309799b8-f7fc-775c-2ded-d78d4106aa3c, ip_hostname=localhost, timestamp=1496515225247}] 
    00:10:25.247 [pool-1-thread-1] ERROR org.springframework.integration.ip.tcp.TcpOutboundGateway - Cannot correlate response - no pending reply for localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c 
    pool-1-thread-1---reply deserialize---- 
    00:10:25.247 [pool-1-thread-1] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - Message received GenericMessage [payload=, headers={ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=52f47f78-f278-714d-0883-5e0d55930a2e, ip_hostname=localhost, timestamp=1496515225247}] 
    00:10:25.247 [main] DEBUG org.springframework.integration.ip.tcp.TcpOutboundGateway - Added pending reply localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c 
    pool-1-thread-1---reply deserialize---- 
    kksingh---serialize---main 
    00:10:25.247 [pool-1-thread-1] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - Message received GenericMessage [payload=, headers={ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=b2ba4463-6b41-3706-c4da-ef2f8244ac63, ip_hostname=localhost, timestamp=1496515225247}] 
    pool-1-thread-1---reply deserialize---- 
    00:10:25.248 [main] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c Message sent GenericMessage [payload=kksingh, headers={replyChannel=org.springframewor[email protected]7ee8290b, errorChannel=org.springframewor[email protected]7ee8290b, id=f43023da-3596-8cf2-4439-45496c087cd9, timestamp=1496515225235}] 
    00:10:25.248 [main] DEBUG org.springframework.integration.ip.tcp.TcpOutboundGateway - Response GenericMessage [payload=, headers={ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=52f47f78-f278-714d-0883-5e0d55930a2e, ip_hostname=localhost, timestamp=1496515225247}] 
    00:10:25.248 [main] DEBUG org.springframework.integration.ip.tcp.TcpOutboundGateway - Removed pending reply localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c 
    00:10:25.248 [pool-1-thread-1] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - Message received GenericMessage [payload=, headers={ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=87bc891c-1067-1e79-79ec-3c203641cff4, ip_hostname=localhost, timestamp=1496515225248}] 
    00:10:25.248 [pool-1-thread-1] ERROR org.springframework.integration.ip.tcp.TcpOutboundGateway - Cannot correlate response - no pending reply for localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c 
    00:10:25.249 [main] DEBUG org.springframework.integration.channel.DirectChannel - preSend on channel 'output', message: GenericMessage [payload=, headers={replyChannel=org.springframewor[email protected]7ee8290b, errorChannel=org.springframewor[email protected]7ee8290b, ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=af31dde1-93ec-7209-b3c0-1c4d4e0a4949, ip_hostname=localhost, timestamp=1496515225249}] 
    00:10:25.249 [pool-1-thread-1] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - Read exception localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c SocketException:Socket is closed 
    00:10:25.249 [main] DEBUG org.springframework.integration.handler.BridgeHandler - [email protected] received message: GenericMessage [payload=, headers={replyChannel=org.springframewor[email protected]7ee8290b, errorChannel=org.springframewor[email protected]7ee8290b, ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=af31dde1-93ec-7209-b3c0-1c4d4e0a4949, ip_hostname=localhost, timestamp=1496515225249}] 
    00:10:25.249 [main] DEBUG org.springframework.integration.channel.DirectChannel - postSend (sent=true) on channel 'output', message: GenericMessage [payload=, headers={replyChannel=org.springframewor[email protected]7ee8290b, errorChannel=org.springframewor[email protected]7ee8290b, ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:50603:200af55e-020e-4334-ab34-d00ccd9ff43c, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=af31dde1-93ec-7209-b3c0-1c4d4e0a4949, ip_hostname=localhost, timestamp=1496515225249}] 
    00:10:25.249 [main] DEBUG org.springframework.integration.channel.DirectChannel - postSend (sent=true) on channel 'input', message: GenericMessage [payload=kksingh, headers={replyChannel=org.springframewor[email protected]7ee8290b, errorChannel=org.springframewor[email protected]7ee8290b, id=f43023da-3596-8cf2-4439-45496c087cd9, timestamp=1496515225235}] 
    00:10:25.250 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'integrationConversionService' 
    main---response-- 
    Exiting application...bye. 

我更新custommapper CLAS小號

public class CustomMapper extends TcpMessageMapper 
{ 

    @Override 
    protected Map<String, ?> supplyCustomHeaders(TcpConnection connection) 
    { 
     Map<String, String> temp = new HashMap<>(); 
     temp.put("correlationId", connection.getConnectionId()); 
     System.out.println("correlationId--" + connection.getConnectionId()); 

     return temp; 
    } 

} 

現在我更新的日誌中顯示

mainmain 
20:30:13.170 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'integrationEvaluationContext' 
20:30:13.176 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'integrationGlobalProperties' 
20:30:13.177 [main] DEBUG org.springframework.integration.channel.DirectChannel - preSend on channel 'input', message: GenericMessage [payload=kksingh, headers={replyChannel=org.springframewor[email protected]1190200a, errorChannel=org.springframewor[email protected]1190200a, id=c1c3b4e3-84bc-11d1-1ad7-1ffd33fed55e, timestamp=1496588413177}] 
20:30:13.177 [main] DEBUG org.springframework.integration.ip.tcp.TcpOutboundGateway - org.springframework.integration.ip.tcp.TcpOutboundGateway#0 received message: GenericMessage [payload=kksingh, headers={replyChannel=org.springframewor[email protected]1190200a, errorChannel=org.springframewor[email protected]1190200a, id=c1c3b4e3-84bc-11d1-1ad7-1ffd33fed55e, timestamp=1496588413177}] 
20:30:13.177 [main] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory - Opening new socket connection to localhost:9999 
20:30:13.186 [main] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - New connection localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b 
20:30:13.186 [main] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory - client: Added new connection: localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b 
20:30:13.187 [pool-1-thread-1] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b Reading... 
pool-1-thread-1---deserialize---- 
20:30:13.188 [pool-1-thread-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'messageBuilderFactory' 
correlationId--localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b 
20:30:13.188 [main] DEBUG org.springframework.integration.ip.tcp.TcpOutboundGateway - Added pending reply localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b 
20:30:13.189 [pool-1-thread-1] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - Message received GenericMessage [payload=, headers={ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b, ip_localInetAddress=/127.0.0.1, correlationId=localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b, ip_address=127.0.0.1, id=97276838-77fc-30e1-c1b5-ec31f4d43a9f, ip_hostname=localhost, timestamp=1496588413188}] 
pool-1-thread-1---deserialize---- 
kksingh---serialize---main 
correlationId--localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b 
20:30:13.189 [pool-1-thread-1] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - Message received GenericMessage [payload=, headers={ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b, ip_localInetAddress=/127.0.0.1, correlationId=localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b, ip_address=127.0.0.1, id=b4dc9909-2a10-2e7f-fa25-1f88311b99c2, ip_hostname=localhost, timestamp=1496588413189}] 
pool-1-thread-1---deserialize----kksingh 
correlationId--localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b 
20:30:13.191 [main] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b Message sent GenericMessage [payload=kksingh, headers={replyChannel=org.springframewor[email protected]1190200a, errorChannel=org.springframewor[email protected]1190200a, id=c1c3b4e3-84bc-11d1-1ad7-1ffd33fed55e, timestamp=1496588413177}] 
20:30:13.191 [pool-1-thread-1] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - Message received GenericMessage [payload=kksingh, headers={ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b, ip_localInetAddress=/127.0.0.1, correlationId=localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b, ip_address=127.0.0.1, id=3a3016c1-ea89-46b8-56a7-a549d94c529b, ip_hostname=localhost, timestamp=1496588413191}] 
20:30:13.191 [main] DEBUG org.springframework.integration.ip.tcp.TcpOutboundGateway - Response GenericMessage [payload=, headers={ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b, ip_localInetAddress=/127.0.0.1, correlationId=localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b, ip_address=127.0.0.1, id=97276838-77fc-30e1-c1b5-ec31f4d43a9f, ip_hostname=localhost, timestamp=1496588413188}] 
pool-1-thread-1---deserialize---- 
correlationId--localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b 
20:30:13.192 [main] DEBUG org.springframework.integration.ip.tcp.TcpOutboundGateway - Removed pending reply localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b 
20:30:13.192 [pool-1-thread-1] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - Message received GenericMessage [payload=, headers={ip_tcp_remotePort=9999, ip_connectionId=localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b, ip_localInetAddress=/127.0.0.1, correlationId=localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b, ip_address=127.0.0.1, id=269e9644-011e-934b-006d-032b76da146b, ip_hostname=localhost, timestamp=1496588413192}] 
20:30:13.192 [pool-1-thread-1] ERROR org.springframework.integration.ip.tcp.TcpOutboundGateway - Cannot correlate response - no pending reply for localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b 
20:30:13.193 [pool-1-thread-1] DEBUG org.springframework.integration.ip.tcp.connection.TcpNetConnection - Read exception localhost:9999:61953:d06b7c55-aa38-4e7d-ab3e-fd82e477812b SocketException:Socket is closed 
20:30:13.193 [main] DEBUG org.springframework.integration.channel.DirectChannel - postSend (sent=true) on channel 'input', message: GenericMessage [payload=kksingh, headers={replyChannel=org.springframewor[email protected]1190200a, errorChannel=org.springframewor[email protected]1190200a, id=c1c3b4e3-84bc-11d1-1ad7-1ffd33fed55e, timestamp=1496588413177}] 
20:30:13.193 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'integrationConversionService' 
main---response-- 
Exiting application...bye. 

我的服務器類很簡單

echoServer = new ServerSocket(9999); 
clientSocket = echoServer.accept(); 
     System.out.println("client connection established.."); 
     is = new DataInputStream(clientSocket.getInputStream()); 
     os = new PrintStream(clientSocket.getOutputStream()); 
     // As long as we receive data, echo that data back to the client. 
     String tempString = "kksingh"; 
     byte[] tempStringByte = tempString.getBytes(); 
     byte[] temp = new byte[tempString.getBytes().length]; 
     while (true) 
     { 
      is.read(temp); 
      System.out.println(new String(temp) + "--received byte is--- "); 
      System.out.println("sending value"); 
      os.write(tempStringByte); 
      break; 
     } 

請告訴我如何使主線程中使用自定義serialzer總是等待服務器響應和desrialzer PS:我是新的春天融合。

+0

你的問題不明確。 –

+0

@GaryRussell 我想創建TCP客戶端可以將消息發送到服務器,並等待服務器的響應。但問題是有時我的主線程會得到響應,有時我的另一個線程會收到響應。我怎樣才能確保總是主線程拿起響應kksingh – UchihaObito

回答

0

final String msg = gateway.send("kksingh"); 

調用已同步方法調用。線程不會進入下一個操作,直到這個返回。這就是Java和其他語言的工作原理。而這一點已經不重要了。

是的,你在日誌中看到什麼意味着答覆是監聽線程接收,但是因爲我們正在等待答覆狀況主線程阻塞任何方式。

+0

但是你可以看到在輸出的某個時候,我在主線程中接收到的響應爲kksingh,而其他時候它接收到響應爲「」。所以如何讓我的主線程始終接收作爲「kksingh」響應 – UchihaObito

+0

您應該確保沒有更多的用戶到'output'通道。默認情況下'DirectChannel'使用循環分配策略。 OTOH的'網關'你不需要'回覆通道' - 'replyChannel'頭部做的東西。當然,'nt-ip:tcp-outbound-gateway'不需要'output-channel'來從'replyChannel'頭獲得增益。你可以在參考手冊 –

+0

中找到所有的信息我已經更新了你建議的contextxml。我還寫了一個用於映射connectionid的自定義映射器。但我仍然無法將主線程響應作爲「kksingh」。 好心建議我應該做的設置 – UchihaObito