From fdb22c3ebe2e3a5dbafc252977b7621c4ab39dbc Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期三, 06 九月 2023 10:08:28 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/thread/NyShuttleThread.java |   32 ++++++++++++++++++++++++++++----
 1 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/NyShuttleThread.java b/src/main/java/com/zy/core/thread/NyShuttleThread.java
index 302e7f0..ff5a4df 100644
--- a/src/main/java/com/zy/core/thread/NyShuttleThread.java
+++ b/src/main/java/com/zy/core/thread/NyShuttleThread.java
@@ -87,7 +87,7 @@
 
     private void read() {
         try {
-            if (!this.socket.isConnected()) {
+            if (this.socket == null || this.socket.isClosed()) {
                 //閾炬帴鏂紑閲嶆柊閾炬帴
                 this.connect();
             }
@@ -117,7 +117,7 @@
             NyShuttleHttpCommand readStatusCommand = NyHttpUtils.getReadStatusCommand(slave.getId());
             JSONObject jsonObject = NyHttpUtils.requestCommand(socket, readStatusCommand);
             if (jsonObject == null) {
-                OutputQueue.SHUTTLE.offer(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅plc鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+                OutputQueue.SHUTTLE.offer(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅Socket鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
             }else {
                 //鎵嬪姩鐘舵��/鑷姩鐘舵��
                 shuttleProtocol.setWorkingMode(jsonObject.getInteger("workingMode"));
@@ -243,7 +243,15 @@
             }
         } catch (Exception e) {
             e.printStackTrace();
-            OutputQueue.SHUTTLE.offer(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅plc鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+            OutputQueue.SHUTTLE.offer(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅Socket鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+            try {
+                this.socket.close();
+                this.socket = null;
+                Thread.sleep(1000);
+                this.connect();
+            } catch (IOException | InterruptedException exception) {
+                e.printStackTrace();
+            }
         }
     }
 
@@ -252,7 +260,9 @@
         try {
             Socket socket = new Socket(slave.getIp(),slave.getPort());
             socket.setSoTimeout(60000);
+            socket.setKeepAlive(true);
             this.socket = socket;
+            log.info(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅Socket閾炬帴鎴愬姛 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
         } catch (IOException e) {
             OutputQueue.SHUTTLE.offer(MessageFormat.format("銆恵0}銆戝洓鍚戠┛姊溅Socket閾炬帴澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
         }
@@ -282,7 +292,21 @@
         }
 
         //鍙戝嚭璇锋眰
-        JSONObject result = NyHttpUtils.requestCommand(socket, command);
+        JSONObject result = null;
+        try {
+            result = NyHttpUtils.requestCommand(socket, command);
+        } catch (IOException e) {
+            try {
+                this.socket.close();
+                this.socket = null;
+                Thread.sleep(1000);
+                this.connect();
+            } catch (IOException exception) {
+                exception.printStackTrace();
+            } catch (InterruptedException ex) {
+                throw new RuntimeException(ex);
+            }
+        }
         if (result == null) {
             return false;//璇锋眰澶辫触
         }

--
Gitblit v1.9.1