通过观察构建ClientRequest的过程可知,其有一个RequestSend,其构造函数如下:
public RequestSend(String destination, RequestHeader header, Struct body) {
super(destination, serialize(header, body));
this.header = header;
this.body = body;
}
public static ByteBuffer serialize(RequestHeader header, Struct body) {
ByteBuffer buffer = ByteBuffer.allocate(header.sizeOf() + body.sizeOf());
header.writeTo(buffer);
body.writeTo(buffer);
buffer.rewind();
return buffer;
}
那么,其extends的super干了啥
public NetworkSend(String destination, ByteBuffer... buffers) {
super(destination, sizeDelimit(buffers));
}
private static ByteBuffer[] sizeDelimit(ByteBuffer[] buffers) {
int size = 0;
for (int i = 0; i < buffers.length; i++)
size += buffers[i].remaining();
ByteBuffer[] delimited = new ByteBuffer[buffers.length + 1];
delimited[0] = ByteBuffer.allocate(4);//4字节的缓冲区
delimited[0].putInt(size);
delimited[0].rewind();
System.arraycopy(buffers, 0, delimited, 1, buffers.length);
return delimited;
}
继续看NetworkSend执行的super
//这个数组一共是3个元素,第一个是header+body的长度,为int,占四个字节,第二个是header的byteBuffer,第三个是body的ByteBuffer
//这个实现在NetworkSend内,这也是为什么需要实现分散和聚集的接口
protected final ByteBuffer[] buffers;//最终写的是这个ByteBuffer数组
public ByteBufferSend(String destination, ByteBuffer... buffers) {
super();
this.destination = destination;
this.buffers = buffers;
for (int i = 0; i < buffers.length; i++)
remaining += buffers[i].remaining();
this.size = remaining;
}
那nio里怎么写ByteBuffer数组?
使用分散和聚集!即:ScatteringByteChannel and GatheringByteChannel
所以,我们看到KafkaChannel的TransportLayer实现了ScatteringByteChannel and GatheringByteChannel