1、优化TCPClient。TCP断开马上重连的问题:在异常情况下,如连上马上被断开,会出现死循环;改为发数据前的一分钟重连
This commit is contained in:
parent
73eebcd493
commit
121771b068
@ -45,7 +45,7 @@ public class TCPClient {
|
|||||||
@Override
|
@Override
|
||||||
protected void initChannel(SocketChannel ch) throws Exception {
|
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) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}, 3000, TimeUnit.MILLISECONDS);
|
}, 5000, TimeUnit.MILLISECONDS);
|
||||||
} else {
|
} else {
|
||||||
logger.info("xfz tcp client start success!");
|
logger.info("xfz tcp client start success!");
|
||||||
}
|
}
|
||||||
@ -94,4 +94,8 @@ public class TCPClient {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isActive(){
|
||||||
|
return channel.isActive();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,12 +15,6 @@ public class TcpMessageHandler extends SimpleChannelInboundHandler<ByteBuf> {
|
|||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
private final TCPClient tcpClient;
|
|
||||||
|
|
||||||
public TcpMessageHandler(TCPClient tcpClient) {
|
|
||||||
this.tcpClient = tcpClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf buf) throws Exception {
|
protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf buf) throws Exception {
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
@ -38,7 +32,6 @@ public class TcpMessageHandler extends SimpleChannelInboundHandler<ByteBuf> {
|
|||||||
@Override
|
@Override
|
||||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||||
logger.info("xfz tcp channel inactive");
|
logger.info("xfz tcp channel inactive");
|
||||||
tcpClient.connect();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -7,7 +7,6 @@ import com.imdroid.common.util.NumberUtils;
|
|||||||
import com.imdroid.secapi.dto.*;
|
import com.imdroid.secapi.dto.*;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
@ -39,30 +38,29 @@ public class GXXfzForwarder extends Forwarder{
|
|||||||
@PostConstruct
|
@PostConstruct
|
||||||
void registerMe(){
|
void registerMe(){
|
||||||
init(FORWARDER_NAME, "TCP "+host+":"+port);
|
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解算结果
|
* 每半小时转发GNSS解算结果
|
||||||
*/
|
*/
|
||||||
@Scheduled(cron = "0 0/30 * * * ?") // 每30分钟执行一次
|
@Scheduled(cron = "0 0/30 * * * ?") // 每30分钟执行一次
|
||||||
private void forwardGnss() {
|
private void forwardGnss() {
|
||||||
// 1.按项目检索最近半小时的解算结果记录,推送Enable为true的记录
|
|
||||||
// 2.如果某终端最近半小时没有数据,则记录本次时间为待补传时间
|
|
||||||
if(xfzTcpClient == null){
|
|
||||||
xfzTcpClient = new TCPClient();
|
|
||||||
xfzTcpClient.init(host, port);
|
|
||||||
xfzTcpClient.start();
|
|
||||||
}
|
|
||||||
forwardCurrentGnss(FORWARDER_NAME);
|
forwardCurrentGnss(FORWARDER_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Scheduled(cron = "0 40 * * * ?") // 每小时的40分钟执行一次
|
@Scheduled(cron = "0 40 * * * ?") // 每小时的40分钟执行一次
|
||||||
//@Scheduled(cron = "0 0/20 * * * ?") // 每20分钟执行一次
|
//@Scheduled(cron = "0 0/20 * * * ?") // 每20分钟执行一次
|
||||||
private void forwardHistoryGnss() {
|
private void forwardHistoryGnss() {
|
||||||
if(xfzTcpClient == null){
|
|
||||||
xfzTcpClient = new TCPClient();
|
|
||||||
xfzTcpClient.init(host, port);
|
|
||||||
xfzTcpClient.start();
|
|
||||||
}
|
|
||||||
forwardHistoryGnss(FORWARDER_NAME);
|
forwardHistoryGnss(FORWARDER_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,10 +90,16 @@ public class GXXfzForwarder extends Forwarder{
|
|||||||
sendNum++;
|
sendNum++;
|
||||||
}
|
}
|
||||||
String json = GsonUtil.toJson(xfzTcpMessage);
|
String json = GsonUtil.toJson(xfzTcpMessage);
|
||||||
xfzTcpClient.writeAndFlush(json);
|
|
||||||
logger.info("project " + projectId + ": push calculation result to XFZ");
|
logger.info("project " + projectId + ": push calculation result to XFZ");
|
||||||
logger.info(json);
|
logger.info(json);
|
||||||
return sendNum;
|
if(xfzTcpClient.isActive()) {
|
||||||
|
xfzTcpClient.writeAndFlush(json);
|
||||||
|
return sendNum;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
logger.info("TCP disconnected!");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,10 +21,10 @@ app.format.datetime = yyyy-MM-dd HH:mm:ss
|
|||||||
|
|
||||||
mybatis-plus.configuration.map-underscore-to-camel-case=false
|
mybatis-plus.configuration.map-underscore-to-camel-case=false
|
||||||
|
|
||||||
#xfz.server.host = 171.106.48.63
|
xfz.server.host = 171.106.48.63
|
||||||
#xfz.server.port = 52000
|
xfz.server.port = 52000
|
||||||
xfz.server.host = 115.236.153.174
|
#xfz.server.host = 115.236.153.174
|
||||||
xfz.server.port = 31035
|
#xfz.server.port = 31035
|
||||||
|
|
||||||
kingma.server.login_user = ceshi
|
kingma.server.login_user = ceshi
|
||||||
kingma.server.login_pwd = ceshi!123
|
kingma.server.login_pwd = ceshi!123
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user