5.4.1 Netty线程模型
5.4.2 责任链设计模式
责任链代码:
// -----链表形式调用------netty就是类似的这种形式
public class PipelineDemo {
/**
* 初始化的时候造一个head,作为责任链的开始,但是并没有具体的处理
*/
public HandlerChainContext head = new HandlerChainContext(new AbstractHandler() {
@Override
void doHandler(HandlerChainContext handlerChainContext, Object arg0) {
handlerChainContext.runNext(arg0);
}
});
public void requestProcess(Object arg0) {
this.head.handler(arg0);
}
public void addLast(AbstractHandler handler) {
HandlerChainContext context = head;
while (context.next != null) {
context = context.next;
}
context.next = new HandlerChainContext(handler);
}
public static void main(String[] args) {
PipelineDemo pipelineChainDemo = new PipelineDemo();
pipelineChainDemo.addLast(new Handler2());
pipelineChainDemo.addLast(new Handler1());
pipelineChainDemo.addLast(new Handler1());
pipelineChainDemo.addLast(new Handler2());
// 发起请求
pipelineChainDemo.requestProcess("火车呜呜呜~~");
}
}
/**
* handler上下文,我主要负责维护链,和链的执行
*/
class HandlerChainContext {
HandlerChainContext next; // 下一个节点
AbstractHandler handler;
public HandlerChainContext(AbstractHandler handler) {
this.handler = handler;
}
void handler(Object arg0) {
this.handler.doHandler(this, arg0);
}
/**
* 继续执行下一个
*/
void runNext(Object arg0) {
if (this.next != null) {
this.next.handler(arg0);
}
}
}
// 处理器抽象类
abstract class AbstractHandler {
/**
* 处理器,这个处理器就做一件事情,在传入的字符串中增加一个尾巴..
*/
abstract void doHandler(HandlerChainContext handlerChainContext, Object arg0); // handler方法
}
// 处理器具体实现类
class Handler1 extends AbstractHandler {
@Override
void doHandler(HandlerChainContext handlerChainContext, Object arg0) {
arg0 = arg0.toString() + "..handler1的小尾巴.....";
System.out.println("我是Handler1的实例,我在处理:" + arg0);
// 继续执行下一个
handlerChainContext.runNext(arg0);
}
}
// 处理器具体实现类
class Handler2 extends AbstractHandler {
@Override
void doHandler(HandlerChainContext handlerChainContext, Object arg0) {
arg0 = arg0.toString() + "..handler2的小尾巴.....";
System.out.println("我是Handler2的实例,我在处理:" + arg0);
// 继续执行下一个
handlerChainContext.runNext(arg0);
}
}
5.4.3 零拷贝机制
0 -> readerIndex : 已读取区域,可以释放
readerIndex -> wtiterIndex : 已写区域,尚未读取
writerIndex -> capacity : 未写区域,无数据
netty的ByteBuf建议使用Unpooled工具实例化创建
ByteBuf buf = Unpooled.buffer(10);//创建一个非池化的ByteBuf,大小为10字节
ByteBuf – API
byte[] bytes = {1,2,3,4,5};
buf.writeBytes(bytes);//写入
byte b1 = buf.readByte();//读取
buf.discardReadBytes();//将读取的内容丢弃
buf.clear();//…
堆内:unpooledHeapByteBuf 本质是数组 byte[]
堆外:unpooledDirectByteBuf 本质是nio的ByteBuf
API的区别在于buf.array(),堆外没有实现,调用会抛异常
查看对象复用是否生效:查看不同2次请求中,红框内的byteBuf内存地址是否一样
如果想启用对象复用,可以使用图下2种方法
netty内存分配算法:就像快递柜。大对象放大格子,小对象放小格子。当缓存用完,netty就去申请新的unpooled内存,用完就释放
netty自己会使用PooledUnsafeDirectByteBuf:主要是快
建议开发人员使用UnpooledHeapByteBuf:安全
零拷贝:数据不动,逻辑上合体