From 41a4c7f38897d1939d18b0f0b8718ad9f0b60d2f Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Thu, 23 May 2024 15:56:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=96=B0=E5=A2=9E):=20:rocket:=20NettyFut?= =?UTF-8?q?ure=E5=85=88=E5=AF=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/java/cn/bunny/InputClientTest.java | 62 +++++++++++++++++++ .../cn/bunny/demo3/EventLoopServerTest.java | 4 +- .../demo4/TestDefaultServerLoopGroup.java | 45 ++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 netty/service/src/test/java/cn/bunny/InputClientTest.java create mode 100644 netty/service/src/test/java/cn/bunny/demo4/TestDefaultServerLoopGroup.java diff --git a/netty/service/src/test/java/cn/bunny/InputClientTest.java b/netty/service/src/test/java/cn/bunny/InputClientTest.java new file mode 100644 index 0000000..10c997c --- /dev/null +++ b/netty/service/src/test/java/cn/bunny/InputClientTest.java @@ -0,0 +1,62 @@ +package cn.bunny; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.handler.codec.string.StringEncoder; +import io.netty.handler.logging.LogLevel; +import io.netty.handler.logging.LoggingHandler; +import lombok.extern.slf4j.Slf4j; + +import java.net.InetSocketAddress; +import java.util.Scanner; + +@Slf4j +public class InputClientTest { + public static void main(String[] args) throws InterruptedException { + NioEventLoopGroup group = new NioEventLoopGroup(); + ChannelFuture future = new Bootstrap() + // .group(new NioEventLoopGroup()) + .group(group) + .channel(NioSocketChannel.class) + .handler(new ChannelInitializer() { + @Override + protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception { + nioSocketChannel.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG)); + nioSocketChannel.pipeline().addLast(new StringEncoder()); + } + }).connect(new InetSocketAddress("localhost", 8080)); + + Channel channel = future.sync().channel(); + + new Thread(() -> { + Scanner scanner = new Scanner(System.in); + while (true) { + String line = scanner.nextLine(); + if ("q".equals(line)) { + channel.close(); + break; + } + channel.writeAndFlush(line); + } + }, "input").start(); + + // 获取CloseFuture 对象,同步处理处理关闭 异步处理要关闭 + ChannelFuture channelFuture = channel.closeFuture(); + System.out.println("等待关闭......"); + + // 第一种方式关闭 + // channelFuture.sync(); + // log.debug("处理之后的关闭"); + + // 第二种方式关闭 + channelFuture.addListener((ChannelFutureListener) future1 -> { + log.debug("处理之后的关闭"); + group.shutdownGracefully(); + }); + } +} diff --git a/netty/service/src/test/java/cn/bunny/demo3/EventLoopServerTest.java b/netty/service/src/test/java/cn/bunny/demo3/EventLoopServerTest.java index b223aaf..47ea21a 100644 --- a/netty/service/src/test/java/cn/bunny/demo3/EventLoopServerTest.java +++ b/netty/service/src/test/java/cn/bunny/demo3/EventLoopServerTest.java @@ -15,8 +15,10 @@ import java.nio.charset.Charset; @Slf4j public class EventLoopServerTest { public static void main(String[] args) { + // 创建服务器 new ServerBootstrap() - .group(new NioEventLoopGroup()) + // BOOS只负责accept事件,worker 只负责 SocketChannel 上的读写 + .group(new NioEventLoopGroup(), new NioEventLoopGroup(2))// 设置工作线程为2个 .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer() { @Override diff --git a/netty/service/src/test/java/cn/bunny/demo4/TestDefaultServerLoopGroup.java b/netty/service/src/test/java/cn/bunny/demo4/TestDefaultServerLoopGroup.java new file mode 100644 index 0000000..0ff4212 --- /dev/null +++ b/netty/service/src/test/java/cn/bunny/demo4/TestDefaultServerLoopGroup.java @@ -0,0 +1,45 @@ +package cn.bunny.demo4; + +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 TestDefaultServerLoopGroup { + 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("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, "hander2", new ChannelInboundHandlerAdapter() { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + ByteBuf buf = (ByteBuf) msg; + log.debug(buf.toString(Charset.defaultCharset())); + super.channelRead(ctx, msg); + } + }); + } + }).bind(8080); + } +}