我被困在写一个简单地使用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来读取客户端发送的消息?