Netty组件Future、Promise、Handler、Pipline、ByteBuf

Future&Promise

Netty中的Future与jdk中的Future同名,但是是两个接口,netty的Future继承自jdk的Future,而Promise又对netty Future进行了扩展

  • jdk Future只能同步等待任务结束(或成功、或失败)才能得到结果
  • netty Future可以同步等待任务结束得到结果,也可以异步方式得到结果,但是要等到任务结束
  • netty Promise不仅有netty Future的功能,而且脱离了任务独立存在,只作为两个线程间传递结果的容器
功能/名称jdk Futurenetty FuturePromise
cancel取消任务--
isCanceled任务是否取消--
isDone任务是否完成,不能区分成功失败--
get获取任务结果,阻塞等待--
getNow-获取任务结果,非阻塞,还未产生结果时返回 null-
await-等待任务结束,如果任务失败,不会抛异常,而是通过 isSuccess 判断-
sync-等待任务结束,如果任务失败,抛出异常-
isSuccess-判断任务是否成功-
cause-获取失败信息,非阻塞,如果没有失败,返回null-
addLinstener-添加回调,异步接收结果-
setSuccess--设置成功结果
setFailure--设置失败结果

JDK Future

@Slf4j
public class Fuature1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(5);
        Future<? extends Integer> future = pool.submit((Callable<? extends Integer>) () -> {
            log.info("waiting ...");
            TimeUnit.SECONDS.sleep(3);
            return 60;
        });
        log.info("---");
        Integer result = future.get();
        log.info("result:{}",result);
    }
}

Netty Future

@Slf4j
public class Future2 {
    public static void main(String[] args) {
        NioEventLoopGroup loopGroup = new NioEventLoopGroup();
        EventLoop eventLoop = loopGroup.next();
        Future<? extends Integer> future = eventLoop.submit((Callable<? extends Integer>) () -> {
            log.info("task runing ...");
            TimeUnit.SECONDS.sleep(3);
            return 80;
        });
        future.addListener(new FutureListener<Integer>(){
            @Override
            public void operationComplete(Future<Integer> integerFuture) throws Exception {
                Integer result = integerFuture.getNow();
                log.info("result:{}",result);
            }
        });
        log.info("waiting ...");
    }
}

Promise

Promise相当于一个容器,可以用于存放各个线程中的结果,然后让其他线程去获取该结果

