Parcourir la source

光栅代码测试加优化(解决了客户端连接断开之后线程不会释放的问题,会造成线java程序占用过多cup资源

k il y a 2 ans
Parent
commit
c1218c0b76

+ 2 - 2
ruoyi-admin/src/main/java/com/ruoyi/init/StartService.java

@@ -38,7 +38,7 @@ public class StartService implements CommandLineRunner {
         }
 
         //光栅检测
-//        ServerGs server = new ServerGs(2002,configService);
-//        server.start();
+        ServerGs server = new ServerGs(2002,configService);
+        server.start();
     }
 }

+ 21 - 5
ruoyi-admin/src/main/java/com/ruoyi/thread/ClientHandler.java

@@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.net.Socket;
 import java.util.Date;
 
@@ -32,7 +33,6 @@ public class ClientHandler {
     }
 
     public void start() {
-        log.info("新客户端接入");
         new Thread(new Runnable() {
             @Override
             public void run() {
@@ -45,12 +45,20 @@ public class ClientHandler {
      * 对客户端的业务处理,接收并重写回去
      */
     private void doStart() {
-
+        InputStream inputStream = null;
+        OutputStream outputStream = null;
         try {
-            InputStream inputStream = socket.getInputStream();
+            inputStream = socket.getInputStream();
+            outputStream = socket.getOutputStream();
             while (true) {
                 byte[] data = new byte[MAX_DATA_LEN];
-
+                try {
+                    Thread.sleep(1000);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+                // 服务器向客户端写,如果客户端断开了,这边会抛出异常,释放线程
+                outputStream.write(ByteUtil.hexString2ByteArray("0000000000"));
                 //根据数据判断是否超高,然后变更字段信息
                 while ((inputStream.read(data)) != -1) {
 
@@ -133,7 +141,15 @@ public class ClientHandler {
                 }
             }
         } catch (IOException e) {
-            e.printStackTrace();
+            log.info("光栅服务器读,线程资源释放:" + socket.getInetAddress() + ",p:" + socket.getPort());
+        } finally {
+            try {
+                inputStream.close();
+                outputStream.close();
+                socket.close();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
         }
     }
 

+ 31 - 13
ruoyi-admin/src/main/java/com/ruoyi/thread/ServerGs.java

@@ -27,18 +27,26 @@ public class ServerGs {
     private ServerSocket serverSocket;
 
     private ISysConfigService configService;
+
     /**
      * 监听端口
      *
      * @param port
      */
-    public ServerGs(int port,ISysConfigService configService) {
+    public ServerGs(int port, ISysConfigService configService) {
         try {
             this.serverSocket = new ServerSocket(port);
             this.configService = configService;
-            System.out.println("服务端启动成功,端口:" + port);
+            System.out.println("光栅服务端启动成功,端口:" + port);
         } catch (IOException e) {
-            System.out.println("服务端启动失败");
+            System.out.println("光栅服务端启动失败");
+            if (serverSocket != null) {
+                try {
+                    serverSocket.close();
+                } catch (IOException ex) {
+                    ex.printStackTrace();
+                }
+            }
         }
     }
 
@@ -61,12 +69,14 @@ public class ServerGs {
         while (true) {
             try {
                 Socket client = serverSocket.accept();
+                log.info("----->有光栅客户端新连接:ip," + client.getInetAddress() + ",p:" + client.getPort());
 
                 new Timer().schedule(new TimerTask() {
                     @Override
                     public void run() {
+                        OutputStream os = null;
                         try {
-                            OutputStream os = client.getOutputStream();
+                            os = client.getOutputStream();
 
                             //根据参数判定哪个光栅进行超高检测  > condifvalue为Y开启光栅检测,N关闭光栅检测
                             SysConfig config = new SysConfig();
@@ -74,41 +84,49 @@ public class ServerGs {
                             config.setConfigValue("Y");
                             List<SysConfig> list = configService.selectConfigList(config);
 
-                            for (int i=0; i<list.size(); i++){
+                            for (int i = 0; i < list.size(); i++) {
 
                                 //发送检测信号到光栅PLC
                                 switch (list.get(i).getConfigKey()) {
                                     case "sys.guangshan.no1":
                                         // 光栅1 发送检测信号
-                                        log.info("------------光栅1 发送检测信号------------>");
+//                                        log.info("------------光栅1 发送检测信号------------>");
                                         client.getOutputStream().write(ByteUtil.hexString2ByteArray("3F00010D0A"));
                                         break;
                                     case "sys.guangshan.no2":
                                         // 光栅2 发送检测信号
-                                        log.info("------------光栅2 发送检测信号------------>");
+//                                        log.info("------------光栅2 发送检测信号------------>");
                                         client.getOutputStream().write(ByteUtil.hexString2ByteArray("3F00020D0A"));
                                         break;
                                     case "sys.guangshan.no3":
                                         // 光栅3 发送检测信号
-                                        log.info("------------光栅3 发送检测信号------------>");
+//                                        log.info("------------光栅3 发送检测信号------------>");
                                         client.getOutputStream().write(ByteUtil.hexString2ByteArray("3F00030D0A"));
                                         break;
                                     case "sys.guangshan.no4":
                                         // 光栅4 发送检测信号
-                                        log.info("------------光栅4 发送检测信号------------>");
+//                                        log.info("------------光栅4 发送检测信号------------>");
                                         client.getOutputStream().write(ByteUtil.hexString2ByteArray("3F00040D0A"));
                                         break;
                                 }
                             }
                             os.flush();
                         } catch (Exception e) {
-                            log.error(e.getMessage());
+                            log.error("光栅客户端断开:" + e.getMessage());
+                            //关闭连接
+                            try {
+                                os.close();
+                                client.close();
+                            } catch (Exception ex) {
+                                ex.printStackTrace();
+                            }
+                            throw new RuntimeException("光栅客户端断开,释放线程资源:" + e.getMessage());
                         }
                     }
-                }, 10000, 5000);
-                new ClientHandler(client,configService).start();
+                }, 6000, 3000);
+                new ClientHandler(client, configService).start();
             } catch (IOException e) {
-                System.out.println("服务端异常");
+                System.out.println("光栅服务端异常:" + e);
             }
         }
     }