소스 검색

定时10分钟发送一条空提示,防止连接断开

lgy 3 년 전
부모
커밋
144a96039e
2개의 변경된 파일10개의 추가작업 그리고 0개의 파일을 삭제
  1. 6 0
      src/main/java/com/persagy/job/SpringSchedule.java
  2. 4 0
      src/main/java/com/persagy/netty/client/CenterChannelInitializer.java

+ 6 - 0
src/main/java/com/persagy/job/SpringSchedule.java

@@ -38,6 +38,12 @@ public class SpringSchedule {
             log.warn("NettyClient is not init");
             return;
         }
+        try {
+            NettyMessage message = new NettyMessage(3);
+            nettyClient.sendMessage(message);
+        } catch (Exception e) {
+            log.error(e.getMessage(),e);
+        }
         log.info("NettyClient State:isActive[{}],isOpen[{}],isRegistered[{}],isWritable[{}]", NettyClient.channel.isActive(), NettyClient.channel.isOpen(), NettyClient.channel.isRegistered(), NettyClient.channel.isWritable());
         sengAlarmMessage();
     }

+ 4 - 0
src/main/java/com/persagy/netty/client/CenterChannelInitializer.java

@@ -6,6 +6,9 @@ import com.persagy.repository.AlarmRecordRepository;
 import com.persagy.service.AlarmDefineService;
 import io.netty.channel.ChannelInitializer;
 import io.netty.channel.socket.SocketChannel;
+import io.netty.handler.timeout.IdleStateHandler;
+
+import java.util.concurrent.TimeUnit;
 
 
 public class CenterChannelInitializer extends ChannelInitializer<SocketChannel> {
@@ -21,6 +24,7 @@ public class CenterChannelInitializer extends ChannelInitializer<SocketChannel>
 
     @Override
     protected void initChannel(SocketChannel channel) throws Exception {
+        channel.pipeline().addLast(new IdleStateHandler(0,60,0, TimeUnit.SECONDS));
         //对象传输处理
         channel.pipeline().addLast(new ObjDecoder());
         // 在管道中添加我们自己的接收数据实现方法