@Slf4j
public class NettyPromise {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        NioEventLoopGroup group=new NioEventLoopGroup();
        Promise<Integer> promise=new DefaultPromise<>(group.next());
        new Thread(()->{
            try {
                log.info("calc ...");
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                promise.setFailure(e);
            }
            promise.setSuccess(100);
        }).start();
        log.info("result:{}",promise.get());#### Future&Promise

Netty中的Future与jdk中的Future同名,但是是两个接口,netty的Future继承自jdk的Future,Promise又对netty Future进行了扩展

* jdk Future只能同步等待任务结束(或成功、或失败)才能得到结果
* netty Future可以同步等待任务结束得到结果,也可以异步方式得到结果,但是要等到任务结束
* netty Promise不仅有netty Future的功能,而且脱离了任务独立存在,只作为两个线程间传递结果的容器

| 功能/名称    | jdk Future                     | netty Future                                                 | Promise      |
| ------------ | ------------------------------ | ------------------------------------------------------------ | ------------ |
| cancel       | 取消任务                       | -                                                            | -            |
| isCanceled   | 任务是否取消                   | -                                                            | -            |
| isDone       | 任务是否完成,不能区分成功失败 | -                                                            | -            |
| get          | 获取任务结果,阻塞等待         | -                                                            | -            |
| getNow       | -                              | 获取任务结果,非阻塞,还未产生结果时返回 null                | -            |
| await        | -                              | 等待任务结束,如果任务失败,不会抛异常,而是通过 isSuccess 判断 | -            |
| sync         | -                              | 等待任务结束,如果任务失败,抛出异常                         | -            |
| isSuccess    | -                              | 判断任务是否成功                                             | -            |
| cause        | -                              | 获取失败信息,非阻塞,如果没有失败,返回null                 | -            |
| addLinstener | -                              | 添加回调,异步接收结果                                       | -            |
| setSuccess   | -                              | -                                                            | 设置成功结果 |
| setFailure   | -                              | -                                                            | 设置失败结果 |

**JDK Future**

```java
@Slf4j
public class Fuature1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(5);
        Future<? extends Integer> future = pool.submit((Callable<? extends Integer>) () -> {
            log.info("waiting ...");
            TimeUnit.SECONDS.sleep(3);
            return 60;
        });
        log.info("---");
        Integer result = future.get();
        log.info("result:{}",result);
    }
}

Netty Future

@Slf4j
public class Future2 {
    public static void main(String[] args) {
        NioEventLoopGroup loopGroup = new NioEventLoopGroup();
        EventLoop eventLoop = loopGroup.next();
        Future<? extends Integer> future = eventLoop.submit((Callable<? extends Integer>) () -> {
            log.info("task runing ...");
            TimeUnit.SECONDS.sleep(3);
            return 80;
        });
        future.addListener(new FutureListener<Integer>(){
            @Override
            public void operationComplete(Future<Integer> integerFuture) throws Exception {
                Integer result = integerFuture.getNow();
                log.info("result:{}",result);
            }
        });
        log.info("waiting ...");
    }
}

Promise

Promise相当于一个容器,可以用于存放各个线程中的结果,然后让其他线程去获取该结果

@Slf4j
public class NettyPromise {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        NioEventLoopGroup group=new NioEventLoopGroup();
        Promise<Integer> promise=new DefaultPromise<>(group.next());
        new Thread(()->{
            try {
                log.info("calc ...");
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                promise.setFailure(e);
            }
            promise.setSuccess(100);
        }).start();
        log.info("result:{}",promise.get());
   }

Handler&Pipline

ChannelHandler用来处理Channel上的各种事件,分为入站、出站两种。所有ChannelHandler被连成一串,就是Pipline

  • 入站处理器通常是ChannelInboundHandlerAdapter的子类,主要用来读取客户端数据,写回结果
  • 出站处理器通常是ChannelOutboundHandlerAdapter的子类,主要对写回结果进行加工

打个比喻,每个Channel是一个产品的加工车间,Pipline是车间中的流水线,ChannelHandler就是流水线上的各道工序,而后面的ByteBuf就是原材料,经过很多工序的加工:先经过一道道入站工序,再经过一道道出站工序最终变为产品

package com.vmware.netty.utils.s5;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

@Slf4j
public class PiplineServer {
    public static void main(String[] args) {
        new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast("handler1",new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.info("1");
                                ctx.fireChannelRead(msg);
                            }
                        });
                        socketChannel.pipeline().addLast("handler2",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.info("2");
                                ctx.fireChannelRead(msg);
                            }
                        });
                        socketChannel.pipeline().addLast("handler3",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.info("3");
                                socketChannel.writeAndFlush(ctx.alloc().buffer().writeBytes("Server...".getBytes(StandardCharsets.UTF_8)));
                                super.channelRead(ctx,msg);
                            }
                        });
                        socketChannel.pipeline().addLast("handler4",new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.info("4");
                                super.write(ctx, msg, promise);
                            }
                        });
                        socketChannel.pipeline().addLast("handler5",new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.info("5");
                                super.write(ctx, msg, promise);
                            }
                        });
                        socketChannel.pipeline().addLast("handler6",new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.info("6");
                                super.write(ctx, msg, promise);
                            }
                        });
                    }
                }).bind(8888);
    }
}

输出结果

2023-04-06 00:15:25.721 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer - 1
2023-04-06 00:15:25.722 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer - 2
2023-04-06 00:15:25.722 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer - 3
2023-04-06 00:15:25.722 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer - 6
2023-04-06 00:15:25.723 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer - 5
2023-04-06 00:15:25.723 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer - 4

可以看到,ChannelInboundHandlerAdapter是按照addLast的执行顺序执行的,而ChannelOutboundHandlerAdapter是按照addLast的逆序执行的。ChannelPipline的实现是一个ChannelHandlerContext(包装了ChannelHandler)组成的双向链表
在这里插入图片描述

在通过channel.pipline().addLast(name,handler)添加handler时,可以为handler取名字。这样可以调用pipline的addAfter、addBefore等方法更灵活的向pipline中添加handler

  • pipline是一个结构带有head与tail指针的双向链表,其中的节点为handler
    • 通过ctx.fireChannelRead(msg)等方法,将当前handler的处理结果传递给下一个handler
  • 当有入站(Inbound)操作时,会从head开始向后调用handler,直到handler不是处理Inbound操作为止
  • 当有出站(Outbound)操作时,会从tail开始向前调用handler,直到handler不是处理Outbound操作为止

在这里插入图片描述

OutboundHandler

  • socketChannel.writeAndFlush()

当handler中调用该方法进行写操作时,会触发Outbound操作,此时是从tail向前寻找OutboundHandler

  • ctx.writeAndFlush()

当handler中调用该方法进行写操作时,会触发Outbound操作,此时是从当前handler向前寻找OutboundHandler

修改服务器端代码

socketChannel.pipeline().addLast("handler4",new ChannelOutboundHandlerAdapter(){
   @Override
   public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        log.info("4");
        super.write(ctx, msg, promise);
    }
});
socketChannel.pipeline().addLast("handler3",new ChannelInboundHandlerAdapter(){
   @Override
   public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info("3");
        ctx.writeAndFlush(ctx.alloc().buffer().writeBytes("Server...".getBytes(StandardCharsets.UTF_8)));
        ctx.fireChannelRead(msg);
   }
});

输出

2023-04-06 00:29:16.381 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer2 - 1
2023-04-06 00:29:16.381 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer2 - 2
2023-04-06 00:29:16.381 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer2 - 3
2023-04-06 00:29:16.382 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer2 - 4

EmbeddedChannel

EmbeddedChannel可以用于测试各种handler,通过其构造函数按顺序传入需要测试的handler,然后调用对应的Inbound和Outbound方法即可

@Slf4j
public class EmbededChannel {
    public static void main(String[] args) {
        ChannelInboundHandlerAdapter c1 = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                log.info("1");
                ctx.fireChannelRead(msg);
            }
        };
        ChannelInboundHandlerAdapter c2 = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                log.info("2");
                ctx.fireChannelRead(msg);
            }
        };
        ChannelInboundHandlerAdapter c3 = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                log.info("3");
                ctx.fireChannelRead(msg);
            }
        };

        ChannelOutboundHandlerAdapter o1 = new ChannelOutboundHandlerAdapter(){
            @Override
            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                log.info("4");
                super.write(ctx,msg,promise);
            }
        };
        ChannelOutboundHandlerAdapter o2 = new ChannelOutboundHandlerAdapter(){
            @Override
            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                log.info("5");
                super.write(ctx,msg,promise);
            }
        };
        EmbeddedChannel channel = new EmbeddedChannel(c1, c2, c3, o1, o2);
        //执行Inbound
        channel.writeInbound(ByteBufAllocator.DEFAULT.buffer().writeBytes("hello".getBytes(StandardCharsets.UTF_8)));
        //执行Outbound
        channel.writeOutbound(ByteBufAllocator.DEFAULT.buffer().writeBytes("hello".getBytes(StandardCharsets.UTF_8)));
    }
}

ByteBuf

创建调试工具

public class BufUtil {
    public static void log(ByteBuf buffer) {
        int length = buffer.readableBytes();
        int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
        StringBuilder buf = new StringBuilder(rows * 80 * 2)
                .append("read index:").append(buffer.readerIndex())
                .append(" write index:").append(buffer.writerIndex())
                .append(" capacity:").append(buffer.capacity())
                .append(NEWLINE);
        appendPrettyHexDump(buf, buffer);
        System.out.println(buf.toString());
    }
}

该方法可以帮助我们更为详细地查看ByteBuf中的内容

创建ByteBuf

@Slf4j
public class ByteBufStudy {
    public static void main(String[] args) {
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(16);
        BufUtil.log(buffer);
        StringBuilder builder = new StringBuilder();
        for (int index = 0; index < 20; index++) {
            builder.append("a");
        }
        buffer.writeBytes(builder.toString().getBytes(StandardCharsets.UTF_8));
        BufUtil.log(buffer);
    }
}

执行结果

read index:0 write index:0 capacity:16

read index:0 write index:20 capacity:64
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
|00000010| 61 61 61 61                                     |aaaa            |
+--------+-------------------------------------------------+----------------+

创建方式

ByteBuf可以通过ByteBufAllocator选择allocator并调用对应的buffer方法来创建,默认使用直接内存作为ByteBuf,容量为256个字节,可以指定初始容量大小

当ByteBuf的容量无法容纳所有数据时,ByteBuf会自动进行扩容操作

当在handler中创建ByteBuf,建议使用ChannelHandlerContext ctx.alloc().buffer()来创建

直接内存与堆内存

声明直接内存类型的ByteBuf

//方式1
ByteBufAllocator.DEFAULT.buffer();
//方式2
ByteBufAllocator.DEFAULT.directBuffer();

声明堆内存类型的ByteBuf

ByteBufAllocator.DEFAULT.heapBuffer(16);
  • 直接内存创建和销毁的代价昂贵,但读写性能高(少一次内存复制),适合配合池化功能一起用
  • 直接内存对 GC 压力小,因为这部分内存不受 JVM 垃圾回收的管理,但也要注意及时主动释放

示例

@Slf4j
public class ByteBufStudy2 {
    public static void main(String[] args) {
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        log.info("type:{}",buffer.getClass());
        ByteBuf buffer1 = ByteBufAllocator.DEFAULT.heapBuffer();
        log.info("type:{}",buffer1.getClass());
        ByteBuf buffer2 = ByteBufAllocator.DEFAULT.directBuffer();
        log.info("type:{}",buffer2.getClass());
    }
}

输出

2023-04-06 01:35:13.138 [main] INFO  com.vmware.netty.utils.buffer.ByteBufStudy2 - type:class io.netty.buffer.PooledUnsafeDirectByteBuf
2023-04-06 01:35:13.145 [main] INFO  com.vmware.netty.utils.buffer.ByteBufStudy2 - type:class io.netty.buffer.PooledUnsafeHeapByteBuf
2023-04-06 01:35:13.145 [main] INFO  com.vmware.netty.utils.buffer.ByteBufStudy2 - type:class io.netty.buffer.PooledUnsafeDirectByteBuf

池化与非池化

池化的最大意义在于可以重用 ByteBuf,优点有

  • 没有池化,则每次都得创建新的 ByteBuf 实例,这个操作对直接内存代价昂贵,就算是堆内存,也会增加 GC 压力
  • 有了池化,则可以重用池中 ByteBuf 实例,并且采用了与 jemalloc 类似的内存分配算法提升分配效率
  • 高并发时,池化功能更节约内存,减少内存溢出的可能

池化功能是否开启,可以通过下面的系统环境变量来设置

-Dio.netty.allocator.type={unpooled|pooled}Copy
  • 4.1 以后,非 Android 平台默认启用池化实现,Android 平台启用非池化实现
  • 4.1 之前,池化功能还不成熟,默认是非池化实现

组成

ByteBuf主要由一下几个组成部分

最大容量与当前容量

  • 在构造ByteBuf时,可以传入两个参数,分别代表初始容量和最大容量,若未传入第二个参数(最大容量),最大容量默认为Integer.MAX_VALUE
  • 当ByteBuf容量无法容纳所有数据时,会进行扩容操作,若超出最大容量,会抛出IndexOutOfBoundsException

读写操作不同于ByteBuffer只用position进行控制,ByteBuf分别由读指针和写指针两个指针控制。进行读写操作时,无需进行模式的切换

  • 读指针前的部分被称为废弃部分,是已经读过的内容
  • 读指针与写指针之间的空间称为可读部分
  • 写指针与当前容量之间的空间称为可写部分

在这里插入图片描述

最开始读写指针都在 0 位置

}


#### Handler&Pipline

`ChannelHandler`用来处理Channel上的各种事件,分为入站、出站两种。所有ChannelHandler被连成一串,就是Pipline

* 入站处理器通常是ChannelInboundHandlerAdapter的子类,主要用来读取客户端数据,写回结果
* 出站处理器通常是ChannelOutboundHandlerAdapter的子类,主要对写回结果进行加工

打个比喻,每个Channel是一个产品的加工车间,Pipline是车间中的流水线,ChannelHandler就是流水线上的各道工序,而后面的ByteBuf就是原材料,经过很多工序的加工:先经过一道道入站工序,再经过一道道出站工序最终变为产品

```java
package com.vmware.netty.utils.s5;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

@Slf4j
public class PiplineServer {
    public static void main(String[] args) {
        new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast("handler1",new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.info("1");
                                ctx.fireChannelRead(msg);
                            }
                        });
                        socketChannel.pipeline().addLast("handler2",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.info("2");
                                ctx.fireChannelRead(msg);
                            }
                        });
                        socketChannel.pipeline().addLast("handler3",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.info("3");
                                socketChannel.writeAndFlush(ctx.alloc().buffer().writeBytes("Server...".getBytes(StandardCharsets.UTF_8)));
                                super.channelRead(ctx,msg);
                            }
                        });
                        socketChannel.pipeline().addLast("handler4",new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.info("4");
                                super.write(ctx, msg, promise);
                            }
                        });
                        socketChannel.pipeline().addLast("handler5",new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.info("5");
                                super.write(ctx, msg, promise);
                            }
                        });
                        socketChannel.pipeline().addLast("handler6",new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.info("6");
                                super.write(ctx, msg, promise);
                            }
                        });
                    }
                }).bind(8888);
    }
}

