From 9ff67fa63d301a5a16c1a0fd1ff09a752ab6d22a Mon Sep 17 00:00:00 2001 From: limqhz Date: Sun, 24 May 2020 23:28:52 +0800 Subject: [PATCH] FixBug NETTY --- .../java/com/sv/netty/netty/BootService.java | 22 ++++++++----------- .../netty/netty/ServerProtocolInitalizer.java | 20 +++++------------ 2 files changed, 14 insertions(+), 28 deletions(-) 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 eefdb47..69a0a4b 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 @@ -69,7 +69,7 @@ public class BootService { private Map channelOptions; @Autowired - private ChannelHandler channelHandler; + private ServerProtocolInitalizer serverProtocolInitalizer; /** * 初始化netty启动配置 @@ -79,17 +79,18 @@ public class BootService { bossGroup = new NioEventLoopGroup(); channelOptions = Maps.newHashMap(); try { - channelOptions.put(ChannelOption.SO_KEEPALIVE,keepalive); +// channelOptions.put(ChannelOption.SO_KEEPALIVE,keepalive); channelOptions.put(ChannelOption.SO_BACKLOG,backlog); - channelOptions.put(ChannelOption.TCP_NODELAY,TCP_NODELAY); +// channelOptions.put(ChannelOption.TCP_NODELAY,TCP_NODELAY); channelOptions.put(ChannelOption.SO_REUSEADDR,reuseaddr); bootstrap = new ServerBootstrap(); - bootstrap.group(bossGroup, workerGroup); - bootstrap.channel(NioServerSocketChannel.class); - bootstrap.childHandler(channelHandler); + bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) + .childHandler(serverProtocolInitalizer); for (Map.Entry entry : channelOptions.entrySet()) { bootstrap.option(entry.getKey(), entry.getValue()); } + bootstrap.childOption(ChannelOption.TCP_NODELAY,nodelay); + bootstrap.childOption(ChannelOption.SO_KEEPALIVE,keepalive); serverChannelFuture = bootstrap.bind(new InetSocketAddress(port)).sync(); logger.info("成功bind端口:" + port); @@ -106,7 +107,8 @@ public class BootService { * 开启单独的线程运行netty服务,避免和spring mvc冲突 */ public void run() { - messageExecutor = Executors.newFixedThreadPool(1); +// messageExecutor = Executors.newFixedThreadPool(1); + messageExecutor = Executors.newSingleThreadExecutor(); messageExecutor.execute(() -> init()); } @@ -123,10 +125,4 @@ public class BootService { public void setChannelOptions(Map channelOptions) { this.channelOptions = channelOptions; } - - public void setChannelHandler(ChannelHandler channelHandler) { - this.channelHandler = channelHandler; - } - - } diff --git a/netty-pad/src/main/java/com/sv/netty/netty/ServerProtocolInitalizer.java b/netty-pad/src/main/java/com/sv/netty/netty/ServerProtocolInitalizer.java index 7f3ad54..2bc7f93 100644 --- a/netty-pad/src/main/java/com/sv/netty/netty/ServerProtocolInitalizer.java +++ b/netty-pad/src/main/java/com/sv/netty/netty/ServerProtocolInitalizer.java @@ -8,10 +8,9 @@ import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.timeout.IdleStateHandler; import io.netty.handler.timeout.ReadTimeoutHandler; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.nio.charset.Charset; - /** * 服务器端协议初始化适配器 * @@ -21,11 +20,10 @@ import java.nio.charset.Charset; @Component public class ServerProtocolInitalizer extends ChannelInitializer { - private ChannelHandler handler; - private final int IDLE_TIME = 15; //连接检测空闲时间 - private final int READ_TIME = 15; //读超时时间 - private final int WRITE_TIME = 15; //写超时时间 - private final int READ_TIMEOUT = 300; //读超时时间 + private final int IDLE_TIME = 30; //连接检测空闲时间 + private final int READ_TIME = 30; //读超时时间 + private final int WRITE_TIME = 30; //写超时时间 + private final int READ_TIMEOUT = 600; //读超时时间 @Override protected void initChannel(SocketChannel ch) throws Exception { @@ -39,12 +37,4 @@ public class ServerProtocolInitalizer extends ChannelInitializer } - public ChannelHandler getHandler() { - return handler; - } - - public void setHandler(ChannelHandler handler) { - this.handler = handler; - } - }