From 150a71a1e407e118b71791166830eaf894152275 Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Fri, 24 May 2024 09:53:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=96=B0=E5=A2=9E):=20:rocket:=20PipLineS?= =?UTF-8?q?erver=E7=9A=84=E8=BF=9B=E6=A0=88=E5=92=8C=E5=87=BA=E6=A0=88?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/netty/demo5/PipLineServer.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 netty/service/src/main/java/cn/bunny/service/netty/demo5/PipLineServer.java diff --git a/netty/service/src/main/java/cn/bunny/service/netty/demo5/PipLineServer.java b/netty/service/src/main/java/cn/bunny/service/netty/demo5/PipLineServer.java new file mode 100644 index 0000000..2e9bf15 --- /dev/null +++ b/netty/service/src/main/java/cn/bunny/service/netty/demo5/PipLineServer.java @@ -0,0 +1,79 @@ +package cn.bunny.service.netty.demo5; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class PipLineServer { + public static void main(String[] args) { + new ServerBootstrap() + .group(new NioEventLoopGroup()) + .channel(NioServerSocketChannel.class) + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception { + // 通过 Channel 拿到pipLine + ChannelPipeline pipeline = nioSocketChannel.pipeline(); + // handler 处理结果 + pipeline.addLast("h1", new ChannelInboundHandlerAdapter() { + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + log.info("第一个"); + super.channelRead(ctx, msg); + } + }); + + // handler 第二个处理结果 + pipeline.addLast("h2", new ChannelInboundHandlerAdapter() { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + log.info("第二个"); + super.channelRead(ctx, msg); + } + }); + // handler 第三个处理结果 + pipeline.addLast("h3", new ChannelInboundHandlerAdapter() { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + log.info("第三个"); + super.channelRead(ctx, msg); + nioSocketChannel.writeAndFlush(ctx.alloc().buffer().writeBytes("服务".getBytes())); + } + }); + + // 第四个出栈内容 + pipeline.addLast("h4", new ChannelOutboundHandlerAdapter() { + @Override + public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { + log.info("第四个 出栈"); + super.write(ctx, msg, promise); + } + }); + + // 第五个出栈内容 + pipeline.addLast("h5", new ChannelOutboundHandlerAdapter() { + @Override + public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { + log.info("第五个 出栈"); + super.write(ctx, msg, promise); + } + }); + + // 第六个出栈内容 + pipeline.addLast("h6", new ChannelOutboundHandlerAdapter() { + @Override + public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { + log.info("第六个 出栈"); + super.write(ctx, msg, promise); + } + }); + } + }).bind(8080); + + // 进栈:按照顺序进行 + // 出栈:从后往前走 + } +}