我被困在寫一個簡單地使用spring-integration接收TCP流消息的服務。我使用這個類來發送測試消息:閱讀TCP流消息
class TCPClient {
public static void main(String args[]) throws Exception {
Socket clientSocket = new Socket("localhost", 9999);
clientSocket.getOutputStream().write("XYZ".getBytes());
clientSocket.close();
}
}
服務器代碼,這應該收到一條消息:
@EnableIntegration
@IntegrationComponentScan
@Configuration
public class TcpServer {
@Bean
public AbstractServerConnectionFactory serverCF() {
return new TcpNetServerConnectionFactory(9999);
}
@Bean
public TcpInboundGateway tcpInGate(AbstractServerConnectionFactory conFactory) {
TcpInboundGateway inGate = new TcpInboundGateway();
inGate.setConnectionFactory(conFactory);
SubscribableChannel channel = new DirectChannel();
//Planning to set custom message handler here, to process messages later
channel.subscribe(message -> System.out.println(convertMessage(message)));
inGate.setRequestChannel(channel);
return inGate;
}
private String convertMessage(Message<?> message) {
return message == null || message.getPayload() == null
? null
: new String((byte[]) message.getPayload());
}
}
問題:客戶端代碼在運行時 - 服務器記錄以下異常:
TcpNetConnection : Read exception localhost:46924:9999:6d00ac25-b5c8-47ac-9bdd-edb6bc09fe55 IOException:Socket closed during message assembly
一個很能接受,當我使用的telnet 發送它的消息或當我使用SI簡單的java-only tcp-server實現。我如何配置spring-integration來讀取客戶端發送的消息?