输出结果

2023-04-06 00:15:25.721 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer - 1
2023-04-06 00:15:25.722 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer - 2
2023-04-06 00:15:25.722 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer - 3
2023-04-06 00:15:25.722 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer - 6
2023-04-06 00:15:25.723 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer - 5
2023-04-06 00:15:25.723 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer - 4

可以看到,ChannelInboundHandlerAdapter是按照addLast的执行顺序执行的,而ChannelOutboundHandlerAdapter是按照addLast的逆序执行的。ChannelPipline的实现是一个ChannelHandlerContext(包装了ChannelHandler)组成的双向链表

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xJjoHjq2-1680802895652)(img/pipline.png)]

在通过channel.pipline().addLast(name,handler)添加handler时,可以为handler取名字。这样可以调用pipline的addAfter、addBefore等方法更灵活的向pipline中添加handler

  • pipline是一个结构带有head与tail指针的双向链表,其中的节点为handler
    • 通过ctx.fireChannelRead(msg)等方法,将当前handler的处理结果传递给下一个handler
  • 当有入站(Inbound)操作时,会从head开始向后调用handler,直到handler不是处理Inbound操作为止
  • 当有出站(Outbound)操作时,会从tail开始向前调用handler,直到handler不是处理Outbound操作为止

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZYaUkPwV-1680802895653)(img/pipline_call.png)]

