From 1145f50691f91c140dba71e32db5e05cdf0449be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Sun, 23 Apr 2023 11:26:13 +0800 Subject: [PATCH] fix:group --- .../{ => c1_basic}/HelloClient.java | 2 +- .../{ => c1_basic}/HelloServer.java | 2 +- .../TestEventLoop_01_EventLoopGroup.java" | 31 +++++++++++ .../TestEventLoop_02_submit.java" | 35 +++++++++++++ ...TestEventLoop_03_scheduleAtFixedRate.java" | 30 +++++++++++ .../TestEventLoop_04_Client.java" | 34 ++++++++++++ .../TestEventLoop_04_server.java" | 45 ++++++++++++++++ .../TestEventLoop_05_group.java" | 52 +++++++++++++++++++ 8 files changed, 229 insertions(+), 2 deletions(-) rename src/main/java/com/kwan/shuyu/heima/netty_04_netty/{ => c1_basic}/HelloClient.java (95%) rename src/main/java/com/kwan/shuyu/heima/netty_04_netty/{ => c1_basic}/HelloServer.java (97%) create mode 100644 "src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_01_EventLoopGroup.java" create mode 100644 "src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_02_submit.java" create mode 100644 "src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_03_scheduleAtFixedRate.java" create mode 100644 "src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_04_Client.java" create mode 100644 "src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_04_server.java" create mode 100644 "src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_05_group.java" diff --git a/src/main/java/com/kwan/shuyu/heima/netty_04_netty/HelloClient.java b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c1_basic/HelloClient.java similarity index 95% rename from src/main/java/com/kwan/shuyu/heima/netty_04_netty/HelloClient.java rename to src/main/java/com/kwan/shuyu/heima/netty_04_netty/c1_basic/HelloClient.java index dbb42e0..67102e1 100644 --- a/src/main/java/com/kwan/shuyu/heima/netty_04_netty/HelloClient.java +++ b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c1_basic/HelloClient.java @@ -1,4 +1,4 @@ -package com.kwan.shuyu.heima.netty_04_netty; +package com.kwan.shuyu.heima.netty_04_netty.c1_basic; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelInitializer; diff --git a/src/main/java/com/kwan/shuyu/heima/netty_04_netty/HelloServer.java b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c1_basic/HelloServer.java similarity index 97% rename from src/main/java/com/kwan/shuyu/heima/netty_04_netty/HelloServer.java rename to src/main/java/com/kwan/shuyu/heima/netty_04_netty/c1_basic/HelloServer.java index 73d992f..450980c 100644 --- a/src/main/java/com/kwan/shuyu/heima/netty_04_netty/HelloServer.java +++ b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c1_basic/HelloServer.java @@ -1,4 +1,4 @@ -package com.kwan.shuyu.heima.netty_04_netty; +package com.kwan.shuyu.heima.netty_04_netty.c1_basic; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelHandlerContext; diff --git "a/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_01_EventLoopGroup.java" "b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_01_EventLoopGroup.java" new file mode 100644 index 0000000..9ddd124 --- /dev/null +++ "b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_01_EventLoopGroup.java" @@ -0,0 +1,31 @@ +package com.kwan.shuyu.heima.netty_04_netty.c2_组件; + + +import io.netty.channel.DefaultEventLoopGroup; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import lombok.extern.slf4j.Slf4j; + +/** + * 测试EventLoop + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/23 10:51 + */ +@Slf4j +public class TestEventLoop_01_EventLoopGroup { + + public static void main(String[] args) { + //创建事件循环组 + EventLoopGroup group1 = new NioEventLoopGroup(2);//io 本件,普通任务,定时任务 + EventLoopGroup group2 = new DefaultEventLoopGroup();// 普通任务,定时任务 + System.out.println(group1.next()); + System.out.println(group1.next()); + System.out.println(group1.next()); + System.out.println(group1.next()); + System.out.println(group1.next()); + System.out.println(group1.next()); + System.out.println(group1.next()); + } +} diff --git "a/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_02_submit.java" "b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_02_submit.java" new file mode 100644 index 0000000..7695320 --- /dev/null +++ "b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_02_submit.java" @@ -0,0 +1,35 @@ +package com.kwan.shuyu.heima.netty_04_netty.c2_组件; + + +import io.netty.channel.DefaultEventLoopGroup; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import lombok.extern.slf4j.Slf4j; + +/** + * 测试EventLoop + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/23 10:51 + */ +@Slf4j +public class TestEventLoop_02_submit { + + public static void main(String[] args) { + //创建事件循环组 + EventLoopGroup group1 = new NioEventLoopGroup(2);//io 本件,普通任务,定时任务 + EventLoopGroup group2 = new DefaultEventLoopGroup();// 普通任务,定时任务 + System.out.println(group1.next()); + //提交普通任务 + group1.next().submit(() -> { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + log.info("ok"); + }); + log.info("main"); + } +} diff --git "a/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_03_scheduleAtFixedRate.java" "b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_03_scheduleAtFixedRate.java" new file mode 100644 index 0000000..25cec7b --- /dev/null +++ "b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_03_scheduleAtFixedRate.java" @@ -0,0 +1,30 @@ +package com.kwan.shuyu.heima.netty_04_netty.c2_组件; + + +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import lombok.extern.slf4j.Slf4j; + +import java.util.concurrent.TimeUnit; + +/** + * 测试EventLoop + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/23 10:51 + */ +@Slf4j +public class TestEventLoop_03_scheduleAtFixedRate { + + public static void main(String[] args) { + //创建事件循环组 + EventLoopGroup group1 = new NioEventLoopGroup(2);//io 本件,普通任务,定时任务 + System.out.println(group1.next()); + //执行定时任务 + group1.next().scheduleAtFixedRate(() -> { + log.info("ok"); + }, 0, 1, TimeUnit.SECONDS); + log.info("main"); + } +} diff --git "a/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_04_Client.java" "b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_04_Client.java" new file mode 100644 index 0000000..cabb12a --- /dev/null +++ "b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_04_Client.java" @@ -0,0 +1,34 @@ +package com.kwan.shuyu.heima.netty_04_netty.c2_组件; + +import io.netty.bootstrap.Bootstrap; +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 java.net.InetSocketAddress; + +public class TestEventLoop_04_Client { + public static void main(String[] args) throws InterruptedException { + //1.启动类 + new Bootstrap() + // 2.添加 EventLoop + .group(new NioEventLoopGroup()) + //3.选择客户端channel实现 + .channel(NioSocketChannel.class) + // 4.添加处理器 + .handler(new ChannelInitializer() { + @Override //在连接建立后被调用 + protected void initChannel(NioSocketChannel ch) throws Exception { + ch.pipeline().addLast(new StringEncoder()); + } + }) + //连接到服务器 + .connect(new InetSocketAddress("localhost", 8080)) + .sync() + .channel() + //发送数据 + .writeAndFlush("hello world") + ; + } +} \ No newline at end of file diff --git "a/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_04_server.java" "b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_04_server.java" new file mode 100644 index 0000000..a3837c5 --- /dev/null +++ "b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_04_server.java" @@ -0,0 +1,45 @@ +package com.kwan.shuyu.heima.netty_04_netty.c2_组件; + + +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.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; + +/** + * 测试EventLoop + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/23 10:51 + */ +@Slf4j +public class TestEventLoop_04_server { + + public static void main(String[] args) { + new ServerBootstrap() + .group(new NioEventLoopGroup(2)) + .channel(NioServerSocketChannel.class) + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(NioSocketChannel ch) throws Exception { + ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { + // ByteBuf + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + ByteBuf buf = (ByteBuf) msg; + log.info(buf.toString(Charset.defaultCharset())); + } + }); + } + }) + .bind(8080); + } +} diff --git "a/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_05_group.java" "b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_05_group.java" new file mode 100644 index 0000000..8d6bec9 --- /dev/null +++ "b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_05_group.java" @@ -0,0 +1,52 @@ +package com.kwan.shuyu.heima.netty_04_netty.c2_组件; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.ByteBuf; +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; + +import java.nio.charset.Charset; + +/** + * 测试EventLoop + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/23 10:51 + */ +@Slf4j +public class TestEventLoop_05_group { + public static void main(String[] args) { + //普通任务 + final EventLoopGroup group = new DefaultEventLoopGroup(); + new ServerBootstrap() + //boss 只负责ServerSocketChannel 上 accept 事件 worker只负责socketChannel上的读写 + .group(new NioEventLoopGroup(), new NioEventLoopGroup(2)) + .channel(NioServerSocketChannel.class) + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(NioSocketChannel ch) throws Exception { + ch.pipeline().addLast("handler1", new ChannelInboundHandlerAdapter() { + // ByteBuf + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + ByteBuf buf = (ByteBuf) msg; + log.info(buf.toString(Charset.defaultCharset())); + ctx.fireChannelRead(msg); + } + }).addLast(group, "handler2", new ChannelInboundHandlerAdapter() { + // handler2 是一个普通任务 + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + ByteBuf buf = (ByteBuf) msg; + log.info(buf.toString(Charset.defaultCharset())); + } + }); + } + }) + .bind(8080); + } +} \ No newline at end of file -- GitLab