无限重连

忽略软加载
读取sick加时间限制
chongqing-yashichuangneng
LAPTOP-S9HJSOEB\昊天 3 years ago
parent 71de5ccd65
commit 0563b52626

2
.gitignore vendored

@ -32,3 +32,5 @@ target
/modules/modules.iml /modules/modules.iml
/modules/filter/filter.iml /modules/filter/filter.iml
/modules/common/common.iml /modules/common/common.iml
lp.key
lp.lic

@ -82,6 +82,8 @@ public class ConfigProperties {
public static class KSEC{ public static class KSEC{
private String ip; private String ip;
private Integer port; private Integer port;
private Integer reconnectNum = 10;
private Integer reconnectInterval = 10;
} }
@Data @Data

@ -67,13 +67,18 @@ public class KsecNettyClient {
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
while (num < RECONNECT_NUM && !isConnected) { while ((ksec.getReconnectNum() == -1 || num < ksec.getReconnectNum() ) && !isConnected) {
try { try {
createClient(ksec); createClient(ksec);
} catch (Exception e) { } catch (Exception e) {
//没连上 继续 //没连上 继续
log.error("reconnect error num:{}", num); log.error("reconnect error num:{}", num);
num++; num++;
try {
Thread.sleep(ksec.getReconnectInterval()*1000);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
continue; continue;
} }
isConnected = true; isConnected = true;

@ -8,6 +8,10 @@ import java.io.*;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/** /**
* sick * sick
@ -35,7 +39,7 @@ public class SickSocket {
while ("NoRead".equals(code) && i <= 4){ while ("NoRead".equals(code) && i <= 4){
Long start = System.currentTimeMillis(); Long start = System.currentTimeMillis();
writeCmd(os); writeCmd(os);
code = read(is); code = read(is,ip);
Long end = System.currentTimeMillis(); Long end = System.currentTimeMillis();
log.info("sick read time:{}",(end-start)/1000); log.info("sick read time:{}",(end-start)/1000);
tcpLogger.info("count:{},ip:{},code:{}",i,ip,code); tcpLogger.info("count:{},ip:{},code:{}",i,ip,code);
@ -81,9 +85,35 @@ public class SickSocket {
os.write(bytes); os.write(bytes);
} }
private static String read(InputStream inStream) throws IOException {
private static String read(InputStream inStream,String ip) throws IOException {
BufferedReader bd = new BufferedReader(new InputStreamReader(inStream)); BufferedReader bd = new BufferedReader(new InputStreamReader(inStream));
return bd.readLine(); StringBuilder stringBuilder = new StringBuilder();
FutureTask<StringBuilder> future = new FutureTask <>(()->{
StringBuilder bdString = new StringBuilder();
while (true) {
try {
bdString.append((char) bd.read());
} catch (IOException exc) {
throw new RuntimeException(exc);
}
if (bdString.toString().contains("\\n") || bdString.toString().contains("\n")) {
break;
}
}
return bdString;
});
try {
new Thread(future).start();
stringBuilder = future.get(1000, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
tcpLogger.error("sick 可以连接但发送返回存在问题,ip:{},info:{}", ip, e);
log.error("sick 可以连接但发送返回存在问题,ip:{},info:{}", ip, e);
throw new RuntimeException(e);
}
return stringBuilder.toString();
} }
} }

Loading…
Cancel
Save