OutboundHandler

  • socketChannel.writeAndFlush()

当handler中调用该方法进行写操作时,会触发Outbound操作,此时是从tail向前寻找OutboundHandler

  • ctx.writeAndFlush()

当handler中调用该方法进行写操作时,会触发Outbound操作,此时是从当前handler向前寻找OutboundHandler

修改服务器端代码

socketChannel.pipeline().addLast("handler4",new ChannelOutboundHandlerAdapter(){
   @Override
   public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        log.info("4");
        super.write(ctx, msg, promise);
    }
});
socketChannel.pipeline().addLast("handler3",new ChannelInboundHandlerAdapter(){
   @Override
   public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info("3");
        ctx.writeAndFlush(ctx.alloc().buffer().writeBytes("Server...".getBytes(StandardCharsets.UTF_8)));
        ctx.fireChannelRead(msg);
   }
});

输出

2023-04-06 00:29:16.381 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer2 - 1
2023-04-06 00:29:16.381 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer2 - 2
2023-04-06 00:29:16.381 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer2 - 3
2023-04-06 00:29:16.382 [nioEventLoopGroup-2-2] INFO  com.vmware.netty.utils.s5.PiplineServer2 - 4

EmbeddedChannel

EmbeddedChannel可以用于测试各种handler,通过其构造函数按顺序传入需要测试的handler,然后调用对应的Inbound和Outbound方法即可

@Slf4j
public class EmbededChannel {
    public static void main(String[] args) {
        ChannelInboundHandlerAdapter c1 = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                log.info("1");
                ctx.fireChannelRead(msg);
            }
        };
        ChannelInboundHandlerAdapter c2 = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                log.info("2");
                ctx.fireChannelRead(msg);
            }
        };
        ChannelInboundHandlerAdapter c3 = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                log.info("3");
                ctx.fireChannelRead(msg);
            }
        };

        ChannelOutboundHandlerAdapter o1 = new ChannelOutboundHandlerAdapter(){
            @Override
            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                log.info("4");
                super.write(ctx,msg,promise);
            }
        };
        ChannelOutboundHandlerAdapter o2 = new ChannelOutboundHandlerAdapter(){
            @Override
            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                log.info("5");
                super.write(ctx,msg,promise);
            }
        };
        EmbeddedChannel channel = new EmbeddedChannel(c1, c2, c3, o1, o2);
        //执行Inbound
        channel.writeInbound(ByteBufAllocator.DEFAULT.buffer().writeBytes("hello".getBytes(StandardCharsets.UTF_8)));
        //执行Outbound
        channel.writeOutbound(ByteBufAllocator.DEFAULT.buffer().writeBytes("hello".getBytes(StandardCharsets.UTF_8)));
    }
}

ByteBuf

创建调试工具

public class BufUtil {
    public static void log(ByteBuf buffer) {
        int length = buffer.readableBytes();
        int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
        StringBuilder buf = new StringBuilder(rows * 80 * 2)
                .append("read index:").append(buffer.readerIndex())
                .append(" write index:").append(buffer.writerIndex())
                .append(" capacity:").append(buffer.capacity())
                .append(NEWLINE);
        appendPrettyHexDump(buf, buffer);
        System.out.println(buf.toString());
    }
}

该方法可以帮助我们更为详细地查看ByteBuf中的内容

创建ByteBuf

@Slf4j
public class ByteBufStudy {
    public static void main(String[] args) {
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(16);
        BufUtil.log(buffer);
        StringBuilder builder = new StringBuilder();
        for (int index = 0; index < 20; index++) {
            builder.append("a");
        }
        buffer.writeBytes(builder.toString().getBytes(StandardCharsets.UTF_8));
        BufUtil.log(buffer);
    }
}

执行结果

read index:0 write index:0 capacity:16

read index:0 write index:20 capacity:64
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
|00000010| 61 61 61 61                                     |aaaa            |
+--------+-------------------------------------------------+----------------+

创建方式

ByteBuf可以通过ByteBufAllocator选择allocator并调用对应的buffer方法来创建,默认使用直接内存作为ByteBuf,容量为256个字节,可以指定初始容量大小

当ByteBuf的容量无法容纳所有数据时,ByteBuf会自动进行扩容操作

当在handler中创建ByteBuf,建议使用ChannelHandlerContext ctx.alloc().buffer()来创建

直接内存与堆内存

声明直接内存类型的ByteBuf

//方式1
ByteBufAllocator.DEFAULT.buffer();
//方式2
ByteBufAllocator.DEFAULT.directBuffer();

声明堆内存类型的ByteBuf

ByteBufAllocator.DEFAULT.heapBuffer(16);
  • 直接内存创建和销毁的代价昂贵,但读写性能高(少一次内存复制),适合配合池化功能一起用
  • 直接内存对 GC 压力小,因为这部分内存不受 JVM 垃圾回收的管理,但也要注意及时主动释放

示例

@Slf4j
public class ByteBufStudy2 {
    public static void main(String[] args) {
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        log.info("type:{}",buffer.getClass());
        ByteBuf buffer1 = ByteBufAllocator.DEFAULT.heapBuffer();
        log.info("type:{}",buffer1.getClass());
        ByteBuf buffer2 = ByteBufAllocator.DEFAULT.directBuffer();
        log.info("type:{}",buffer2.getClass());
    }
}

输出

