From 8275bdc2815d1dbe9f2667f51af1ecc2b14e80b9 Mon Sep 17 00:00:00 2001 From: Bunny <1319900154@qq.com> Date: Thu, 23 May 2024 23:47:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=96=B0=E5=A2=9E):=20:rocket:=20netty?= =?UTF-8?q?=E7=9A=84=20NioEventLoopGroup=20=E7=9A=84=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E7=AB=AF=E6=B6=88=E6=81=AF=E5=90=91=E4=B8=8B=E4=BC=A0=E9=80=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo2/EventLoopIoServer向下传递.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 netty/service/src/main/java/cn/bunny/service/netty/demo2/EventLoopIoServer向下传递.java diff --git a/netty/service/src/main/java/cn/bunny/service/netty/demo2/EventLoopIoServer向下传递.java b/netty/service/src/main/java/cn/bunny/service/netty/demo2/EventLoopIoServer向下传递.java new file mode 100644 index 0000000..4d93b44 --- /dev/null +++ b/netty/service/src/main/java/cn/bunny/service/netty/demo2/EventLoopIoServer向下传递.java @@ -0,0 +1,45 @@ +package cn.bunny.service.netty.demo2; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.DefaultEventLoopGroup; +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; + +import java.nio.charset.Charset; + +@Slf4j +public class EventLoopIoServer向下传递 { + public static void main(String[] args) { + DefaultEventLoopGroup group = new DefaultEventLoopGroup(); + new ServerBootstrap() + .group(new NioEventLoopGroup(), new NioEventLoopGroup(2)) + .channel(NioServerSocketChannel.class) + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception { + nioSocketChannel.pipeline().addLast(group, "handler", new ChannelInboundHandlerAdapter() { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + ByteBuf buf = (ByteBuf) msg; + log.debug(buf.toString(Charset.defaultCharset())); + // 让消息继续传递下去 + ctx.fireChannelRead(msg); + } + }) + .addLast(group, "handler2", new ChannelInboundHandlerAdapter() { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + ByteBuf buf = (ByteBuf) msg; + log.debug(buf.toString(Charset.defaultCharset())); + } + }); + } + }).bind(8080); + } +}