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("使用监听方法"); + } + }); + } +}