2023-04-06 01:35:13.138 [main] INFO  com.vmware.netty.utils.buffer.ByteBufStudy2 - type:class io.netty.buffer.PooledUnsafeDirectByteBuf
2023-04-06 01:35:13.145 [main] INFO  com.vmware.netty.utils.buffer.ByteBufStudy2 - type:class io.netty.buffer.PooledUnsafeHeapByteBuf
2023-04-06 01:35:13.145 [main] INFO  com.vmware.netty.utils.buffer.ByteBufStudy2 - type:class io.netty.buffer.PooledUnsafeDirectByteBuf

池化与非池化

池化的最大意义在于可以重用 ByteBuf,优点有

  • 没有池化,则每次都得创建新的 ByteBuf 实例,这个操作对直接内存代价昂贵,就算是堆内存,也会增加 GC 压力
  • 有了池化,则可以重用池中 ByteBuf 实例,并且采用了与 jemalloc 类似的内存分配算法提升分配效率
  • 高并发时,池化功能更节约内存,减少内存溢出的可能

池化功能是否开启,可以通过下面的系统环境变量来设置

-Dio.netty.allocator.type={unpooled|pooled}Copy
  • 4.1 以后,非 Android 平台默认启用池化实现,Android 平台启用非池化实现
  • 4.1 之前,池化功能还不成熟,默认是非池化实现

组成

ByteBuf主要由一下几个组成部分

最大容量与当前容量

  • 在构造ByteBuf时,可以传入两个参数,分别代表初始容量和最大容量,若未传入第二个参数(最大容量),最大容量默认为Integer.MAX_VALUE
  • 当ByteBuf容量无法容纳所有数据时,会进行扩容操作,若超出最大容量,会抛出IndexOutOfBoundsException

读写操作不同于ByteBuffer只用position进行控制,ByteBuf分别由读指针和写指针两个指针控制。进行读写操作时,无需进行模式的切换

  • 读指针前的部分被称为废弃部分,是已经读过的内容
  • 读指针与写指针之间的空间称为可读部分
  • 写指针与当前容量之间的空间称为可写部分

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rTw42YEu-1680802895653)(img/bytebuf.png)]

最开始读写指针都在 0 位置

写入

常用方法

方法签名含义备注
writeBoolean(boolean value)写入 boolean 值用一字节 01|00 代表 true|false
writeByte(int value)写入 byte 值
writeShort(int value)写入 short 值
writeInt(int value)写入 int 值Big Endian(大端写入),即 0x250,写入后 00 00 02 50
writeIntLE(int value)写入 int 值Little Endian(小端写入),即 0x250,写入后 50 02 00 00
writeLong(long value)写入 long 值
writeChar(int value)写入 char 值
writeFloat(float value)写入 float 值
writeDouble(double value)写入 double 值
writeBytes(ByteBuf src)写入 netty 的 ByteBuf
writeBytes(byte[] src)写入 byte[]
writeBytes(ByteBuffer src)写入 nio 的 ByteBuffer
int writeCharSequence(CharSequence sequence, Charset charset)写入字符串CharSequence为字符串类的父类,第二个参数为对应的字符集

注意

  • 这些方法的未指明返回值的,其返回值都是 ByteBuf,意味着可以链式调用来写入不同的数据
  • 网络传输中,默认习惯是 Big Endian(大端写入),使用 writeInt(int value)

还有一类方法是 set 开头的一系列方法,也可以写入数据,但不会改变写指针位置

扩容

当ByteBuf中的容量无法容纳写入的数据时,会自动进行扩容操作

扩容规则

  • 如何写入后数据大小未超过 512 字节,则选择下一个 16 的整数倍进行扩容
    • 例如写入后大小为 12 字节,则扩容后 capacity 是 16 字节
  • 如果写入后数据大小超过 512 字节,则选择下一个 2^n
    • 例如写入后大小为 513 字节,则扩容后 capacity 是 210=1024 字节(2^9=512 已经不够了)
  • 扩容不能超过 maxCapacity,否则会抛出java.lang.IndexOutOfBoundsException异常
Exception in thread "main" java.lang.IndexOutOfBoundsException: writerIndex(20) + minWritableBytes(8) exceeds maxCapacity(20): PooledUnsafeDirectByteBuf(ridx: 0, widx: 20, cap: 20/20)
...

读取

读取主要是通过一系列read方法进行读取,读取时会根据读取数据的字节数移动读指针

@Slf4j
public class ByteBufTest {
    public static void main(String[] args) {
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        buffer.writeBytes(new byte[]{1, 2, 3, 4, 5, 6});
        log.info("{}",buffer.readByte());
        log.info("{}",buffer.readByte());
        log.info("{}",buffer.readByte());
        log.info("{}",buffer.readByte());
        BufUtil.log(buffer);
    }
}

读过的内容,就属于废弃部分了,再读只能读那些尚未读取的部分

2023-04-07 00:50:32.527 [main] INFO  com.vmware.netty.utils.buf.ByteBufTest - 1
2023-04-07 00:50:32.527 [main] INFO  com.vmware.netty.utils.buf.ByteBufTest - 2
2023-04-07 00:50:32.527 [main] INFO  com.vmware.netty.utils.buf.ByteBufTest - 3
2023-04-07 00:50:32.527 [main] INFO  com.vmware.netty.utils.buf.ByteBufTest - 4
read index:4 write index:6 capacity:256
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 05 06                                           |..              |
+--------+-------------------------------------------------+----------------+

如果需要重复读取,需要调用buffer.markReaderIndex()对读指针进行标记,并通过buffer.resetReaderIndex()将读指针恢复到mark标记的位置

@Slf4j
public class ByteBufTest {
    public static void main(String[] args) {
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        buffer.writeBytes(new byte[]{1, 2, 3, 4, 5, 6});
        log.info("{}",buffer.readByte());
        buffer.markReaderIndex();//标记
        log.info("{}",buffer.readByte());
        log.info("{}",buffer.readByte());
        log.info("{}",buffer.readByte());
        buffer.resetReaderIndex();//回到标记位
        log.info("{}",buffer.readByte());
    }
}

输出

2023-04-07 00:53:40.216 [main] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@4d49af10
2023-04-07 00:53:40.222 [main] INFO  com.vmware.netty.utils.buf.ByteBufTest - 1
2023-04-07 00:53:40.222 [main] INFO  com.vmware.netty.utils.buf.ByteBufTest - 2
2023-04-07 00:53:40.222 [main] INFO  com.vmware.netty.utils.buf.ByteBufTest - 3
2023-04-07 00:53:40.222 [main] INFO  com.vmware.netty.utils.buf.ByteBufTest - 4
2023-04-07 00:53:40.222 [main] INFO  com.vmware.netty.utils.buf.ByteBufTest - 2

