作者 crossoverJie

:white_check_mark: Adding tests.

@@ -183,6 +183,9 @@ public class CIMClient { @@ -183,6 +183,9 @@ public class CIMClient {
183 183
184 184
185 public void reconnect() throws Exception { 185 public void reconnect() throws Exception {
  186 + if (channel != null && channel.isActive()) {
  187 + return;
  188 + }
186 start(); 189 start();
187 } 190 }
188 191
1 package com.crossoverjie.cim.client.handle; 1 package com.crossoverjie.cim.client.handle;
2 2
  3 +import com.crossoverjie.cim.client.thread.HeartBeatJob;
3 import com.crossoverjie.cim.client.util.SpringBeanFactory; 4 import com.crossoverjie.cim.client.util.SpringBeanFactory;
4 import com.crossoverjie.cim.common.constant.Constants; 5 import com.crossoverjie.cim.common.constant.Constants;
5 import com.crossoverjie.cim.common.protocol.CIMRequestProto; 6 import com.crossoverjie.cim.common.protocol.CIMRequestProto;
@@ -16,6 +17,7 @@ import org.slf4j.LoggerFactory; @@ -16,6 +17,7 @@ import org.slf4j.LoggerFactory;
16 17
17 import java.util.concurrent.ScheduledExecutorService; 18 import java.util.concurrent.ScheduledExecutorService;
18 import java.util.concurrent.ThreadPoolExecutor; 19 import java.util.concurrent.ThreadPoolExecutor;
  20 +import java.util.concurrent.TimeUnit;
19 21
20 /** 22 /**
21 * Function: 23 * Function:
@@ -66,6 +68,15 @@ public class CIMClientHandle extends SimpleChannelInboundHandler<CIMResponseProt @@ -66,6 +68,15 @@ public class CIMClientHandle extends SimpleChannelInboundHandler<CIMResponseProt
66 } 68 }
67 69
68 @Override 70 @Override
  71 + public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  72 + LOGGER.info("客户端断开了!");
  73 + if (scheduledExecutorService == null){
  74 + scheduledExecutorService = SpringBeanFactory.getBean("scheduledTask",ScheduledExecutorService.class) ;
  75 + }
  76 + scheduledExecutorService.scheduleAtFixedRate(new HeartBeatJob(ctx),0,10, TimeUnit.SECONDS) ;
  77 + }
  78 +
  79 + @Override
69 protected void channelRead0(ChannelHandlerContext ctx, CIMResponseProto.CIMResProtocol msg) throws Exception { 80 protected void channelRead0(ChannelHandlerContext ctx, CIMResponseProto.CIMResProtocol msg) throws Exception {
70 81
71 //心跳更新时间 82 //心跳更新时间
@@ -81,12 +92,9 @@ public class CIMClientHandle extends SimpleChannelInboundHandler<CIMResponseProt @@ -81,12 +92,9 @@ public class CIMClientHandle extends SimpleChannelInboundHandler<CIMResponseProt
81 LOGGER.info(msg.getResMsg()); 92 LOGGER.info(msg.getResMsg());
82 } 93 }
83 94
84 - if (scheduledExecutorService == null){  
85 - scheduledExecutorService = SpringBeanFactory.getBean("scheduledTask",ScheduledExecutorService.class) ;  
86 - }  
87 95
88 96
89 - //scheduledExecutorService.scheduleAtFixedRate(new HeartBeatJob(ctx),60,60, TimeUnit.SECONDS) ; 97 +
90 98
91 } 99 }
92 100
@@ -4,7 +4,6 @@ import com.crossoverjie.cim.client.client.CIMClient; @@ -4,7 +4,6 @@ import com.crossoverjie.cim.client.client.CIMClient;
4 import com.crossoverjie.cim.client.config.AppConfiguration; 4 import com.crossoverjie.cim.client.config.AppConfiguration;
5 import com.crossoverjie.cim.client.service.RouteRequest; 5 import com.crossoverjie.cim.client.service.RouteRequest;
6 import com.crossoverjie.cim.common.kit.HeartBeatHandler; 6 import com.crossoverjie.cim.common.kit.HeartBeatHandler;
7 -import com.crossoverjie.cim.common.util.NettyAttrUtil;  
8 import io.netty.channel.ChannelHandlerContext; 7 import io.netty.channel.ChannelHandlerContext;
9 import okhttp3.MediaType; 8 import okhttp3.MediaType;
10 import org.slf4j.Logger; 9 import org.slf4j.Logger;
@@ -26,10 +25,10 @@ public class ClientHeartBeatHandlerImpl implements HeartBeatHandler { @@ -26,10 +25,10 @@ public class ClientHeartBeatHandlerImpl implements HeartBeatHandler {
26 private final MediaType mediaType = MediaType.parse("application/json"); 25 private final MediaType mediaType = MediaType.parse("application/json");
27 26
28 @Autowired 27 @Autowired
29 - private AppConfiguration appConfiguration ; 28 + private AppConfiguration appConfiguration;
30 29
31 @Autowired 30 @Autowired
32 - private CIMClient cimClient ; 31 + private CIMClient cimClient;
33 32
34 @Autowired 33 @Autowired
35 private RouteRequest routeRequest; 34 private RouteRequest routeRequest;
@@ -37,13 +36,6 @@ public class ClientHeartBeatHandlerImpl implements HeartBeatHandler { @@ -37,13 +36,6 @@ public class ClientHeartBeatHandlerImpl implements HeartBeatHandler {
37 @Override 36 @Override
38 public void process(ChannelHandlerContext ctx) throws Exception { 37 public void process(ChannelHandlerContext ctx) throws Exception {
39 38
40 - long heartBeatTime = appConfiguration.getHeartBeatTime() * 1000;  
41 -  
42 - Long lastReadTime = NettyAttrUtil.getReaderTime(ctx.channel());  
43 - long now = System.currentTimeMillis();  
44 - if (lastReadTime != null && now - lastReadTime > heartBeatTime){  
45 - LOGGER.warn("服务端心跳超时[{}]ms,[{}]需要关闭重新连接!",now - lastReadTime,appConfiguration.getUserName());  
46 -  
47 //首先清除路由信息,下线 39 //首先清除路由信息,下线
48 routeRequest.offLine(); 40 routeRequest.offLine();
49 41
@@ -51,7 +43,6 @@ public class ClientHeartBeatHandlerImpl implements HeartBeatHandler { @@ -51,7 +43,6 @@ public class ClientHeartBeatHandlerImpl implements HeartBeatHandler {
51 cimClient.reconnect(); 43 cimClient.reconnect();
52 44
53 } 45 }
54 - }  
55 46
56 47
57 } 48 }
@@ -32,7 +32,7 @@ public class HeartBeatJob implements Runnable { @@ -32,7 +32,7 @@ public class HeartBeatJob implements Runnable {
32 try { 32 try {
33 heartBeatHandler.process(context); 33 heartBeatHandler.process(context);
34 } catch (Exception e) { 34 } catch (Exception e) {
35 - e.printStackTrace(); 35 + LOGGER.error("Exception",e);
36 } 36 }
37 } 37 }
38 } 38 }
@@ -26,7 +26,7 @@ public class CIMServerInitializer extends ChannelInitializer<Channel> { @@ -26,7 +26,7 @@ public class CIMServerInitializer extends ChannelInitializer<Channel> {
26 26
27 ch.pipeline() 27 ch.pipeline()
28 //30 秒没有向客户端发送消息就发生心跳 28 //30 秒没有向客户端发送消息就发生心跳
29 - .addLast(new IdleStateHandler(15, 0, 0)) 29 + .addLast(new IdleStateHandler(11, 0, 0))
30 // google Protobuf 编解码 30 // google Protobuf 编解码
31 .addLast(new ProtobufVarint32FrameDecoder()) 31 .addLast(new ProtobufVarint32FrameDecoder())
32 .addLast(new ProtobufDecoder(CIMRequestProto.CIMReqProtocol.getDefaultInstance())) 32 .addLast(new ProtobufDecoder(CIMRequestProto.CIMReqProtocol.getDefaultInstance()))