|
@@ -8,11 +8,14 @@ import io.netty.channel.ChannelInitializer;
|
|
|
import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
import io.netty.handler.codec.http.*;
|
|
|
+import io.netty.handler.timeout.IdleStateHandler;
|
|
|
+import io.netty.handler.timeout.ReadTimeoutHandler;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import javax.annotation.PreDestroy;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Slf4j
|
|
|
@Component
|
|
@@ -31,6 +34,8 @@ public class LapiTcpServer {
|
|
|
|
|
|
private Channel serverChannel;
|
|
|
|
|
|
+ private final int READ_TIME_OUT = 300;
|
|
|
+
|
|
|
public void start(int port){
|
|
|
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
|
|
|
NioEventLoopGroup workerGroup = new NioEventLoopGroup();
|
|
@@ -42,14 +47,17 @@ public class LapiTcpServer {
|
|
|
.childHandler(new ChannelInitializer(){
|
|
|
@Override
|
|
|
protected void initChannel(Channel channel) throws Exception {
|
|
|
-// channel.pipeline().addLast(new HttpRequestDecoder());
|
|
|
-// channel.pipeline().addLast(new HttpResponseDecoder());
|
|
|
-// channel.pipeline().addLast(new ParseResponseHandler());
|
|
|
-// channel.pipeline().addLast(new HttpClientCodec());
|
|
|
-// channel.pipeline().addLast(new HttpServerCodec());
|
|
|
+
|
|
|
+ //通道空闲超时时间
|
|
|
+ channel.pipeline().addLast(new IdleStateHandler(READ_TIME_OUT, READ_TIME_OUT, READ_TIME_OUT, TimeUnit.SECONDS));
|
|
|
+
|
|
|
channel.pipeline().addLast(new HttpEncoder());
|
|
|
channel.pipeline().addLast(new HttpDecoder());
|
|
|
channel.pipeline().addLast("aggregator", new HttpObjectAggregator(10 * 1024 * 1024));
|
|
|
+
|
|
|
+ //保活超时时间
|
|
|
+ channel.pipeline().addLast(new ReadTimeoutHandler(READ_TIME_OUT));
|
|
|
+
|
|
|
channel.pipeline().addLast(new ParseHttpHandler());
|
|
|
channel.pipeline().addLast(heartReportHandler);
|
|
|
channel.pipeline().addLast(personVerificationHandler);
|