diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 3bc8589..8951634 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Aug 01 15:00:16 CST 2018 +#Thu Jun 04 17:02:40 CST 2020 +distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-rc-2-all.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-rc-2-bin.zip +zipStoreBase=GRADLE_USER_HOME diff --git a/netty-pad/src/main/java/com/sv/netty/netty/BootService.java b/netty-pad/src/main/java/com/sv/netty/netty/BootService.java index f4ce634..234827e 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/BootService.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/BootService.java @@ -2,10 +2,7 @@ package com.sv.netty.netty; import com.google.common.collect.Maps; import io.netty.bootstrap.ServerBootstrap; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelHandler; -import io.netty.channel.ChannelOption; -import io.netty.channel.EventLoopGroup; +import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import org.slf4j.Logger; @@ -91,14 +88,19 @@ public class BootService { bootstrap.childOption(ChannelOption.SO_KEEPALIVE,keepalive); // 绑定一个端口并且同步 启动服务器 serverChannelFuture = bootstrap.bind(new InetSocketAddress(port)).sync(); - logger.info("成功bind端口:" + port); + // serverChannelFuture的用处 + serverChannelFuture.addListener(f -> { + if (f.isSuccess()){ + logger.info("成功bind端口:" + port); + } + }); // 对关闭通道进行监听 (异步模型) serverChannelFuture.channel().closeFuture().sync(); } catch (InterruptedException e) { logger.error("启动NETTY TCP异常:", e); } finally { - workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); + workerGroup.shutdownGracefully(); } } diff --git a/netty-pad/src/main/java/com/sv/netty/netty/ClientHandler.java b/netty-pad/src/main/java/com/sv/netty/netty/ClientHandler.java index 75e81e3..9ba542a 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/ClientHandler.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/ClientHandler.java @@ -33,6 +33,11 @@ public class ClientHandler extends ChannelInboundHandlerAdapter { super.channelActive(ctx); } + /** + * 当通道失效就会触发 + * @param ctx + * @throws Exception + */ @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); diff --git a/netty-pad/src/main/java/com/sv/netty/netty/ServerHandler.java b/netty-pad/src/main/java/com/sv/netty/netty/ServerHandler.java index 6963bc9..10c599f 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/ServerHandler.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/ServerHandler.java @@ -28,6 +28,11 @@ public class ServerHandler extends ChannelInboundHandlerAdapter { messageService = SpringContextHolder.getBean("messageService"); } + /** + * 通道就绪事件 + * @param ctx + * @throws Exception + */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { super.channelActive(ctx); @@ -42,6 +47,10 @@ public class ServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { super.channelRead(ctx, msg); + //taskQueue +// ctx.channel().eventLoop().execute() + //scheduleTaskQueue +// ctx.channel().eventLoop().schedule() String clientIp = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getClientIp(); try { messageService.receive(ctx.channel(), msg.toString()); diff --git a/netty-pad/src/main/java/com/sv/netty/netty/ServerProtocolInitializer.java b/netty-pad/src/main/java/com/sv/netty/netty/ServerProtocolInitializer.java index f5421b4..157d6c3 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/ServerProtocolInitializer.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/ServerProtocolInitializer.java @@ -29,11 +29,13 @@ public class ServerProtocolInitializer extends ChannelInitializer protected void initChannel(SocketChannel ch){ ChannelPipeline pipeline = ch.pipeline(); logger.info("ServerProtocolInitializer"); + // 通过指定的长度来标识整包的信息,这样就可以自动的处理粘包和半包的问题 pipeline.addFirst(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 4, 4, 0, 0)); - pipeline.addLast(new IdleStateHandler(IDLE_TIME, READ_TIME, WRITE_TIME)); pipeline.addLast(new ReadTimeoutHandler(READ_TIMEOUT)); pipeline.addLast(new MessageDecoder()); pipeline.addLast(new MessageEncoder()); + // 心跳检测机制,通过调用触发下一个handler userEventTriggered 方法 + pipeline.addLast(new IdleStateHandler(READ_TIME, WRITE_TIME,IDLE_TIME)); pipeline.addLast(new ServerHandler()); }