Browse Source

设备离线检测修改。

zhengqiang 5 years ago
parent
commit
5eb93570ca

+ 13 - 5
lapi/src/main/java/com/jpsoft/smart/lapi/LapiTcpServer.java

@@ -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);

+ 7 - 0
lapi/src/main/java/com/jpsoft/smart/lapi/handler/HeartReportHandler.java

@@ -104,6 +104,13 @@ public class HeartReportHandler extends SimpleChannelInboundHandler<HeartReportI
             log.warn(deviceNo + "已离线");
 
             new Thread(()->{
+                try {
+                    //防止channelRead0和channelInactive同时发生
+                    Thread.sleep(1000);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+
                 SysLog sysLog = new SysLog();
                 sysLog.setPointcut(deviceNo);
                 sysLog.setUrl("/LAPI/V1.0/PACS/Controller/HeartReportInfo");