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