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); + } +}