1、修改TCP的重连机制,只有在发送数据时才connect

This commit is contained in:
weidong 2025-07-20 22:53:32 +08:00
parent 2fd12b9361
commit f68d548a2c
3 changed files with 4 additions and 6 deletions

View File

@ -1,6 +1,5 @@
package com.imdroid.beidou_fwd.service; package com.imdroid.beidou_fwd.service;
import com.imdroid.common.util.ThreadManager;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
@ -13,7 +12,6 @@ import org.slf4j.LoggerFactory;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
public class TCPClient { public class TCPClient {
private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger logger = LoggerFactory.getLogger(this.getClass());
@ -93,7 +91,7 @@ public class TCPClient {
new Thread(this::connect, host+":"+port+" forwarder tcp-client").start(); new Thread(this::connect, host+":"+port+" forwarder tcp-client").start();
for(int i=0; i<20; i++){ for(int i=0; i<20; i++){
Thread.sleep(50); Thread.sleep(50);
if(channel.isActive()) return true; if(channel!=null && channel.isActive()) return true;
} }
return false; return false;
} }
@ -102,7 +100,7 @@ public class TCPClient {
ByteBuf sendBuffer = Unpooled.buffer(); ByteBuf sendBuffer = Unpooled.buffer();
sendBuffer.writeBytes(json.getBytes(StandardCharsets.UTF_8)); sendBuffer.writeBytes(json.getBytes(StandardCharsets.UTF_8));
logger.info("send to {}: {}",host,json); logger.info("send to {}: {}",host,json);
if(!channel.isActive()){ if(channel==null || !channel.isActive()){
try { try {
if(!tryReconnect()) return; if(!tryReconnect()) return;
} catch (Exception e) { } catch (Exception e) {

View File

@ -34,7 +34,7 @@ public class GXJSForwarder extends GXXfzForwarder{
/** /**
* 每半小时转发GNSS解算结果 * 每半小时转发GNSS解算结果
*/ */
@Scheduled(cron = "0 15,45 * * * ?") // 每30分钟执行一次 @Scheduled(cron = "0 0/10 * * * ?") // 每30分钟执行一次
private void forwardGnss() { private void forwardGnss() {
if(!enabled) return; if(!enabled) return;
logger.debug("gxjs forwardGnss"); logger.debug("gxjs forwardGnss");

View File

@ -71,7 +71,7 @@ public class GXXfzForwarder extends Forwarder{
// 发送 // 发送
batchNum++; batchNum++;
if(batchNum==50){ if(batchNum==20){
String json = "#" + GsonUtil.toJson(xfzTcpMessage) + "!"; String json = "#" + GsonUtil.toJson(xfzTcpMessage) + "!";
//logger.debug("project {}: forwad {} gnss records to {}",projectId, dataList.size(),fwdGroupId); //logger.debug("project {}: forwad {} gnss records to {}",projectId, dataList.size(),fwdGroupId);
//logger.debug(json); //logger.debug(json);