还有种办法是采用 get 开头的一系列方法,这些方法不会改变 read index

释放

由于 Netty 中有堆外内存的 ByteBuf 实现,堆外内存最好是手动来释放,而不是等 GC 垃圾回收。

  • UnpooledHeapByteBuf 使用的是 JVM 内存,只需等 GC 回收内存即可
  • UnpooledDirectByteBuf 使用的就是直接内存了,需要特殊的方法来回收内存
  • PooledByteBuf 和它的子类使用了池化机制,需要更复杂的规则来回收内存

回收内存的源码实现,请关注下面方法的不同实现

protected abstract void deallocate()

Netty 这里采用了引用计数法来控制回收内存,每个 ByteBuf 都实现了 ReferenceCounted 接口

  • 每个 ByteBuf 对象的初始计数为 1
  • 调用 release 方法计数减 1,如果计数为 0,ByteBuf 内存被回收
  • 调用 retain 方法计数加 1,表示调用者没用完之前,其它 handler 即使调用了 release 也不会造成回收
  • 当计数为 0 时,底层内存会被回收,这时即使 ByteBuf 对象还在,其各个方法均无法正常使用

谁来负责 release 呢?

不是我们想象的(一般情况下)

ByteBuf buf = ...
try {
    ...
} finally {
    buf.release();
}

请思考,因为 pipeline 的存在,一般需要将 ByteBuf 传递给下一个 ChannelHandler,如果在 finally 中 release 了,就失去了传递性(当然,如果在这个 ChannelHandler 内这个 ByteBuf 已完成了它的使命,那么便无须再传递)

基本规则是,谁是最后使用者,谁负责 release,详细分析如下

  • 起点,对于 NIO 实现来讲,在 io.netty.channel.nio.AbstractNioByteChannel.NioByteUnsafe#read 方法中首次创建 ByteBuf 放入 pipeline(line 163 pipeline.fireChannelRead(byteBuf))
  • 入站 ByteBuf 处理原则
    • 对原始 ByteBuf 不做处理,调用 ctx.fireChannelRead(msg) 向后传递,这时无须 release
    • 将原始 ByteBuf 转换为其它类型的 Java 对象,这时 ByteBuf 就没用了,必须 release
    • 如果不调用 ctx.fireChannelRead(msg) 向后传递,那么也必须 release
    • 注意各种异常,如果 ByteBuf 没有成功传递到下一个 ChannelHandler,必须 release
    • 假设消息一直向后传,那么 TailContext 会负责释放未处理消息(原始的 ByteBuf)
  • 出站 ByteBuf 处理原则
    • 出站消息最终都会转为 ByteBuf 输出,一直向前传,由 HeadContext flush 后 release
  • 异常处理原则
    • 有时候不清楚 ByteBuf 被引用了多少次,但又必须彻底释放,可以循环调用 release 直到返回 true
while (!buffer.release()) {}

TailContext 释放未处理消息逻辑

// io.netty.channel.DefaultChannelPipeline#onUnhandledInboundMessage(java.lang.Object)
protected void onUnhandledInboundMessage(Object msg) {
    try {
        logger.debug(
            "Discarded inbound message {} that reached at the tail of the pipeline. " +
            "Please check your pipeline configuration.", msg);
    } finally {
        ReferenceCountUtil.release(msg);
    }
}

具体代码

// io.netty.util.ReferenceCountUtil#release(java.lang.Object)
public static boolean release(Object msg) {
    if (msg instanceof ReferenceCounted) {
        return ((ReferenceCounted) msg).release();
    }
    return false;
}

slice

【零拷贝】的体现之一,对原始 ByteBuf 进行切片成多个 ByteBuf,切片后的 ByteBuf 并没有发生内存复制,还是使用原始 ByteBuf 的内存,切片后的 ByteBuf 维护独立的 read,write 指针

在这里插入图片描述

无参 slice 是从原始 ByteBuf 的 read index 到 write index 之间的内容进行切片,切片后的 max capacity 被固定为这个区间的大小,因此不能追加 write

public class SliceTest {
    public static void main(String[] args) {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        buf.writeBytes(new byte[]{1, 2, 3, 4, 5, 6, 7, 8});
        buf.readByte();
        ByteBuf byteBuf = buf.slice();//02 03 04 05 06 07 08
        BufUtil.log(byteBuf);
        byteBuf.writeByte(11);//error
        BufUtil.log(byteBuf);
    }
}

输出

read index:0 write index:7 capacity:7
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 02 03 04 05 06 07 08                            |.......         |
+--------+-------------------------------------------------+----------------+
Exception in thread "main" java.lang.IndexOutOfBoundsException: writerIndex(7) + minWritableBytes(1) exceeds maxCapacity(7): UnpooledSlicedByteBuf(ridx: 0, widx: 7, cap: 7/7, unwrapped: PooledUnsafeDirectByteBuf(ridx: 1, widx: 8, cap: 256))
	at io.netty.buffer.AbstractByteBuf.ensureWritable0(AbstractByteBuf.java:295)
	at io.netty.buffer.AbstractByteBuf.writeByte(AbstractByteBuf.java:985)
	at com.vmware.netty.utils.buf.SliceTest.main(SliceTest.java:15)

Process finished with exit code 1

如果原始 ByteBuf 再次读操作

public class SliceTest {
    public static void main(String[] args) {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        buf.writeBytes(new byte[]{1, 2, 3, 4, 5, 6, 7, 8});
        buf.readByte();
        ByteBuf byteBuf = buf.slice();//02 03 04 05 06 07 08
        buf.readByte();//再次读
        BufUtil.log(byteBuf);
    }
}

输出

read index:0 write index:7 capacity:7
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 02 03 04 05 06 07 08                            |.......         |
+--------+-------------------------------------------------+----------------+

这时的byteBuf不受影响,因为它有独立的读写指针

但是如果byteBuf的内容发生了更改,原ByteBuf也会受到影响,因为底层都是同一块内存

