阅读TCP流消息
问题描述:
我被困在写一个简单地使用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来读取客户端发送的消息?
答
默认的反序列化程序期望消息以CRLF(这是Telnet发送的内容以及为什么有效)终止。
发送"XYZ\r\n".getBytes()
。
或者,将解串器更改为使用接近套接字的ByteArrayRawDeserializer
来终止消息。
请参阅the documentation about (de)serializers here。
TCP是流媒体协议;这意味着必须为通过TCP传输的数据提供一些结构,因此接收器可以将数据划分为离散消息。连接工厂被配置为使用(de)序列化器在消息负载和通过TCP发送的位之间进行转换。这是通过分别为入站和出站消息提供解串器和串行器来完成的。提供了许多标准(德)序列化器。
ByteArrayCrlfSerializer*
将字节数组转换为字节流,然后是回车符和换行符(\r\n
)。这是默认的(反)串行器,例如,可以用telnet作为客户端。...
的
ByteArrayRawSerializer*
,转换一个字节数组字节流,并增加了没有附加的消息划分的数据;使用这个(de)序列化程序,消息的结束由客户端按顺序关闭套接字来指示。使用此串行器时,消息接收将挂起,直到客户端关闭套接字或发生超时;超时将不会导致消息。