#
zjj
2025-09-23 b6765d9c6102b83002ead34bfbdb39518777e0b9
#
1个文件已添加
1个文件已修改
42 ■■■■ 已修改文件
src/main/java/com/zy/core/thread/impl/NyShuttleThread.java 21 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/utils/TimeoutExecutor.java 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/core/thread/impl/NyShuttleThread.java
@@ -15,6 +15,7 @@
import com.zy.core.enums.SlaveType;
import com.zy.core.thread.ShuttleThread;
import com.zy.core.utils.FakeDeviceUtils;
import com.zy.core.utils.TimeoutExecutor;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
@@ -24,6 +25,7 @@
import java.net.SocketTimeoutException;
import java.text.MessageFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Slf4j
@SuppressWarnings("all")
@@ -54,8 +56,13 @@
                try {
                    this.connect();
                    // 在循环中使用
                    boolean result = TimeoutExecutor.executeWithTimeout(
                            () -> this.listenSocketMessage(),
                            30,  // 30秒超时
                            TimeUnit.SECONDS
                    );
                    Thread.sleep(200);
                    listenSocketMessage();
                } catch (Exception e) {
                    e.printStackTrace();
                }
@@ -131,11 +138,11 @@
        }
    }
    private void listenSocketMessage() {
    private boolean listenSocketMessage() {
        StringBuffer sb = new StringBuffer();
        try {
            if (this.socket == null) {
                return;
                return false;
            }
            DeviceMsgUtils deviceMsgUtils = null;
@@ -144,7 +151,7 @@
            } catch (Exception e) {
            }
            if (deviceMsgUtils == null) {
                return;
                return false;
            }
            // 获取输入流
@@ -168,7 +175,7 @@
            }catch (Exception e){}
            if(result == null) {
                return;
                return false;
            }
            JSONObject header = null;
@@ -225,11 +232,13 @@
                }
            }
        } catch (SocketTimeoutException e) {
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            closeSocket();
            return false;
        }
        return true;
    }
    public JSONObject parseSocketResult(JSONObject data) {
src/main/java/com/zy/core/utils/TimeoutExecutor.java
New file
@@ -0,0 +1,21 @@
package com.zy.core.utils;
import java.util.concurrent.*;
public class TimeoutExecutor {
    public static <T> T executeWithTimeout(Callable<T> task, long timeout, TimeUnit unit) throws Exception {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<T> future = executor.submit(task);
        try {
            return future.get(timeout, unit);
        } catch (TimeoutException e) {
            future.cancel(true); // 中断任务
            throw new TimeoutException("Task timed out");
        } finally {
            executor.shutdownNow();
        }
    }
}