1、优化TCPClient。TCP断开马上重连的问题:在异常情况下,如连上马上被断开,会出现死循环;改为发数据前的一分钟重连

This commit is contained in:
weidong 2024-01-10 08:46:03 +08:00
parent 73eebcd493
commit 121771b068
4 changed files with 29 additions and 28 deletions

View File

@ -45,7 +45,7 @@ public class TCPClient {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
//加入处理器
ch.pipeline().addLast(new TcpMessageHandler(TCPClient.this));
ch.pipeline().addLast(new TcpMessageHandler());
}
});
}
@ -67,7 +67,7 @@ public class TCPClient {
} catch (Exception e) {
e.printStackTrace();
}
}, 3000, TimeUnit.MILLISECONDS);
}, 5000, TimeUnit.MILLISECONDS);
} else {
logger.info("xfz tcp client start success!");
}
@ -94,4 +94,8 @@ public class TCPClient {
}
});
}
public boolean isActive(){
return channel.isActive();
}
}

View File

@ -15,12 +15,6 @@ public class TcpMessageHandler extends SimpleChannelInboundHandler<ByteBuf> {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final TCPClient tcpClient;
public TcpMessageHandler(TCPClient tcpClient) {
this.tcpClient = tcpClient;
}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf buf) throws Exception {
if (logger.isDebugEnabled()) {
@ -38,7 +32,6 @@ public class TcpMessageHandler extends SimpleChannelInboundHandler<ByteBuf> {
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
logger.info("xfz tcp channel inactive");
tcpClient.connect();
}
@Override

View File

@ -7,7 +7,6 @@ import com.imdroid.common.util.NumberUtils;
import com.imdroid.secapi.dto.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
@ -39,30 +38,29 @@ public class GXXfzForwarder extends Forwarder{
@PostConstruct
void registerMe(){
init(FORWARDER_NAME, "TCP "+host+":"+port);
xfzTcpClient = new TCPClient();
xfzTcpClient.init(host, port);
xfzTcpClient.start();
}
@Scheduled(cron = "0 29,39,59 * * * ?") // 每29分/59分执行一次
private void checkTCP() {
if(!xfzTcpClient.isActive()) {
xfzTcpClient.connect();
}
}
/**
* 每半小时转发GNSS解算结果
*/
@Scheduled(cron = "0 0/30 * * * ?") // 每30分钟执行一次
private void forwardGnss() {
// 1.按项目检索最近半小时的解算结果记录推送Enable为true的记录
// 2.如果某终端最近半小时没有数据则记录本次时间为待补传时间
if(xfzTcpClient == null){
xfzTcpClient = new TCPClient();
xfzTcpClient.init(host, port);
xfzTcpClient.start();
}
forwardCurrentGnss(FORWARDER_NAME);
}
@Scheduled(cron = "0 40 * * * ?") // 每小时的40分钟执行一次
//@Scheduled(cron = "0 0/20 * * * ?") // 每20分钟执行一次
private void forwardHistoryGnss() {
if(xfzTcpClient == null){
xfzTcpClient = new TCPClient();
xfzTcpClient.init(host, port);
xfzTcpClient.start();
}
forwardHistoryGnss(FORWARDER_NAME);
}
@ -92,10 +90,16 @@ public class GXXfzForwarder extends Forwarder{
sendNum++;
}
String json = GsonUtil.toJson(xfzTcpMessage);
xfzTcpClient.writeAndFlush(json);
logger.info("project " + projectId + ": push calculation result to XFZ");
logger.info(json);
return sendNum;
if(xfzTcpClient.isActive()) {
xfzTcpClient.writeAndFlush(json);
return sendNum;
}
else{
logger.info("TCP disconnected!");
return 0;
}
}
}

View File

@ -21,10 +21,10 @@ app.format.datetime = yyyy-MM-dd HH:mm:ss
mybatis-plus.configuration.map-underscore-to-camel-case=false
#xfz.server.host = 171.106.48.63
#xfz.server.port = 52000
xfz.server.host = 115.236.153.174
xfz.server.port = 31035
xfz.server.host = 171.106.48.63
xfz.server.port = 52000
#xfz.server.host = 115.236.153.174
#xfz.server.port = 31035
kingma.server.login_user = ceshi
kingma.server.login_pwd = ceshi!123