public class SliceTest {
    public static void main(String[] args) {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        buf.writeBytes(new byte[]{1, 2, 3, 4, 5, 6, 7, 8});
        buf.readByte();
        ByteBuf byteBuf = buf.slice();//02 03 04 05 06 07 08
        byteBuf.setByte(0,16);
        BufUtil.log(byteBuf);
    }
}

输出

read index:0 write index:7 capacity:7
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 10 03 04 05 06 07 08                            |.......         |
+--------+-------------------------------------------------+----------------+

duplicate

零拷贝的体现之一,就好比截取了原始 ByteBuf 所有内容,并且没有 max capacity 的限制,也是与原始 ByteBuf 使用同一块底层内存,只是读写指针是独立的
在这里插入图片描述

copy

会将底层内存数据进行深拷贝,因此无论读写,都与原始 ByteBuf 无关

CompositeByteBuf

零拷贝的体现之一,可以将多个 ByteBuf 合并为一个逻辑上的 ByteBuf,避免拷贝

public class CompositeByteBufTest {
    public static void main(String[] args) {
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        buffer.writeBytes(new byte[]{1, 2, 3, 4, 5});
        ByteBuf buffer1 = ByteBufAllocator.DEFAULT.buffer();
        buffer1.writeBytes(new byte[]{6, 7, 8, 9, 10});
        CompositeByteBuf compositeByteBuf = ByteBufAllocator.DEFAULT.compositeBuffer();
        //true 表示增加新的 ByteBuf 自动递增 write index, 否则 write index 会始终为 0
        compositeByteBuf.addComponents(true,buffer,buffer1);
        BufUtil.log(compositeByteBuf);
    }
}

输出

read index:0 write index:10 capacity:10
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 01 02 03 04 05 06 07 08 09 0a                   |..........      |
+--------+-------------------------------------------------+----------------+

CompositeByteBuf 是一个组合的 ByteBuf,它内部维护了一个 Component 数组,每个 Component 管理一个 ByteBuf,记录了这个 ByteBuf 相对于整体偏移量等信息,代表着整体中某一段的数据。

  • 优点,对外是一个虚拟视图,组合这些 ByteBuf 不会产生内存复制
  • 缺点,复杂了很多,多次操作会带来性能的损耗

Unpooled

Unpooled 是一个工具类,类如其名,提供了非池化的 ByteBuf 创建、组合、复制等操作

这里仅介绍其跟【零拷贝】相关的 wrappedBuffer 方法,可以用来包装 ByteBuf

ByteBuf buf1 = ByteBufAllocator.DEFAULT.buffer(5);
buf1.writeBytes(new byte[]{1, 2, 3, 4, 5});
ByteBuf buf2 = ByteBufAllocator.DEFAULT.buffer(5);
buf2.writeBytes(new byte[]{6, 7, 8, 9, 10});

// 当包装 ByteBuf 个数超过一个时, 底层使用了 CompositeByteBuf
ByteBuf buf3 = Unpooled.wrappedBuffer(buf1, buf2);
System.out.println(ByteBufUtil.prettyHexDump(buf3));

输出

         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 01 02 03 04 05 06 07 08 09 0a                   |..........      |
+--------+-------------------------------------------------+----------------+

也可以用来包装普通字节数组,底层也不会有拷贝操作

ByteBuf buf4 = Unpooled.wrappedBuffer(new byte[]{1, 2, 3}, new byte[]{4, 5, 6});
System.out.println(buf4.getClass());
System.out.println(ByteBufUtil.prettyHexDump(buf4));

输出

class io.netty.buffer.CompositeByteBuf
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 01 02 03 04 05 06                               |......          |
+--------+-------------------------------------------------+----------------+

💡 ByteBuf 优势

  • 池化 - 可以重用池中 ByteBuf 实例,更节约内存,减少内存溢出的可能
  • 读写指针分离,不需要像 ByteBuffer 一样切换读写模式
  • 可以自动扩容
  • 支持链式调用,使用更流畅
  • 很多地方体现零拷贝,例如 slice、duplicate、CompositeByteBuf

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/7497.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

阿里云弹性计算高级产品专家马小婷:ECS 使用成熟度评估与洞察

2023 年 3 月 22 日&#xff0c;【全新升级 阿里云 ECS CloudOps 2.0 来啦】发布会正式播出&#xff0c;本次发布会上阿里云宣布 CloudOps&#xff08;云上自动化运维&#xff09;套件全新升级&#xff0c;并发布了 CloudOps 云上自动化运维白皮书 2.0 版本。阿里云弹性计算高级…

【数据结构】栈和队列(笔记总结)

&#x1f466;个人主页&#xff1a;Weraphael ✍&#x1f3fb;作者简介&#xff1a;目前学习C和算法 ✈️专栏&#xff1a;数据结构 &#x1f40b; 希望大家多多支持&#xff0c;咱一起进步&#xff01;&#x1f601; 如果文章对你有帮助的话 欢迎 评论&#x1f4ac; 点赞&…

DC-1渗透实战

目标机&#xff1a;192.168.26.161 攻击机&#xff1a;192.168.26.144 1、信息收集&#xff1a; 使用ARP扫描&#xff0c;获得信息&#xff1a; arp-scan -l 适应NMAP进行扫描IP为192.168.26.161 &#xff0c;看他开启了哪些端口、服务和操作系统&#xff1a; nmap -A -T…

SQL函数

文章目录一、SQL 函数二、SQL COUNT() 函数三、SQL FIRST() 函数四、SQL LAST() 函数五、SQL MAX() 函数总结一、SQL 函数 SQL 拥有很多可用于计数和计算的内建函数。 SQL Aggregate 函数 SQL Aggregate 函数计算从列中取得的值&#xff0c;返回一个单一的值。 有用的 Aggrega…

「程序员值得一看」| 传说中的“全球公认最健康的作息时间表”

身体是革命的本钱&#xff0c;健康问题关乎我们每一个人&#xff0c;说到健康作息&#xff0c;下面这篇文章还是非常值得我们参考的&#xff0c;当然每个人结合自身可以好好总结一下。 都说程序员这一行&#xff0c;猝死概率极高&#xff0c;究其原因还是很难有很好的作息规律…

【Matlab算法】粒子群算法求解二维线性优化问题(附MATLAB代码)

