This commit is contained in:
caixiang 2022-08-09 14:16:03 +08:00
parent 3b1c667915
commit e373cd8e01

View File

@ -283,41 +283,41 @@ public enum S7Client {
} }
private void check_ping(){ private void check_ping(){
while (true){ ping_fail_check.execute(new Runnable() {
try {
ScheduledFuture<?> schedule = ping_fail_check.schedule(new Runnable() {
@Override @Override
public void run() { public void run() {
if (connections.size() != coreSize) { while (true){
int c = coreSize - connections.size(); try {
for (int z = 0; z < c; z++) { if(connections.size()!=coreSize){
int c = coreSize-connections.size();
for(int z=0;z<c;z++){
S7Connector connect = connect(host, rack, slot); S7Connector connect = connect(host, rack, slot);
if (connect != null) { if(connect!=null){
logger.info("host:" + host + " ;;(check_ping()) 检测到有断线 ==》 现在断线恢复成功"); logger.info("host:"+host +" ;;(check_ping()) 检测到有断线 ==》 现在断线恢复成功");
connections.add(connect); connections.add(connect);
} else { }else {
logger.info("host:" + host + " ;;(check_ping()) 检测到有断线 ==》 现在断线恢复失败"); logger.info("host:"+host +" ;;(check_ping()) 检测到有断线 ==》 现在断线恢复失败");
} }
} }
} }
} Thread.sleep(10000);
}, 10000, TimeUnit.SECONDS);
Object o = schedule.get();
}catch (Exception e){ }catch (Exception e){
logger.info("( out catched ) host:"+host +" ;;(check_ping) "+"check connect.size 出现异常errMessage is : "+e.getMessage()+"现在进入下一次轮询"); logger.info(" ( outside check_ping catched ) host:"+host +" ;;(check_ping()) Thread.sleep 异常,异常原因:"+e.getMessage());
} }
} }
} }
});
}
//todo 当网络断开之后下面这个ping() 循环不生效 可能是read = connector.read( 出来是null但还是继续通过了也可能是 当连接全部断开后 getConnector(); 应该是取不到数据的也就会报null这个明天再看看 //todo 当网络断开之后下面这个ping() 循环不生效 可能是read = connector.read( 出来是null但还是继续通过了也可能是 当连接全部断开后 getConnector(); 应该是取不到数据的也就会报null这个明天再看看
private void ping(){ private void ping(){
while (true){ executor.execute(new Runnable() {
try {
ScheduledFuture<?> schedule = executor.schedule(new Runnable() {
@Override @Override
public void run() { public void run() {
for (int i = 0; i < coreSize; i++) { while (true){
try {
for(int i=0;i<coreSize;i++){
S7Connector connector = getConnector(); S7Connector connector = getConnector();
try { try {
//只要没有报异常 都是通讯正常的 //只要没有报异常 都是通讯正常的
@ -328,41 +328,43 @@ public enum S7Client {
heartBeat.getByteOffset(), heartBeat.getByteOffset(),
heartBeat.getBitOffset(), heartBeat.getBitOffset(),
heartBeat.getType().getTransportSize()); heartBeat.getType().getTransportSize());
System.out.println("host:" + host + " ;; " + connector.hashCode() + " : ping");
System.out.println("host:"+host +" ;; "+connector.hashCode()+" : ping");
Thread.sleep(100); Thread.sleep(100);
} catch (Exception e) { }catch (Exception e){
logger.info("host:" + host + " ;;(ping) " + connector.hashCode() + " : connection error" + "errMessage is : " + e.getMessage()); logger.info("host:"+host +" ;;(ping) "+connector.hashCode()+" : connection error"+"errMessage is : "+e.getMessage());
//先把 socket close掉 //先把 socket close掉
try { try {
connector.close(); connector.close();
connections.remove(connector); connections.remove(connector);
//如果是网络波动照成的socket断开 等个1S 再重连试试 //如果是网络波动照成的socket断开 等个1S 再重连试试
Thread.sleep(100); Thread.sleep(100);
} catch (Exception ee) { }catch (Exception ee){
logger.info("host:" + host + " ;;(ping) " + "connector.close() 出现异常errMessage is : " + ee.getMessage()); logger.info("host:"+host +" ;;(ping) "+"connector.close() 出现异常errMessage is : "+ee.getMessage());
} }
} }
//todo 把之前的连接close 然后新增一个连接到connections //todo 把之前的连接close 然后新增一个连接到connections
if (connections.size() != coreSize) { if(connections.size()!=coreSize){
int c = coreSize - connections.size(); int c = coreSize-connections.size();
for (int z = 0; z < c; z++) { for(int z=0;z<c;z++){
S7Connector connect = connect(host, rack, slot); S7Connector connect = connect(host, rack, slot);
if (connect != null) { if(connect!=null){
logger.info("host:" + host + " ;;(ping) " + "ping时候出现异常尝试重连 重连成功!!"); logger.info("host:"+host +" ;;(ping) "+"ping时候出现异常尝试重连 重连成功!!");
connections.add(connect); connections.add(connect);
} else { }else {
logger.info("host:" + host + " ;;(ping) " + "ping时候出现异常尝试重连 重连时候还是 出现异常。。"); logger.info("host:"+host +" ;;(ping) "+"ping时候出现异常尝试重连 重连时候还是 出现异常。。");
} }
} }
} }
} }
} Thread.sleep(30000);
}, 30000, TimeUnit.SECONDS);
Object o = schedule.get();
}catch (Exception e){ }catch (Exception e){
logger.info("( out catched ) host:"+host +" ;;(ping) "+"ping的时候 整体 出现异常errMessage is : "+e.getMessage()+"现在进入下一次轮询"); e.printStackTrace();
logger.info(" ( outside ping catched ) host:"+host +" ;;(ping) "+e.getMessage());
} }
} }
} }
});
}
} }