Netty理解pipeline实验设计

我目前正处于Netty的学习过程中,所学的知识并没有覆盖到我接下来要进行的实验,但是为了更好的理解pipeline,我设计了如下的实验:

  1. 准备5个ChannelInBoundHandler,依次输出inBound 1~5
  2. 准备5个ChannelOutBoundHandler,依次输出outBound 1~5
  3. 发送一条消息,看是否是按照设计先输出inBound 1~5,然后再输出outBound 1~5

实验过程设计

我准备了如下的InboundHandler(1~4是一致的,我就不呈现了):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21

public class Inbound1 extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        System.out.println(this.getClass().getSimpleName());

        ctx.fireChannelRead(msg);
    }
}

public class Inbound5 extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        System.out.println(this.getClass().getSimpleName());

        ctx.writeAndFlush(msg);
    }
}

我准备了如下的OutboundHandler(1~5是一致的,我就不呈现了):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

public class Outbound1 extends ChannelOutboundHandlerAdapter {
    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {

        System.out.println(this.getClass().getSimpleName());

        ctx.write(msg);
    }
}

然后开发Bootstrap类,如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

public static void main(String[] args) throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup(); //8个NioEventLoop

    try {

        ServerBootstrap serverBootstrap = new ServerBootstrap()
                .group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline()
                                .addLast(new Outbound1())
                                .addLast(new Outbound2())
                                .addLast(new Outbound3())
                                .addLast(new Outbound4())
                                .addLast(new Outbound5())
                                .addLast(new Inbound1())
                                .addLast(new Inbound2())
                                .addLast(new Inbound3())
                                .addLast(new Inbound4())
                                .addLast(new Inbound5());
                    }
                });

        System.out.println("Server Ready.");

        ChannelFuture channelFuture = serverBootstrap.bind(Constant.PORT).sync();
        channelFuture.channel().closeFuture().sync();

    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

启动服务端后,使用telnet工具进行链接并发送消息,服务端有如下输出:

2021-07-27-22-09-22

实验中遇到的问题

一开始的时候,我进行Handler编排的时候按如下代码编排:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22

ServerBootstrap serverBootstrap = new ServerBootstrap()
        .group(bossGroup, workerGroup)
        .channel(NioServerSocketChannel.class)
        .handler(new LoggingHandler(LogLevel.INFO))
        .childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel socketChannel) throws Exception {
                socketChannel.pipeline()
                        .addLast(new Inbound1())
                        .addLast(new Inbound2())
                        .addLast(new Inbound3())
                        .addLast(new Inbound4())
                        .addLast(new Inbound5())
                        .addLast(new Outbound1())
                        .addLast(new Outbound2())
                        .addLast(new Outbound3())
                        .addLast(new Outbound4())
                        .addLast(new Outbound5());
            }
        });

可以看到,我将inbound写在了前头将outbound写在了后头,最终导致了Outbound中没有任何输出。