MATLAB求解二维线性优化问题前言正文函数实现可视化结果前言 二维线性优化问题指的是在二维空间中&#xff0c;对于一个由线性函数构成的目标函数&#xff0c;通过限制自变量的范围或满足特定的约束条件&#xff0c;寻找一个最优解&#xff08;最小值或最大值&#xff09;。这…

面试官 : 你了解的MySQL 集群高可用架构都有哪些?

文章目录 MySQL 主从架构MySQL+DRDB 架构MySQL+MHA 架构MySQL+MMM 架构MySQL 主从架构 此种架构,一般初创企业比较常用,也便于后面步步的扩展 此架构特点: 1、成本低,布署快速、方便 2、读写分离 3、还能通过及时增加从库来减少读库压力 4、主库单点故障 5、数据一致性问题…

Windows上提示 api-ms-win-core-path-l1-1-0.dll 丢失怎么办?

Windows上提示 api-ms-win-core-path-l1-1-0.dll 丢失怎么办&#xff1f;最近有用户在开启电脑的photoshop软件使用的时候&#xff0c;出现另外无法启动软件的情况&#xff0c;因为系统中缺失了对应的dll文件。那么这个情况怎么去进行问题的解决呢&#xff1f;来看看以下的解决…

PyTorch深度学习实战 | 基于YOLO V3的安全帽佩戴检测

本期将提供一个利用深度学习检测是否佩戴安全帽的案例,从而展示计算机视觉中的目标识别问题的一般流程。目标检测是基于图片分类的计算机视觉任务,既包含了分类,又包含了定位。给出一张图片,目标检测系统要能够识别出图片的目标并给出其位置。由于图片中目标数是不确定的,…

JUC并发编程第一章之进程/并发/异步的概念[理解基本概念]

1. 进程和线程的概念 进程: 系统正在运行的一个应用程序;程序一旦运行就是一个进程;进程是资源分配的最小单位 线程: 是进程的实际运行单位;一个人进程可以并发控制多个线程,每条线程并行执行不同的任务 区别: 进程基本上相互独立的;而线程存在于进程内&#xff0c;是进程…

类ChatGPT项目的部署与微调(上):从LLaMA到Alpaca、Vicuna、BELLE

前言 近期&#xff0c;除了研究ChatGPT背后的各种技术细节 不断看论文(至少100篇&#xff0c;100篇目录见此&#xff1a;ChatGPT相关技术必读论文100篇)&#xff0c;还开始研究一系列开源模型(包括各自对应的模型架构、训练方法、训练数据、本地私有化部署、硬件配置要求、微…

如何把多个文件(夹)随机复制到多个文件夹中

首先&#xff0c;需要用到的这个工具&#xff1a; 百度 密码&#xff1a;qwu2 蓝奏云 密码&#xff1a;2r1z 先看文件的情况一共20个兔兔的图片&#xff0c;4个文件夹&#xff0c;把全部的图片随机的复制这些地方去 打开工具&#xff0c;切换到 文件批量复制 版块 找到右下角…

Java EE企业级应用开发(SSM)第3章

第3章Spring Bean装配一.预习笔记 1.Spring中的Bean 在Spring中&#xff0c;一切Java类都被视为资源&#xff0c;而这些资源都被视为Bean&#xff0c;而Spring就是管理这些Bean的容器。 Bean的配置有3种方式&#xff0c;分别是XML文件配置、Java类和注解 2.基于XML的Bean装…

`Caché/IRIS` 代码优化效率提升十一条 - 持续更新

文章目录Cach/IRIS代码优化效率提升十一条 - 持续更新 汇总数据使用多维数组Global取数据时需将Global先赋值变量将表达式直接返回使用块语法的运行效率要比点语法更快复杂的if逻辑条件&#xff0c;可以调整顺序&#xff0c;让程序更高效在循环中取不变的配置时&#xff0c;应使…

SpringRetry接口异常优雅重试机制

场景&#xff1a; 某些场景下&#xff0c;如果接口出现异常需要进行重试&#xff0c;例如网络抖动、调用接口超时等并非接口代码导致的报错&#xff0c;此时可以进行接口重试机制 1、导入 spring retry 重试依赖 <!-- spring retry --><dependency><groupId>…

【Node.js】Express框架的基本使用

✍️ 作者简介: 前端新手学习中。 &#x1f482; 作者主页: 作者主页查看更多前端教学 &#x1f393; 专栏分享&#xff1a;css重难点教学 Node.js教学 从头开始学习 目录 初识Express Express简介 什么是Express 进一步理解 Express Express能做什么 Express的基本使用 …

分享:如何给 DBA 减负?

欢迎访问 OceanBase 官网获取更多信息&#xff1a;https://www.oceanbase.com/ 本文来自OceanBase社区分享&#xff0c;仅限交流探讨。原作者肖杨&#xff0c;OceanBase 软件开发工程师。 研发、数据分析师及运维内部人员有数据查询、数据订正等需求&#xff0c;若无管控平台&…

Midjourney 使用总结

1.关键词提问 中国古典女性&#xff0c;东方美女极致面容站在运河码头遥望丈夫&#xff0c;史诗奇幻场景风格&#xff0c;深绿浅棕&#xff0c;细节风帆&#xff0c;柔焦&#xff0c;人像隐喻&#xff0c;4K&#xff0c;电影级高品质照片&#xff0c;全景图&#xff0c; 焦距 …

突破市场壁垒:如何利用关键词采集和市场调查找到你的细分市场?

在市场竞争日益激烈的今天&#xff0c;寻找一个适合自己的细分市场成为了每个企业和创业者的必要之举。然而&#xff0c;许多人在寻找细分市场时陷入了困境&#xff0c;不知道如何找到一个符合自己产品的市场&#xff0c;因此&#xff0c;在这种情况下&#xff0c;利用关键词采…

UVM response_handler和get_response机制

很多UVM用户平时更多的使用get_response()方式去获得uvm_driver的response&#xff0c;但get_response有些缺点&#xff1a;由于 get_response() 是一种阻塞方法&#xff0c;它会阻塞直到收到来自 UVM 驱动程序 (put_response()) 的响应。因此&#xff0c;如果我们使用 get_res…
最新文章