From 15ace8f636ef54172db37d1aecccac1b36a7e2e4 Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Fri, 24 May 2024 08:58:45 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=96=B0=E5=A2=9E):=20:rocket:=20?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AFaddListener=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E5=8F=91=E9=80=81=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 2 +- netty/model/pom.xml | 12 +++++ netty/pom.xml | 2 +- .../service/netty/demo3/ChannelClient.java | 45 +++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 netty/service/src/main/java/cn/bunny/service/netty/demo3/ChannelClient.java diff --git a/.idea/misc.xml b/.idea/misc.xml index be09ff0..04e58ef 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -10,7 +10,7 @@ - + \ No newline at end of file diff --git a/netty/model/pom.xml b/netty/model/pom.xml index d3756f7..4cb1141 100644 --- a/netty/model/pom.xml +++ b/netty/model/pom.xml @@ -77,4 +77,16 @@ 1.6.14 + + + + org.apache.maven.plugins + maven-compiler-plugin + + 14 + 14 + + + + diff --git a/netty/pom.xml b/netty/pom.xml index 8c943dd..efb5fc8 100644 --- a/netty/pom.xml +++ b/netty/pom.xml @@ -25,7 +25,7 @@ 22 22 - 21 + 22 3.8.1 3.5.6 8.0.30 diff --git a/netty/service/src/main/java/cn/bunny/service/netty/demo3/ChannelClient.java b/netty/service/src/main/java/cn/bunny/service/netty/demo3/ChannelClient.java new file mode 100644 index 0000000..7a58a4d --- /dev/null +++ b/netty/service/src/main/java/cn/bunny/service/netty/demo3/ChannelClient.java @@ -0,0 +1,45 @@ +package cn.bunny.service.netty.demo3; + +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 lombok.extern.slf4j.Slf4j; + +import java.net.InetSocketAddress; + +@Slf4j +public class ChannelClient { + public static void main(String[] args) throws InterruptedException { + ChannelFuture channelFuture = new Bootstrap() + .group(new NioEventLoopGroup()) + .channel(NioSocketChannel.class) + .handler(new ChannelInitializer() { + @Override + protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception { + nioSocketChannel.pipeline().addLast(new StringEncoder()); + } + }) + .connect(new InetSocketAddress("localhost", 8080)); + // 使用 sycn 方法同步处理结果,五阻塞当前线程 + // channelFuture.sync(); + // 五阻塞向下执行获取Channel + // Channel channel = channelFuture.channel(); + // channel.writeAndFlush("哈哈哈哈"); + // 使用addLietener 方法异步处理结果 + + channelFuture.addListener(new ChannelFutureListener() { + @Override + // 在nio线程连接建立好之后,会调用aperationComplate + public void operationComplete(ChannelFuture channelFuture) throws Exception { + Channel channel = channelFuture.channel(); + log.debug("当前的 Channel: {}", channel); + channel.writeAndFlush("使用监听方法"); + } + }); + } +}